PHP Mail Form Tutorial

PHP mail forms are very easy to create. This brief tutorial and code sample, will get you going.

It uses templates to format the body of the email that will be sent, and supports plain text and HTML formatted messages.

The first step is to create a simple HTML web page to take the form input. Here is a simple example:

<HTML>
<HEAD>
<TITLE>PHP mail form</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="mailform.php">
<P>First Name: <INPUT TYPE="TEXT" NAME=”firstname”></P>
<P>First Name: <INPUT TYPE=”TEXT” NAME=”lastname”></P>
<P>Email: <INPUT TYPE=”TEXT” NAME=”from_email”></P>
<P>Message:</P>
<TEXTAREA NAME=”message”></TEXTAREA>
<P><INPUT TYPE=”submit” NAME=”submit” VALUE=”submit”></P>
</FORM>
</BODY>
</HTML>

The second step is to create your template files. The first file is just plain text. Take the INPUT name attributes (see above in bold), and place them in your template file enclosed in square brackets. For example [firstname] wherever you would like these to appear in the body of your message.

Form Input:
[firstname]
[lastname]
[email]
[subject]
[message]

Then create an HTML formatted template in the same way. Use HTML markup to format the body of the message as shown below:

<HTML>
<BODY>
<P>[firstname]</P> 
<P>[lastname]</P> 
<P>[from_email]</P> 
<P>[message]</P>
</BODY>
</HTML> 

Next, you will need to create another HTML page to display confirmation that the form has been submitted. An example is shown below:

<HTML>
<HEAD>
<TITLE>PHP mail form: Thank you page</TITLE>
</HEAD>
<BODY>
<P>Thank you. Your message has been sent</P>
</BODY>
</HTML>

Finally, the script that makes it all work. Make sure you change the user defined values at the top of the script:

<?php
// User defined values
$text_template_file = '/path/to/text/template/file';
$html_template_file = '/path/to/html/template/file';
$thank_you_page_url = 'http://url/to/thankyou.htm';
$from_name = 'From Name';
$from_email = 'From Email';
$to_email = 'To Email';
$subject = 'Subject';
// Open template files
$text_template_file_handle = fopen($text_template_file,'r');
$html_template_file_handle = fopen($html_template_file,'r');
$text_template = fread($text_template_file, filesize($text_template_file));
$html_template = fread($html_template_file, filesize($html_template_file));
fclose($text_template_file_handle);
fclose($html_template_file_handle);
foreach ($_POST as $name => $value) {
    $pattern = '/\[' . $name . '\]/’;
    $value = stripslashes($value);
    $text_template = preg_replace_all($pattern,$value,$text_template);
    $html_template = preg_replace_all($pattern,$value,$html_template);
}
$boundary = uniqid(’np’);
$headers = “MIME-Version: 1.0\r\n”;
$headers .= “From: $from_name <$from_email>\r\n”;
$headers .= “Subject: $subject\r\n”;
$headers .= “Content-Type: multipart/alternative;boundary=” . $boundary . “\r\n”;
$message .= “\r\n\r\n–” . $boundary . “\r\n”;
$message .= “Content-type: text/plain;charset=utf-8\r\n\r\n”;
$message .= $text_template;
$message .= “\r\n\r\n–” . $boundary . “\r\n”;
$message .= “Content-type: text/html;charset=utf-8\r\n\r\n”;
$message .= $html_template;
$message .= “\r\n\r\n–” . $boundary . “–”;
mail($to_email, $subject, $message, $headers);
header(”location: $thank_you_page_url”);
?>
That’s all there is to creating a PHP mail form. With minimal modifications, you can replace the default subject line, from name, and from email address with form input.

Copyright notice. This script and tutorial was prepared by thewebhostcompany.com. You may use the script for any purpose, and post copies of this tutorial without restriction provided that this copyright notice is retained.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Webnews
  • MisterWong
  • Y!GG
  • Google Bookmarks
  • Technorati
Tags: , , , , , , , ,
If you like this post and would like to receive updates from this blog, please subscribe our feed. Subscribe via RSS

Leave a Reply