HTML Form To Email

Use the following HTML code to create a basic feedback form that sends you an email when submitted. When a visitor submits this form, the PHP script will generate an email to the webmaster containing the contents of the completed form.

The PHP script is a basic script but it works well. Because of its simplicity, it is very easy to follow. Feel free to customize the code for your own purposes.

View Example

The following code creates this feedback form.

To get the code for this feedback form, download the zip file below.

Download

This is the quickest way to get the form to email script. Simply download the .zip file, customize the files, and you're done.

This .zip file consists of four files:

  1. The feedback form
  2. The "send mail" script
  3. A "Thank you" page
  4. An error message page

All four files are required to create the working feedback form.

Download: feedback_form.zip.

Installation:

Once you've downloaded the .zip file above, do the following:

  1. Extract all four files to a directory located within your website (on your local machine). If you don't have a copy of your website on your local machine, any directory will do for now, as long as you upload all files to your hosting provider after the following steps.
  2. Open send_mail.php and find the line that reads $webmaster_email = "name@example.com"; (at the top of the file). Change the name@example.com email address to your own email address (or where ever the form contents should be emailed to).
  3. Once you have checked that the form works on your server, customize the look and feel of feedback_form.html, thank_you.html, and error_message.html to suit your website.

If you change the names of the files, be sure to update the PHP script to reflect the new names.

Also, if you are familiar with HTML/PHP, you could combine the code from all four files into one PHP file. However, this would require substantial modification so, unless you know what you're doing, keep them as four separate files.

If you can't download or access the .zip file, see below.

Modify Your Feedback Form

You can change the look of your feedback form by adding styles to it. Check out these comment boxes to change the color of your feedback form, add a background picture, or add a border to your comment box.

Can't Download The .Zip File?

If you can't download or access the .zip file above, you can copy and paste the code below. Make sure you change the email address at the top of send_mail.php (as per installation instructions above).

  1. The Feedback Form:
  2. Save this code into a file called feedback_form.html

    Note that this feedback form includes HTML table code for formatting purposes.

    Copy to Clipboard
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Feedback Form</title>	
    </head>
    <body>
    <h1>Send Us Your Feedback!</h1>
    <form action="send_mail.php" method="post">
    <table>
    <tr>
    <td>Email Adress:</td>
    <td>
    <input type="text" name="email_address" value="" maxlength="100" />
    </td>
    </tr>
    <tr>
    <td>Comments:</td>
    <td>
    &lt;textarea rows="10" cols="50" name="comments"&gt;&lt;/textarea&gt;
    </td>
    </tr>
    <tr><td>&nbsp;</td>
    <td>
    <input type="submit" value="Submit" />
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>
  3. The "Send Mail" Script
  4. This script has been written in PHP. Your web server will need to support PHP to run this. If you're not sure, upload it and see if it works. Most hosting plans support PHP these days.

    Save this code as send_mail.php.

    If you change the name of any of the other files, be sure to update this script - the names of those files are included in this script.

    If you decide to add some fields to your feedback form, you will need to add them to this script too.

    Copy to Clipboard
    <?php
    // This function checks for email injection. Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
    function isInjected($str) {
    	$injections = array('(\n+)',
    	'(\r+)',
    	'(\t+)',
    	'(%0A+)',
    	'(%0D+)',
    	'(%08+)',
    	'(%09+)'
    	);
    	$inject = join('|', $injections);
    	$inject = "/$inject/i";
    	if(preg_match($inject,$str)) {
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    
    // Load form field data into variables.
    $email_address = $_REQUEST['email_address'] ;
    $comments = $_REQUEST['comments'] ;
    
    // If the user tries to access this script directly, redirect them to feedback form,
    if (!isset($_REQUEST['email_address'])) {
    header( "Location: feedback_form.html" );
    }
    
    // If the form fields are empty, redirect to the error page.
    elseif (empty($email_address) || empty($comments)) {
    header( "Location: error_message.html" );
    }
    
    // If email injection is detected, redirect to the error page.
    elseif ( isInjected($email_address) ) {
    header( "Location: error_message.html" );
    }
    
    // If we passed all previous tests, send the email!
    else {
    mail( "name@example.com", "Feedback Form Results",
      $comments, "From: $email_address" );
    header( "Location: thank_you.html" );
    }
    ?>
  5. The "Thank You" Page
  6. Save this code into a file called thank_you.html

    Copy to Clipboard
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Thank You</title>	
    </head>
    <body>
    <h1>Thanks for your feedback!</h1>
    <p>We appreciate that you took the time to send us feedback. Your feedback has been sent to the webmaster.</p>
    <p><a href="feedback_form.html">Back to feedback form</a>.</p>
    </body>
    </html>
  7. The Error Page
  8. Save this code into a file called error_message.html

    Copy to Clipboard
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Error</title>	
    </head>
    <body>
    <h1>Oops!</h1>
    <p>Please ensure you have completed both fields before submitting the form. </p>
    <p>Also ensure that there is only one email address.</p>
    <p><a href="JavaScript:history.go(-1);">Back</a></p>
    </body>
    </html>

Problems Sending Mail?

If, after installing this script, it doesn't seem to be sending emails:

Enjoy this page?

  1. Link to this page (copy/paste into your own website or blog):
  2. Link to Quackit using one of these banner ads.

Thanks for supporting Quackit!

Sponsored Link: Design CODE-FREE Websites

Check a Domain:

ZappyHost Logo
Webydo Banner Ad
Webydo Banner Ad