Using php to mail a form is a simple task. Grab the codes below to learn how to do it yourself. This is continuation of our last lesson "Create a Form in a PHP Function".

Edit the Functions.php File

In order to accomplish this you will edit your functions.php form to look like it does below. This will give you more form inputs like email, subject and message.
<?php

function form(){
echo '
<form method="post" action="">
<input type="text"name="firstName"placeholder="First Name"/>
<input type="email" name="email"placeholder="email"/>
<input type="text" name="subject" placeholder="subject" />
<textarea name="message"placeholder="your message here"></textarea>
<input type="submit" name="submit" value="submit"/>
</form>

';
}

?>

Edit the Index.php File

Now edit your index.php file so that you can grab the post variables in php. After grabbing the post variables you will be able to email yourself a copy.
<?php if(isset($_POST['submit'])){ echo $_POST['firstName'].' your email has been sent'; $to=$_POST['email']; $subject=$_POST['subject']; $message=$_POST['message']; $firstname=$_POST['firstName']; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); } include('functions.php'); form(); ?>
xxx

Sign Up To Comment