Form Handling with PHP
Lesson 6 - HTML E-mail
Index Lesson << Prev 1 2 3 4 5 6 7 Next >>
Personally, I dislike HTML e-mail, but a lot of people want to send it, so we'll talk about how it's done. The only real requirement to send an e-mail message as HTML is that you send a header that tells the mail client that it is HTML and specifies the character set used and you need to tell it the MIME version. The headers would look like this:
MIME-Version: 1.0 Content-type: text/html; charset=iso-8859-1
After sending those headers, the e-mail client, if it understands HTML, will interpret the message as an HTML message and render it accordingly. So now let's look at a simple script that sends an HTML e-mail.
<?php $to="Recipient Name <somebody@example.com>"; $from="Sender Name <jdoe@example.com>"; $subject="My first HTML E-mail"; $message="<h1>HTML E-mail</h1> <p>This is an <b>HTML</b> e-mail.</p>"; $headers = "MIME-Version: 1.0\r\n"; $headers.= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers.= "From: $from\r\n"; if (mail($to, $subject, $message, $headers)) echo "Message Sent!"; else echo "Failed to send message."; ?>
Now that we've solved the mystery of HTML e-mail. There is one more thing to think about. In these days of viruses being sent by e-mail, some people have switched to e-mail clients that don't recognize HTML. What happens when one of those people receives your carefully built, beautiful HTML e-mail message? It gets pretty ugly. Before you say, "Tough stuff!" realize that there is a way to satisfy both types of e-mail clients. You can send a multi-part message with one part plain text and the other part containing your HTML. Clients that understand MIME, but not HTML will render the plain text version and ignore the HTML. Clients that understand HTML will render the HTML. Here is a script that does that:
<?php $to="Recipient Name <somebody@example.com>"; $from="Sender Name <jdoe@example.com>"; $subject="My first HTML E-mail"; $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; $headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/alternative;\n" . " boundary=\"{$mime_boundary}\"\r\n"; $headers.= "From: $from\r\n"; $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . "HTML E-mail\n\nThis is the text portion of an HTML e-mail\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . "<h1>HTML E-mail</h1>" . "<p>This is an <b>HTML</b> e-mail.</p>"; if (mail($to, $subject, $message, $headers)) echo "Message Sent!"; else echo "Failed to send message."; ?>
The point to take note of in this example is that our content type is set to "multipart/alternative". Then we place boundary markers between the two versions of the message and set a content type for each part. What this means is that we're sending two versions of the message. The e-mail client can choose which of these to display. If the e-mail client understands HTML, it will display it. If it doesn't understand HTML, it will display the plain text version.