Tutorials

Welcome to the Absolute Web Dev tutorials page. All tutorials are made by request. If you need a tutorial, please email me with your request and I will create a tutorial that will fulfill your needs.

James’s Easy PHP Email Script

Written by WebMaster   |  06/09/2010

Sending email through a web based contact form is what everyone needs for their website. Programs like FormMail use long and cumbersome codes to acheive the simplest results. With the following code, you will be able to send emails to your email address through your contact page and create an auto responder that emails the person that filled out the form. The following link is the zipped version of the code that you can save and utilize immediately with your website. Click here to download the script

Validating the form data

The following code comes before the header of the html document. This code will validate the user input. The good thing about this code is that you can duplicate the code and insert different Regular expressions to validate different types of input.


This section must be placed before the header section of the HTML document. 
This is for input validation to see if the correct information was entered into the form.
if (isset($_REQUEST['submitted'])) {
	// Initialize error array.
	$errors = array();
	// Check for a proper name
	if (!empty($_REQUEST['name'])) {
		$name = $_REQUEST['name'];
		$pattern = "/^[a-zA-Z0-9\_]{2,20}/";
        // This is a regular expression that checks if the name is valid characters
		if (preg_match($pattern,$name)){ $name = $_REQUEST['name'];}
		else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}
	} else {$errors[] = 'You forgot to enter your Name.';}
	//Check for a valid email address
	if (!empty($_REQUEST['email'])) {
		$email = $_REQUEST['email'];
		$pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
        //This is a common email regular expression designed to check if the email address is valid
		if (preg_match($pattern,$email)){ $email = $_REQUEST['email'];}
		else{ $errors[] = 'Your Email was invalid.';}
	} else {$errors[] = 'You forgot to enter your Email.';}
	//This the comment the user sent to the contact
	if (!empty($_REQUEST['comment'])) {
		$comment = $_REQUEST['comment'];
		$comment = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', 
        '<a href="$1" target="_blank" class="none">$1</a>', $comment);//changes  urls into links
		$comment = nl2br($comment);//This inserts <br />when the return key is pressed
	} else {$errors[] = 'You forgot to enter your Comment.';}
	// This matches the Verification box to prevent spammers from emailing you
	if (!empty($_REQUEST['very'])) {
		$very = $_REQUEST['very'];
		$rand = $_REQUEST['rand'];
		if ($very == $rand){ /*Do nothing*/}
		else{ $errors[] = 'The verification code you entered was wrong.';}
	} else {$errors[] = 'You forgot to enter the verification code.';}

If the form has no errors

If there are no errors, the form will go through, executing the php email function. The email function is written twice. One is for the person who filled out the form and the other is for the person who the user is trying to contact. The email function will have the message that the user typed into the form sent to them. A simple responder is set up for the user to get in the mail showing that the email they sent went through and that they will be contacted soon.

 
If everything validates correctly. 
The following will send emails to the contact and the person filling out the form
*/
if (empty($errors)) { 
		
	$from = "From: Our Site!";//Site name
	$to = "john@doe.com";//Contact email address
	$subject = "Admin - Our Site! Comment from " . $name . "";
	
	//The following is the Message that the is sent to the contect
	$message1 = "Message from " . $name . " 
	Email: ".$email." 
	Message: ".stripslashes($comment)."";
	mail($to,$subject,$message1,$from);
	//End of contact info
	
	//The is the response email that confirms that the email was sent to the contact.
	$subject2 = "Thank You for your comment at Our Site!";	
	$message2= "Thank You " . $name . " for your comment.
	Please allow me 48 hours to respond to your comment.
	Thank You for your concern.
	John Doe
	john@doe.com
	
	** Your Response Information that you want the person who emailed you to see.
	";
	mail($email,$subject2,$message2,$from);
	//End of auto responder
}
}
//End of validation script. 

1 2 3 Next
Archive
TOP

  100% compatible with:
Firefox Explorer Opera Safari Flock Netscape Chrome

 100% valid CSS 3.0 and XHTML 1.0.
Valid CSS Valid XHTML