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

Review: sending MIME-compliant messages in plain text 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



Before I get my hands dirty and teach you how to provide the MIME mailer class with the ability to send email messages in HTML format, first I’d like to list its current signature, so you can recall how it looked originally.

Here’s the full source code that corresponds to the “Mailer” class built during the previous tutorial:

class Mailer{

var $sender;

var $recipient;

var $subject;

var $headers=array();

var $mimeTypes=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









// define some default MIME types









// 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 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;



// 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";


// add new MIME header

function addHeader($name,$value){



// 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;


// send email

function send(){






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




Does the above class ring any bells? I bet it does! As you can see, the class offers decent functionality, since it’s capable of sending MIME-compliant email messages; it supports working with file attachments as well.

To demonstrate how the above class does its business, I included a short script that puts it to work. Here it is:

// create a new instance of the 'Mailer' class

$mailer=&new Mailer('alejandro@mydomain.com,'mybuddy@yourdomain.com','Testing mailer class','Hello buddy. I am sending to you a couple of fun images.');

// add some attachments



// send MIME email


As you can see, the class is used to send a simple message with a couple of attached files to a hypothetical recipient. That was pretty simple to grasp, right?

At this point, you hopefully remember how to use the previous “Mailer” class. So, what’s the next step to take? As you may have noticed, this class is only capable of sending messages in plain text format, but as I explained in the beginning, the MIME extension allows you to send email in HTML very easily.

Thus, in the upcoming section I’m going to teach you how to add a brand new method to the aforementioned “Mailer” class that provides it with the capacity to send HTML-formatted email messages.

This topic will be discussed in detail in the next few lines, so go ahead and read them.

>>> 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: