PHP script to send email

Its very important for website owners and admins to get feedback from their visitors. There are various methods to be in touch with your visitors and readers and the most common method is through Contact forms. If you are beginner then let me tell you how a contact form works:

A simple contact form collects basic information of user like email id, name, website address(if any) and feedback or comment from user. As soon as user enters his/her details in contact form and submits the form, it sends an email to the admin of the website containing all the details that were filled by the user. So that admin can get the information within his inbox and reply to user as soon as possible.

I am giving you script in PHP that will send you the email containing information contact form’s data. In this tutorial I have written two files:

1>    Form.html (It will collect data from visitors)

2>    Email.php (It will get data from form.html and will validate that data then after successful validation it will send a mail to the admin).

You can check the LIVE DEMO HERE

Code for form.html:

<head><title>My Simple Contact form </title>
<form action="email.php" method="post" name="contactform">
<table width="500">
<td align="top"><label>First Name *</label></td>
<td align="top"><input type="text" name="fname" size="30"
maxlength="50" /></td>
<td align="top"><label>Last Name *</label></td>
<td align="top"><input type="text" name="lname" size="30"
maxlength="50" /></td>
<td align="top"><label>Email Address *</label></td>
<td align="top"><input type="text" name="email_id" size="30"
maxlength="5" /></td>
<td align="top"><label>Website </label></td>
<td align="top"><input type="text" name="website_url" size="30"
maxlength="30" /></td>
<td align="top"><label>Your Response *</label></td>
<td align="top"><textarea name="comment" rows="6" cols="25">
<td style="text-align: center;" colspan="2"><input type="submit"
value="Submit" /></td>

Code for email.php:

if(isset($_POST['email_id'])) {

$email_to = "Enter Your Email Address Here";  // This email address will recieve the data of form.html
$email_subject = "Subject part of email";   // This would be the subject of email that you will recieve

function catch_error($error) {
// Put error message here that you want to be displayed to user
echo "Problem encountered with the form you submitted!. <br />";
echo "Following error occured with your form please fix.<br /><br />";
echo $error."<br /><br />";


// validation expected data exists
if(!isset($_POST['fname']) ||
!isset($_POST['lname']) ||
!isset($_POST['email_id']) ||
!isset($_POST['comment'])) {
died('There is a problem with the form u submitted.Please Resubmit');

$fname = $_POST['fname']; // required
$lname = $_POST['lname']; // required
$email_id = $_POST['email_id']; // required
$website = $_POST['website_url']; // not required
$comment = $_POST['comment']; // required

$error_message = "";
//Using regular expressions for email id validation
$email_verfiy = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_verfiy,$email_id)) {
$error_message .= 'The Email Address you entered is not valid.<br />';
$name_verify = "/^[A-Za-z .'-]+$/";
//Using regular expressions for name validation
if(!preg_match($name_verify,$fname)) {
$error_message .= 'The First Name you entered is not valid.<br />';
if(!preg_match($name_verify,$lname)) {
$error_message .= 'The Last Name you entered is not valid.<br />';
if(!filter_var($website, FILTER_VALIDATE_URL)){
// Website URL validation
$error_message .='Your Website URL is not valid. <br />';
if(strlen($comment) < 2) {
$error_message .= 'The Comment you entered is not valid.<br />';
if(strlen($error_message) > 0) {
$email_message = "Form details below.\n\n";

function clean_text($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);

$email_message .= "First Name: ".clean_text($fname)."\n";
$email_message .= "Last Name: ".clean_text($lname)."\n";
$email_message .= "Email: ".clean_text($email_id)."\n";
$email_message .= "Website: ".clean_text($website)."\n";
$email_message .= "Comments: ".clean_text($comment)."\n";

@mail($email_to, $email_subject, $email_message);

Put your custom message here or redirect user to any other page
after successful submission of form.<br/>
for example a simple message like this:<br /> */
echo "Your form has been Submitted Successfuly.!!
We will be back to you ASAP";


You can use this code in your web projects and edit the code according to your need. Add some CSS to style your form.html page.

NOTE :I have used regular expressions for validation of email, name etc. If you are a beginner to PHP then it may be hard for you to understand regular expressions. If you want to know about regular expressions and how to use them than please comment below.

About sanjeev

Dreamer, Blogger and Thinker. I love to help people in solving their problems. You can also join me HERE

Related posts:

11 Responses so far.

  1. Hey can you suggest me script or php code for detecting ipad and iphone device.

    Actually i am working on HTML 5 project of drupal 6 website to make responsive according to devices like ipad, iphone android….
    in css part i used media tags to detect devices but by using php code or script i am not able to detect ipad and iphone. this is the domain name of site.please go through this website.

  2. Hi Karuna,
    You can detect OS with PHP script, Grab the user agent and search for ‘iPhone’ or ‘iPad’. You can use this code:

    < ?php>
    header(“Location: “your target url”);


    You can also use javascript or .htaccess file to detect users os and browser and redirect them to desired url.

  3. What code do I have to use then, so, after the visiter pushed the submit button, his entries also are being send to his emailaddress? I can’t find a ‘simple’ explanation on the web.


  4. Problem encountered with the form you submitted!.
    Following error occured with your form please fix.

    Your Website URL is not valid.

    I have the errors above,
    could you please let me know?

  5. Hi – Nice form but is there a way to display the error messages on the same page as the form entry – thanks

  6. i have tried it but it gives following error

    Parse error: syntax error, unexpected ‘<' in C:\wamp\www\infotuts\php-script-send-email\email.php on line 68

Leave a Reply

Your email address will not be published. Required fields are marked *