Previous Page

Receive form submission information in an email

NOTE: This is the very basic code to get information from a FORM and handle it with PHP then send it to yourself in an email. This will get that simple job done, but there are other ways to handle the users input to make sure they are entering a correct email address. You may want them to input the email twice, just incase. You will have to check that in your php code. Any questions about how to do any of that, just contact me.


You can insert the following code above the html portion of your new .php document

<?php

	$contact_name = $_POST['Name'];	//name from the form
	$contact_email = $_POST['Email'];// email from the form	
	$contact_comment = $_POST['comment_here'];	//comment from the form
	
	$errorMessage = '';// variable for error message
	$confirm =''; // variable for confirmation message
if(isset($contact_name) && isset($contact_email) && isset($contact_comment))// if nothing exists in fields, then nothing will happen
{
		if(!empty($contact_name) && !empty($contact_email) && !empty($contact_comment))// if they enter information in all fields of form
		{
//========================================Send yourself and email with the information entered in the form ====================================

			$to = 'brian@bcbutler.com'; // YOUR email address will go here!!!!
			$subject = 'CONTACT FORM SUBMITTED'; // enter what you want the subject to say when you receive it
			$body = "Name: ".$contact_name."\nEmail address: ".$contact_email."\n\nComment:\n\n".$contact_comment;//body of email
			$headers = 'From: '.$contact_email;	// who sent you this email
		
				if (mail($to, $subject, $body, $headers)) // This will email the submission to you. You can then output a
														  // confirmation and email them a confirmation if you want - as shown below
//===============================================================================================================================================				
													
				{
					$confirm = 'Thank you for your Submission! A confirmation has been sent to the email provided.' ; // echo this so they know
																									   // the form info will be received by you
				 
//========================================Send a confirmation email to the person contacting you ==============================================
				  
					$sendto = $_POST['Email']; // this is the email address collected form the form
					
					$subject = 'bcbutler.com'; // Subject
					
					// To send HTML mail, the Content-type header must be set - then you can include html tags in the email
					$headers  = 'MIME-Version: 1.0' . "\r\n";
					$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
					
					$message = 'Thank you for your Submission!';//message to person submitting the form
	
	
					// This is the function to send the email
					mail($sendto, $subject, $message, $headers);  
//===============================================================================================================================================				
				
				}//END    if(mail($to, $subject, $body, $headers))
				else
				{
					$errorMessage = 'Sorry, there was a problem submitting the form.';// hopefully they never see  this message
						
				}
        }// END   if(!empty($contact_name) && !empty($contact_email) && !empty($contact_comment))
		
    	else
		{
			$errorMessage = 'Please fill in all fields!';         
		}
}// END       if(isset($contact_name) && isset($contact_email) && isset($contact_comment))
?>




Below is a simple form that gets processed with our php from above. NOTE: Place this code inside the body tags


<form action="" method="post">
<h2>Please fill in the form below:</h2>
<table>
<tr><td>Name</td>
<td><input  maxlength="60" name="Name" size="30" type="text" /></td>
</tr>

<tr><td>E-mail Address</td>
<td><input  maxlength="60" name="Email" size="30" type="text" /></td>
</tr>

</table>

<br />
<h2>Please enter your comment below:</h2>

<div class=".comment_scrollbar"><textarea name="comment_here" rows="12" cols="70" ></textarea></div>


<div style="padding-top:5px; "><input type="submit" style="width:130px; height:45px; font-size:1.7em;" value="Submit" />
</div>

</form>

Below are a couple of variables from the php that we need to output. NOTE: Place this code below your form

<?php
	echo $confirm;
	echo $errorMessage;
?>