Article

Advanced email in PHP

Page: 1 2 3 4 5 6 Next

HTML Email and Other Headers

So now you can send email from your PHP scripts. How exciting! Although I'm sure you're already drunk with power, would you like to know how to send HTML email too? Of course you would!

To understand HTML email, first you need to understand mail headers. Any given email message is made up of two parts: the headers and the message body. Here's how a simple message looks when your email program fetches it from your ISP:

Return-Path: <sender@elsewhere.com>  
Delivered-To: you@some.net  
Received: ...several lines like this...  
From: Sender <sender@elsewhere.com>  
To: You <you@some.net>  
Subject: A Simple Message  
Date: Mon, 11 Feb 2002 16:08:19 -0500  
Organization: Sender's Company  
X-Mailer: Microsoft Outlook, Build 10.0.2616  
 
Hi there! <tap> <tap> Is this thing on?

Everything up to the blank line makes up the headers for this message. In actual fact, most email messages will have many more header lines than this; however, to keep our focus I trimmed this example down to the bare essentials.

As you can see, every line of the headers begins with the name of the header (From:, To:, Subject:, Date:, etc.), followed by some value. Most headers are standardized, and have a specific meaning, either to your mail program or to the mail servers that were responsible for getting the message to you. Non-standard headers exist as well, and they all begin with X- (e.g. X-Mailer:, a non-standard header, often appears to indicate the program that was used to send the message).

NOTE: If a header's value needs more than one line, additional lines should begin with a space. We'll see an example of this in the next section.

As soon as your mail program gets to a blank line, it knows the headers are over and the rest of the email is the message body, which it should display. In this case, the message body is the last line above.

PHP's mail function lets you specify your own headers, which it adds to those it generates automatically to get your message where it needs to go. For example, the following call to the mail function will add an X-Mailer: header to the outgoing message, identifying PHP 4.x as the sending program:

<?php  
mail('recipient@some.net', 'Subject', 'Your message here.',  
    'X-Mailer: PHP 4.x');  
?>

This optional fourth parameter is most often used to specify a 'from' address other than the default specified in php.ini. Let's add a From: header to do just that:

<?php  
mail('recipient@some.net', 'Subject', 'Your message here.',  
    "From: sender@some.net\nX-Mailer: PHP 4.x");  
?>

Note that since headers each appear on a single line, we must separate our two headers with a new line character (\n), which means I need to use double quotes around the header string (single quoted strings don't recognize special character codes like \n).

Additional headers also let you assign names to email addresses by specifying them in the format name <email>. Here's our example again, but this time with names "The Sender" and "The Receiver" attached to the relevant addresses:

<?php  
mail('recipient@some.net', 'Subject', 'Your message here.',  
    "To: The Receiver <recipient@some.net>\n" .  
    "From: The Sender <sender@some.net>\n" .  
    "X-Mailer: PHP 4.x");  
?>

Notice that to do this, we've had to specify the To: header manually, since the first parameter of PHP's mail function doesn't support this more advanced address format. We must still list all the recipients of the message as bare email addresses in the first parameter, however.

The cc: and Bcc: headers provide the ability to send carbon copies and blind carbon copies of a message as well:

<?php  
mail('recipient@some.net, someone@some.net, metoo@some.net',  
    'Subject', 'Your message here.',  
    "To: The Receiver <recipient@some.net>\n" .  
    "From: The Sender <sender@some.net>\n" .  
    "cc: Interested <someone@some.net>\n" .  
    "Bcc: Me Too <metoo@some.net>\n" .  
    "X-Mailer: PHP 4.x");  
?>

See how the email addresses of all recipients, be they actual addressees (To), carbon copies (Cc), or blind carbon copies (Bcc) are listed in the first parameter? This isn't documented anywhere, but in my testing I've found that it's absolutely vital if you want the message to get to everyone it's supposed to, especially on Windows servers where PHP's mail function is especially sensitive.

BUG ALERT: There are two apparent bugs in the PHP mail function, which I've observed as recently as PHP 4.1.0, that you must be aware of. First, the Cc: header must be typed 'cc:' (as above) or 'CC:', that is, in either all-caps or low-caps. The mixed case version (Cc:) should work, but it doesn't. Secondly, on Windows servers, the Bcc: header does not work as it should. Bcc: recipients do get a copy of the message, but the Bcc: headers are not stripped out of the message during sending, so recipients can see the Bcc: receivers by looking at those lines in the headers.

What does all this have to do with HTML email, you ask? Well, a few special headers will cause the message body to be interpreted as HTML by email clients that support it:

<?php  
mail('recipient@some.net', 'Subject',  
    '<html><body><p>Your <i>message</i> here.</p></body></html>',  
    "To: The Receiver <recipient@some.net>\n" .  
    "From: The Sender <sender@some.net>\n" .  
    "MIME-Version: 1.0\n" .  
    "Content-type: text/html; charset=iso-8859-1");  
?>

Note both the message in HTML format as well as the two new headers: Mime-Version: and Content-type:.

The MIME-Version: header indicates that the standard extended mail format will be used (MIME stands for Multipurpose Internet Mail Extensions), which allows for messages to be sent content types other than plain text. The Content-type: header then specifies that the message with contain HTML (and sets the character set to the standard for that format).

If you liked this article, share the love:
Print-Friendly Version Suggest an Article