HTML Email

calendar November 11, 2009

This code describes how to send email using a multipart construction, where one part contains a plain-text version of the email and the other part is HTML. If you customers have HTML email turned off they will still get a nice email even if they don’t get all of the HTML markup.

Code:

 <?php
 $to_nickname = "Jimmy";
 $to = "jimmy@email.com";
 $from_nickname = "Jack";
 $from = "jack@email.com";
 $subject = "HTML Email";
 $random_hash = "2343qwe";
 ob_start();
 ?>
 To: <?=$to_nickname?> < <?=$to?> >
 From: <?=$from_nickname?> < <?=$from?> >
 MIME-Version: 1.0
 Content-Type: multipart/alternative;boundary="==Multipart_Boundary_<?=$random_hash?>"
 <?php
 $headers = ob_get_clean();
 ob_start();
 ?>
This is a multi-part message in MIME format.
--==Multipart_boundary_<?php print "$random_hash\n"; ?>
 Content-Type: text/plain; charset="iso-8859-1"
 Content-Transfer-Encoding: 7bit
This is the text of the message in a simple tetx format.
--==Multipart_boundary_<?=$random_hash?>
 Content-Type: text/html; charset="iso-8859-1"
 Content-Transfer-Encoding: 7bit
<html>
 <body>
 <p>Here is something with HTML formatting. That can include all of the usual:</p>
 <ul>
 <li>Bulleted lists</li>
 <li>Tables</li>
 <li>Images (if you include them as attachments or external links)</li>
 <li>Character formatting</li>
 <li>etc</li>
 </ul>
 </body>
 </html>
--==Multipart_boundary_<?=$random_hash?>
 <?php
 $message = ob_get_clean();
 $mail = @mail($to, $subject, $message, $headers);
 $ok = $mail ? "Mail sent\n" : "Mail failed\n";
 print $ok;
 ?>

The only hitch comes from the images or other file-based resources. You have two alternatives with images. The first is to reference an image on a remote server. There areĀ  a couple of problems with this approach. First it doesn’t work for offline mail reading. Second spammers use this mechanism to see whether you have opened their email.

The second approach is to embed the image as an attachment in another part of the multipart message. This will work, but the email message themselves will be larger because of the base-64-encoded images.

admin

One Response to “HTML Email”

  1. MICHAEL Says:


    CheapTabletsOnline.Com. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. Low price drugs. Buy drugs online

    Buy:Arimidex.Zyban.Prednisolone.Actos.Synthroid.Lumigan.Petcam (Metacam) Oral Suspension.Zovirax.100% Pure Okinawan Coral Calcium.Prevacid.Mega Hoodia.Nexium.Valtrex.Accutane.Retin-A.Human Growth Hormone….

Leave a Reply

You must be logged in to post a comment.