Home arrow PHP arrow Page 2 - A Better Way to Determine MIME Types for MIME Email with PHP

Review: sending MIME-compliant email messages with PHP 4 - PHP

Welcome to the third episode of the series entitled “Sending MIME email with PHP.” This series progressively teaches you how to construct a reusable MIME mailer class that can be easily integrated into your existing PHP applications. In this article, I’ll be modifying the implementation of the “getMimeTypes()” method, included with this mailer class, in order to make it more compact and efficient.

  1. A Better Way to Determine MIME Types for MIME Email with PHP
  2. Review: sending MIME-compliant email messages with PHP 4
  3. Modifying the signature of the “Mailer” class
  4. Putting the modified “Mailer” class to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
July 23, 2008

print this article



Since my plan for this tutorial consists primarily of improving the implementation of the “getMimeTypes()” method that was coded in the preceding article, this is the appropriate moment to list its corresponding definition, before I start modifying the method in question.

Here’s how the pertaining “Mailer” class looked previously:

class Mailer{

var $sender;

var $recipient;

var $subject;

var $headers=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';




// 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){



case 'jpg':



case 'jpeg':



case 'gif':



case 'txt':



case 'pdf':



case 'csv';



case 'html':



case 'htm':



case 'xml':




return $mimeType;


// send email

function send(){






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




It won’t hurt anybody if I also create a basic script that demonstrates how to use the above class, right? Take a look at the script below:

// 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 attachments.');

// add some attachments



// send MIME email


As you can see, the previous MIME mailer class does a decent job when it comes to sending messages in plain text format and attaching the most common types of files, like JPG and GIF images, PDF and HTML documents, and so forth.

Nonetheless, the implementation of the “getMimeType()” method still doesn’t convince me, since determining MIME types of the attached files can be done more efficiently by assigning a few simple class properties.

Therefore, in the following section, I’m going to show you how to modify the signature of the aforementioned “getMimeType()” method in order to improve the overall structure of the mailer class that was listed a few lines above.

To learn about how this will be done, please click on the link 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: