Thursday 15 March 2012
Facebook StumbleUpon Twitter Google+ Pin It

Sending Nice HTML Email with PHP


We're going to improve that output, and make the email into a nicer looking HTML-formatted email.




It's Not Much Different Than Text Email


Sending HTML Email through PHP uses the exact same mail function as text email:
mail($to, $subject, $message, $headers);

The last parameter, the headers, are optional for the function but required for sending HTML email, as this is where we are able to pass along the Content-Type declaration telling email clients to parse the email as HTML.

In fact, the headers area gives us the opportunity to do lots of important email functions. This is where we can set the From: and Reply To: settings if need be, as well as CC and BCC other recipients (Hey, a checkbox for CC'ing yourself would be a cool feature to add!). Here is the code used for the new and improved HTML-Sendin' Website Change Request Form:
$to = 'bob@example.com'; $subject = 'Website Change Reqest'; $headers = "From: " . strip_tags($_POST['req-email']) . "\r\n"; $headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n"; $headers .= "CC: susan@example.com\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

Now We Can Use HTML Tags


The message parameter (a big string we pass to the mail function with the body of our email), can now have HTML tags in it. For example:
$message = '<html><body>'; $message .= '<h1>Hello, World!</h1>'; $message .= '</body></html>';

Here is whole shebang, to create the table of data as shown above:
$message = '<html><body>'; $message .= '<img src="http://css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />'; $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">'; $message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['req-name']) . "</td></tr>"; $message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['req-email']) . "</td></tr>"; $message .= "<tr><td><strong>Type of Change:</strong> </td><td>" . strip_tags($_POST['typeOfChange']) . "</td></tr>"; $message .= "<tr><td><strong>Urgency:</strong> </td><td>" . strip_tags($_POST['urgency']) . "</td></tr>"; $message .= "<tr><td><strong>URL To Change (main):</strong> </td><td>" . $_POST['URL-main'] . "</td></tr>"; $addURLS = $_POST['addURLS']; if (($addURLS) != '') { $message .= "<tr><td><strong>URL To Change (additional):</strong> </td><td>" . strip_tags($addURLS) . "</td></tr>"; } $curText = htmlentities($_POST['curText']); if (($curText) != '') { $message .= "<tr><td><strong>CURRENT Content:</strong> </td><td>" . $curText . "</td></tr>"; } $message .= "<tr><td><strong>NEW Content:</strong> </td><td>" . htmlentities($_POST['newText']) . "</td></tr>"; $message .= "</table>"; $message .= "</body></html>";

I think that looks A LOT nicer. And since this email is (theoretically) coming directly to you, isn't it nice to know that it will be formatted to be easy on the eyes?

Concerns


Some people just absolutely hate HTML email. For one, it can be a security risk as it's possible to run JavaScript in them in some email clients which can be problematic. HTML emails also have a habit of being more easily caught in Spam filters. I think it's less of a concern here as this email is essentially being created BY you FOR you.

-By Parthiv Patel

No comments: