Sending email in PHP

21:21:00 0 Comments

The mail() function in PHP is prototyped as follows:

bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])

This means that in its simplest form, we need only specify the recipient, the subject, and the message. We can also specify extra parameters (for example, the from address).

You’ll notice from the return type of the function that a Boolean is returned upon completion. If PHPsuccessfully passed the email to the SMTP server then true is returned. If an error occurred then false is returned. Please note that even if true is returned the mail may not be sent! (For example if the SMTP is incorrectly configured. In this case you should consult your SMTP server logs to see what went wrong).

To send the email from the previous section – with error checking – we use the following code:

$to = “”;
$from = “”;
$subject = “This is a test email”;
$message = “Dear John, This is a fake email, I hope you enjoy it. From Jane.”;
$headers = “From: $from ”;
$success = mail($to, $subject, $message, $headers);
if ($success) {
echo “The email to $to from $from was successfully sent”;
} else {
echo “An error occurred when sending the email to $to from $from”;

It is also possible to send an email to multiple recipients with a single call to the mail() function. Here is an example. All you need to do is modify the recipient variable as follows:

$to = ",";

Sending HTML emails in PHP

There is no real trick to sending HTML formatted email. After all, it’s only text – just as normal emailmessages are. The difference is in how email clients interpret that text.

The main step involved is in telling the email client that the email is HTML and not plain text. To do this we simply add the Content-type header. HTML uses the text/html mime type. Plain text emails use text/plain.

Here is an example:

$to = "";
$from = "";
$subject = "This is a test email";
$message =<<<EOF
<strong>Hello World!</strong>
$headers = "From: $from\r\n";
$headers .= "(anti-spam-content-type:) text/html\r\n";
$success = mail($to, $subject, $message, $headers);
if ($success)
echo "The email to $to from $from was successfully sent";
echo "An error occurred when sending the email to $to from $from";

Simplifying life with PEAR

In the previous two chapters I showed how to send plaintext and HTML emails. The main limitation with doing HTML as I showed in the previous chapter is that in an email client that doesn’t support HTML email, the user sees a bunch of HTML tags.

The solution to this is to send a multi-part email. That is, within the one message, send a plaintext version and a HTML version. Unfortunately this is complicated with the mail() function as-is.

In order to get around this, we use the Mail_Mime class from the Pear repository. This also allows us to send attachments with our email.

In the following example, we use the Mail_Mime class to send an email with both plain text and HTML, as well as an attachment. Comments are included in the code to explain what is happening.

require_once('Mail.php'); // These two files are part of Pear,
require_once('Mail/Mime.php'); // and are required for the Mail_Mime class
$to = "";
// the email address of the person receiving the email
$from = "";
// the email address of the sender
$subject = "This is a test email";
// the subject of the email
$attachment = "/path/to/someFile.pdf";
// the path to the file we are attaching to the email
// Next we must build an array of email headers for the email.
// This is structured slightly differently to the mail() function.
// The array key is the header name. Remember that subject
// is a header too!
$headers = array('From' => $from,
'Subject' => $subject);
// Here we create the plaintext version of the email to be sent
$textMessage = "Dear John,\n\nThis is a fake email, I hope you enjoy it.\n\nFrom Jane.";
// Here we create the HTML version of the email to be send, using
// the heredoc syntax
$htmlMessage = <<<EOF
<strong>Hello World!</strong>
$mime = new Mail_Mime();
// create a new instance of the Mail_Mime class
// set our plaintext message
// set our HTML message
// attach the file to the email
$body = $mime->get();
// This builds the email message and returns it to $body.
// This should only be called after all the text, html and
// attachments are set.
$hdrs = $mime->headers($headers);
// This builds the corresponding headers for the plaintext,
// HTML and any other required headers. It also includes
// the headers we created earlier by passing them as an argument.
$mail = &Mail::factory('mail');
// Creates an instance of the mail backend that we can use
// to send the email.
$mail->send($to, $hdrs, $body);
// Send our email, according to the address in $to, the email
// headers in $hdrs, and the message body in $body.