Home arrow PHP arrow Page 3 - Migrating Class Code for a MIME Email to PHP 5

Porting the code of the previous MIME mailer class to PHP 5 - PHP

Welcome to the final installment of the series, “Sending MIME email with PHP.” This series teaches you how to build a highly modular PHP class that can be used to send MIME-compliant email messages in plain text and HTML, while letting you work with file attachments as well. In this article, I'm going to migrate the source code from the previous article to PHP 5, taking full advantage of the new and improved features in the latest version of PHP.

  1. Migrating Class Code for a MIME Email to PHP 5
  2. Sending MIME email with PHP 4
  3. Porting the code of the previous MIME mailer class to PHP 5
  4. Putting the brand new mailer class to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
August 06, 2008

print this article



Porting the source code of the previous “Mailer” class to PHP 5 is actually a very simple process, since all that it requires is using the correct syntax, and changing some statements of the class.

However, this procedure will be better understood if you have a look at the definition of the brand new MIME mailer class listed below. It has now been properly migrated to PHP 5. Here it is:

class Mailer{

private $sender;

private $recipient;

private $subject;

private $headers=array();

private $mimeTypes=array();

private $html=array();

private $attachments=array();

public function __construct($sender,$recipient,$subject,$message){

// validate incoming parameters


throw new Exception('Invalid value for email sender.');



throw new Exception('Invalid value for email recipient.');



throw new Exception('Invalid length for email subject.');



throw new Exception('Invalid value for email message.');






// define some default MIME headers









// define some default MIME types









// add new MIME header

public function addHeader($name,$value){



// add HTML to message

public function addHTML($html){


throw new Exception('Invalid HTML.');




// add new attachment

public function addAttachment($attachment){


throw new Exception('Invalid attachment.');




// get MIME Type of attachment

private function getMimeType($attachment){



throw new Exception('MIME Type not found.');


return $mimeType;


// create message MIME headers

private 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

private 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

private 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

private function buildAttachmentPart(){



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


throw new Exception('Error reading contents of 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

public function send(){






throw new Exception('Error sending email.');




The PHP 5 version of the MIME mailer class doesn’t differ much from its counterpart coded in PHP 4. Of course, I used the syntax supported by PHP 5 in order to implement the different methods of the class. I also utilized a few simple exceptions to handle basically all of the errors that might occur during its execution. But other than the mentioned modifications, the signature of the class remains nearly the same.

Well, I showed you how to recode the pertaining “Mailer” class with PHP 5, a process that hopefully didn’t cause you major headaches. Therefore, the only thing left to do is develop an illustrational example that shows how to use the class in question.

As you might have guessed, this hands-on example will be created in the final section of this article, so jump forward and read the next few lines. I’ll be there waiting for you.

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