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

Completing the definition of the 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As you saw in the previous section, the only method of the "Mailer" class that was concretely implemented was its corresponding constructor. However, I'll show you the implementations of the other methods of this mailer class, so that you can understand these methods more easily.

Now, pay attention to the following code sample, which completes the definition of the "Mailer" class: Here it is:


class Mailer{

var $sender;

var $recipient;

var $subject;

var $headers=array();

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

// validate incoming parameters

if(!preg_match("/^.+@.+$/",$sender)){

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

}

if(!preg_match("/^.+@.+$/",$recipient)){

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

}

if(!$subject||strlen($subject)>255){

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

}

if(!$message){

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

}

$this->sender=$sender;

$this->recipient=$recipient;

$this->subject=$subject;

$this->message=$message;

// define some default MIME headers

$this->headers['MIME-Version']='1.0';

$this->headers['Content-Type']='multipart/mixed;boundary="MIME_BOUNDRY"';

$this->headers['From']='<'.$this->sender.'>';

$this->headers['Return-Path']='<'.$this->sender.'>';

$this->headers['Reply-To']=$this->sender;

$this->headers['X-Mailer']='PHP 4/5';

$this->headers['X-Sender']=$this->sender;

$this->headers['X-Priority']='3';

}

// create text part of the message

function buildTextPart(){

return "--MIME_BOUNDRYnContent-Type: text/plain; charset=iso-8859-1nContent-Transfer-Encoding: quoted-printablennn".$this->message."nn";

}

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

$this->headers[$name]=$value;

}

// send email

function send(){

$to=$this->recipient;

$subject=$this->subject;

$headers=$this->buildHeaders();

$message=$this->buildTextPart()."--MIME_BOUNDRY--n";

if(!mail($to,$subject,$message,$headers)){

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

}

return true;

}

}


At this moment, and after studying the recently-implemented methods of the above mailer class, you'll have to agree with me that things are definitely more interesting! As you can see, the class now has been provided with the capacity to incorporate any number of additional MIME headers via the "addHeader()" method to a particular email message. The "buildHeaders()" and "send()" methods have been concretely defined, turning the mailer class into a completely functional piece of code.

You should also notice how the respective header and body parts of the MIME-based message are internally assembled by the "send()" method, before sending the message in question to the specified recipient. For this specific case, I created a basic MIME boundary to delimit the different parts of the message, but you can use a different one as long as it fits the specifications of the MIME extension. Simple and understandable, isn't it?

Now everything looks good, since you hopefully learned how to build an expansible mailer class with PHP 4, which, for the moment, permits you to send MIME-compliant text messages with minor efforts.

However, I'm pretty sure that you're anxious to see how the previous "Mailer" class can be used in the context of a hands-on example. Therefore, in the last section of this tutorial, I'll build a short script that will demonstrate the functionality of the class.

To see how this practical example will be developed, please 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
   

PHP ARTICLES

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