Home arrow PHP arrow Page 3 - Composing Messages in HTML for MIME Email with PHP

Sending email messages in HTML format - PHP

Welcome to the fourth episode of the series, "Sending MIME email with PHP." In this article, I’m going to show you how to provide the MIME mailer class with the ability to send email messages in HTML format. This will greatly extend its functionality, so don't miss this tutorial.

  1. Composing Messages in HTML for MIME Email with PHP
  2. Review: sending MIME-compliant messages in plain text format
  3. Sending email messages in HTML format
  4. Sending email in HTML format using the MIME mailer class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
July 30, 2008

print this article



As I stated in the prior section, it’d be extremely convenient to provide the “Mailer” class with the ability to send email messages in HTML format. This would exploit all the features offered by the MIME extension.

Fortunately, incorporating this functionality to the mailer class is a only a matter of adding a pair of brand new methods, which, obviously, will be responsible for dispatching email messages formatted in HTML. However, this is merely theory that needs to be put into practice. Thus, let me show you the modified signature of the “Mailer” class, including the additional methods that send email in HTML.

Here’s the full source code of the class:

class Mailer{

var $sender;

var $recipient;

var $subject;

var $headers=array();

var $mimeTypes=array();

var $html=array();

var $attachments=array();

function Mailer($sender,$recipient,$subject,$message){

// validate incoming parameters


trigger_error('Invalid value for email sender.');



trigger_error('Invalid value for email recipient.');



trigger_error('Invalid length for email subject.');



trigger_error('Invalid value for email message.');






// define some default MIME headers






$this->headers['X-Mailer']='PHP 4/5';



// define some default MIME types









// add new MIME header

function addHeader($name,$value){



// add HTML to message

function addHTML($html){


trigger_error('Invalid HTML.',E_USER_ERROR);




// add new attachment

function addAttachment($attachment){


trigger_error('Invalid attachment.',E_USER_ERROR);




// get MIME Type of attachment

function getMimeType($attachment){



trigger_error('MIME Type not found.',E_USER_ERROR);


return $mimeType;


// create message MIME headers

function buildHeaders(){

foreach($this->headers as $name=>$value){

$headers[]=$name.': '.$value;


return implode("\n",$headers)."\nThis is a multi-part message in MIME format.\n";


// create text part of the message

function buildTextPart(){

return "--MIME_BOUNDRY\nContent-Type: text/plain; charset=iso-8859-1\nContent-Transfer-Encoding: quoted-printable\n\n\n".$this->message."\n\n";


// create HTML part of the message

function buildHTMLPart(){



foreach($this->html as $html){

$htmlPart.="--MIME_BOUNDRY\nContent-Type: text/html; charset=iso-8859-1\nContent-Transfer-Encoding: 8bit\n\n\n".$html."\n\n";


return $htmlPart;



// create attachments part of the message

function buildAttachmentPart(){



foreach($this->attachments as $attachment){



$attachmentPart.="--MIME_BOUNDRY\nContent-Type: ".$this->getMimeType($attachment)."; name="".basename($attachment).""\nContent-disposition: attachment\nContent-Transfer-Encoding: base64\n\n".$fileStr."\n\n";


return $attachmentPart;



// send email

function send(){






trigger_error('Error sendind email.',E_USER_ERROR);




Now the MIME mailer class defines and implements two additional methods, called “addHTML()” and “buildHTMLPart()” respectively. The first one allows you to aggregate HTML code to an existing email message, and the last one is tasked with assembling the corresponding HTML parts that will be added later to the whole multipart message.

This assembling process is performed by the “send()” method of the class, so you shouldn’t have major trouble understanding how it works.

So far, so good. At this stage you hopefully learned how to provide the previous “Mailer” class with the ability to send email messages in HTML format. This ability has to be added to the existing capacities, meaning the class is now much more powerful.

However, there’s one thing left undone in this specific case. Yes, you guessed right! Naturally, you must want to see how the class works. Therefore, in the last section of this article, I’m going to build a final hands-on example, aimed at demonstrating the improved functionality of the MIME mailer class.

All you have to do is click on the link that appears below and keep reading.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: