[ Team LiB ] Previous Section Next Section

24.5 HTML Email

An HTML email is a message presented in HTML instead of plain text. This allows control of colors and fonts for decoration, and it even allows the inclusion of images in a message. It's easy to send HTML email from your client, but it's not as easy from a PHP script. The key is to understand how to form MIME messages.

But first, you should decide whether the advantages of sending an HTML email fits your needs and whether you can do so ethically. The first reason to consider HTML email is the greater control over presentation you'll gain. Plaintext is fine when an email is a simple narrative, but if you want to present a table, you will have difficulty. Most GUI email clients use a variable-width font to display messages. As a result, it's impossible to align text in columns using tabs or spaces.

Consider an order summary sent to a customer. Information such as items purchased, prices, and other data need to be presented in an email. Although a table seems like a natural way to organize the information, it's only possible with HTML.

HTML offers the value of making a better presentation. It lets you control fonts, colors, and general layout with tables, an important feature to many people. Those in the advertising industry surely see the value in the increased control this type of email provides.

There can also be an issue of usability. Image tags work in most email clients, so you can even put graphics in your messages. Because you can put images inline, you can also take advantage of the client's need to retrieve those images when the message is opened. Although there is a standard that includes all necessary images in one large email (called MHTML), few clients support it. So, your images must be hosted on a Web server.

You can measure how many times your message was viewed by looking at your Web server logs. But you can go further than that. It's easy to put the URL to a PHP script in for the image source attribute. The script can return an image, but before it does, it can capture some information generated by the request—such as the name of the client, the IP address of the requester, or even some extra information you've put in the URL as GET variables.

By now, you're probably detecting the distinct smell of spam. These are the tricks of those annoying people who send out advertisements for anything from cable television descramblers to pornography sites. These tricks are also used by sites you've requested to notify you of sales or new products. There are a few issues to consider before you decide to send HTML emails. The most important one is privacy. With HTML emails, it's very easy to track who opened the email you sent, when they opened it, and maybe even more.

Imagine putting a bit of code like this into an HTML email:

<img src="http://www.spam.com/saveinfo.php?sentTo=you@yourhost.com"
width="1" height="1" border="0">

When the recipient opens the email, the email client fetches the image, but the email address is sent along with the request. Now the operator knows that out of the thousands of people he spammed, this particular person opened it. In most cases, this is rude. You can even gather information about someone without disclosing the practice. In fact, if you make it a tiny 1x1 image, they may not even have the clue of seeing an image in the email.

You should also consider people who have slow connections to the Internet or do not have the ability to view HTML email. If you send an HTML email to people with limited or no ability to view HTML email, they may end up just receiving your raw HTML code.

Sending an email with a lot of large images is a problem for people who use modems to connect to the Internet, regardless of which software they use. This is the same problem you face when creating a Web page, except people aren't used to waiting five minutes for their email to display. They may not be online when they open their email. As a result, the image may be displayed as broken or it may cause their computer to attempt to reconnect to the Internet. Either scenario can be annoying.

There are situations where it is appropriate to send an HTML email, and other times when it's definitely not. Sending unsolicited email, especially a duplicate message to a large group, is definitely not nice in most cases. Gathering information about people without their consent isn't good either. If someone gives you permission, HTML email can be a tool to improve the experience of reading a message.

Your message must use MIME headers in order to use HTML. Sending messages with attachments is similar to sending attachments. Instead of sending a multipart/mixed message, send a multipart/alternative message. This alerts the client that several versions of the same message are included, and the client should pick the best version. The simplest case is to include a plain text version and an HTML version. If the client understands HTML, that version should be presented instead of the plaintext version.

Listing 24.7 demonstrates a simple HTML email. I used base64 encoding instead of quoted-printable because Microsoft's email clients appear to have trouble with quoted-printable messages.

Listing 24.7 HTML email
<?php
    //add From: header
    $headers = "From: webserver@localhost\r\n";

    //specify MIME version 1.0
    $headers .= "MIME-Version: 1.0\r\n";

    //unique boundary
    $boundary = uniqid("COREPHP");

    //tell e-mail client this e-mail contains
    //alternate versions
    $headers .= "Content-Type: multipart/alternative" .
        "; boundary = $boundary\r\n\r\n";

    //message to people with clients who don't
    //understand MIME
    $headers .= "This is a MIME encoded message.\r\n\r\n";

    //plain text version of message
    $headers .= "--$boundary\r\n" .
        "Content-Type: text/plain; charset=UTF-7\r\n" .
        "Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode(
        "This is the plain text version!"));

    //HTML version of message
    $headers .= "--$boundary\r\n" .
        "Content-Type: text/html; charset=UTF-7\r\n" .
        "Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode(
        "This the <b>HTML</b> version!"));

    //send message
    mail("root@localhost", "An HTML Message", "", $headers);

    print("HTML Email sent!");
?>
    [ Team LiB ] Previous Section Next Section