Home arrow PHP arrow Page 2 - Sending MIME Email with PHP

Building a MIME mailer class - PHP

One of the most common tasks that a PHP programmer has to tackle is the development of applications that send MIME email in one form or another. In simple terms, MIME email consists of an extension of traditional email technology and comes in handy for sending email messages in fancy HTML and handling file attachments in a wide variety of formats. This article is the first part of a five-part series.

  1. Sending MIME Email with PHP
  2. Building a MIME mailer class
  3. Completing the definition of the Mailer class
  4. Sending MIME-compliant email messages
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
July 09, 2008

print this article



Before I get my hands dirty by coding this extensible MIME mailer class with PHP, I'd like to explain the functionality that I plan to implement behind its API. The class will be comprised of a few straightforward methods that will be useful for sending MIME-compliant email messages. It will also incorporate the capacity to handle simple attachments.

Once the class has been completed, you'll be able to incorporate additional methods into it, according to your personal requirements.

Having explained how this MIME mailer class will work, I'll start by defining its principal structure and implementing its constructor. Here's the first incarnation of the class in question:

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



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

// code to send text/plain messages goes here


// create message MIME headers

function buildHeaders(){

// code to build MIME headers goes here


// add new MIME header

function addHeader($name,$value){

// code to add MIME headers goes here


// send email

function send(){

// code to send MIME email goes here



As you can see, the structure of the above "Mailer" class is quite simple to grasp and its methods are extremely intuitive. In this first step, I only implemented the corresponding constructor, but in the upcoming sections, I'll be defining the others also.

For now, pay close attention to the constructor. It accepts a few basic email-related parameters, such as the sender, the recipient, the message's subject, and finally, the text of the message itself.

The method performs a basic validation on these input arguments, and if they're considered valid, they are designated class properties. The constructor finishes its execution by defining some default MIME headers, which will be added later on when actually sending the MIME-based email message.

The rest of the methods defined by the "Mailer" class speak for themselves, since they've been named according to the task they will perform. However, they haven't been implemented concretely yet. In the upcoming section of this tutorial, I'll be doing just that. This will complete the definition of this MIME mailer class.

To see how the remaining methods of the class will be implemented, please 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: