Class 8 – Sending Email in PHP

July 21st, 2009 § 0

PHP’s built-in mail() function allows you to send emails from PHP code.  This can be useful when, for example, you want to include a “Contact Me” form on your website, and you want an email to be sent to you whenever someone fills out that form.

The mail() function has three required parameters: the “To” email address, the subject of the message, and the content of the message.  There is also a fourth additional header that can contain one or more additional information, such as who the message is “From”, any “CC” or “Bcc” email address, and some other obscure things.

Click here to view this example code in action.

XHTML page with form

In this example, we’ll look at the most common scenario: how to use an XHTML form that a user can fill in to send a message with all four parameters, “To”, “From”, subject, and message content.

The first thing to do would be set up an XHTML document called “index.html” with a form that users can fill in to send the email.  A typical “Contact us” XHTML document might look like this:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title>Contact us</title>
 <link rel="stylesheet" type="text/css" href="styles/main.css" />
 </head>
 <body>
 <div id="container">

 <h1>Contact us</h1>
 <p>Fill out this form, and click submit, and we'll receive an email!</p>

 <form action="process_email.php" method="POST">
 <label for="name">Your Name:</label>
 <input type="text" id="name" name="name"/>
 <br />
 <label for="email">Your Email:</label>
 <input type="text" id="email" name="email"/>
 <br />
 <label for="subject">Subject:</label>
 <input type="text" id="subject" name="subject"/>
 <br />
 <label for="message">Message:</label>
 <textarea id="message" name="message"></textarea>
 <br />
 <input type="submit" value="Send email!"/>
 <br />
 </form>

 </div><!-- end container -->
 </body>
</html>

As you can probably notice from the attributes of the <form> tag, this form will be using the HTTP POST method of passing data to the server.  And when a user clicks submit, the data the user entered gets sent, along with the POST request, to the file called “process_email.php”.

PHP script to process the data entered in the XHTML form

The PHP script that receives this data, called “process_email.php“, looks like the following:

<?php

$to = "yourname@yourdomain.com"; //the email address where you want to receive these emails//get the data the user entered in the form

$name = $_REQUEST['name']; //get the data the user entered in the name field and store it in a variable
$email = $_REQUEST['email']; //get the data the user entered in the email field and store it in a variable
$subject = $_REQUEST['subject']; //get the data the user entered in the subject field and store it in a variable
$message = $_REQUEST['message']; //get the data the user entered in the message field and store it in a variable

//put together the email headers, which contain the name, email, and other info about the mail program we're using
$phpVersion = phpversion(); //this calls a built-in PHP function that returns the current version of PHP installed

$headers = <<<END
From: {$name} <{$email}>
Reply-To: {$email}
X-Mailer: PHP/{$phpVersion}
END;

//use php's built-in mail() function to send this e-mail
mail($to, $subject, $message, $headers);

//redirect to success page
header("Location: success.php?to={$to}");

?>

Let’s analyze this file. You can see that the first thing this code does is hard-code the “To” address.  Since these emails are supposed to all be going to us, we don’t want to ask the users of our site to enter in the email address to which they want to send these emails.  We don’t want to give them the ability to send emails to anyone but us!  So we hard-code that address.

The other variables we set up are all just storing the data the user entered on the XHTML page in the form.  We access that data by reading it from PHP’s built-in $_REQUEST array, which has all the information on all data that was submitted along with any type of HTTP request.

Then we assemble a set of extra headers that indicate who the email is “From”, what address the receiving email program should use as the “Reply-to” address, and the name of the program that was used to send this email (in this case, we’re simply substituting the version of PHP).

Finally, we call the mail() function of PHP, along with the parameters necessary to actually send the email.  Once that is finished, we redirect the user’s browser to the page, success.php, which has a user-friendly confirmation message.  We redirect the browser by sending a “Location” HTTP header to the browser in response to the browser’s request for this page.  This tells the browser that it should redirect to success.php, rather than stay on the current page.

The header we send to the browser in response to its request for this page looks like this:

header("Location: success.php?to={$to}");

You may recognize that we are attaching some data onto the query string of the file that we are instructing the browser to request instead of the current page.  So when the browser requests that other file, it will pass some data back to that other server script along with the new request.  To be exact, the browser will request the following file from the server with the following query string:

sucess.php?to=yourname@yourdomain.com

In this case, the data being passed is the “To” address indicating to where the email was sent.

Confirmation screen

The confirmation page, called “success.php“, receives that data in the $_REQUEST array, and outputs a friendly message that acknowledges the successful sending of the email to that address that was passed along with the request.

<?php echo "<?" ?>xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title>Success! Email Sent</title>
 <link rel="stylesheet" type="text/css" href="styles/main.css" />
 </head>

 <body>
 <div id="container">

 <h1>Success! Email Sent</h1>
 <p>Your email has been successfully sent to <?php echo $_REQUEST['to'] ?></p>
 <p><a href="index.html">Click here to send another</a></p>

 </div><!-- end container -->
 </body>
</html>

Where Am I?

You are currently browsing the email category at Web Development Intensive.