Home arrow PHP arrow Page 2 - Handling Attachments in MIME Email with PHP

Developing a simple MIME mailer class with PHP 4 - PHP

In this second tutorial of the series, Iím going to teach you how to improve the initial structure of the MIME mailer class developed in the first article by giving it the ability to work directly with attachments. Sounds fairly interesting, right? Then donít waste any more time in preliminaries and begin reading this article now!

  1. Handling Attachments in MIME Email with PHP
  2. Developing a simple MIME mailer class with PHP 4
  3. Working with email attachments
  4. Seeing the Mailer class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
July 16, 2008

print this article



As I said in the beginning, I started this series of articles by explaining how to build a basic MIME mailer class with PHP 4. This was only capable of sending messages in plain text. So let me quickly remind you of the definition of this introductory class. It looked like this:

class Mailer{

var $sender;

var $recipient;

var $subject;

var $headers=array();

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

// validate incoming parameters


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



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



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



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






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



// send email

function send(){






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


return true;



As shown above, the previous ďMailerĒ class comes in handy for sending MIME-complaint messages in just plain text format, meaning that its functionality may be limited in certain situations. Despite this issue, below coded a short, hands-on example that shows how to use it in a simple way:

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

$mailer=&new Mailer('alejandro@mydomain.com,'mybuddy@yourdomain.com','Testing mailer class','Hello buddy. Is everything doing fine over there?');

// send MIME email message


echo 'Message was sent successfully.';


That wasnít rocket science. I have to admit that the mailer class in its current incarnation is pretty useless, particularly when used with PHP applications that require you to work with attachments. What comes next, though? Well, in accordance with the concepts that I deployed in the introduction, in the section to come Iím going to add a couple of additional methods to the pertaining ďMailerĒ class, so it can handle several types of attachments with relative ease.

To see how this will be done, please jump forward and read the next few lines. Itís only one click way.

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