PHP
  Home arrow PHP arrow Page 2 - Migrating Class Code for a MIME Email ...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Migrating Class Code for a MIME Email to PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-08-06

    Table of Contents:
  • Migrating Class Code for a MIME Email to PHP 5
  • Sending MIME email with PHP 4
  • Porting the code of the previous MIME mailer class to PHP 5
  • Putting the brand new mailer class to work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Migrating Class Code for a MIME Email to PHP 5 - Sending MIME email with PHP 4


    (Page 2 of 4 )

    Before I start showing you how to migrate the source code of the MIME mailer class that I built in the previous article to PHP 5 , it’d be pretty useful to list its entire definition. This way you’ll be able to compare this PHP-4 based version of the class with the one that will be coded later on using PHP 5.

    Having said that, here’s how the pertinent “Mailer” class looks before making the aforementioned code migration:


    class Mailer{

    var $sender;

    var $recipient;

    var $subject;

    var $headers=array();

    var $mimeTypes=array();

    var $html=array();

    var $attachments=array();

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

    // validate incoming parameters

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

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

    }

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

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

    }

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

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

    }

    if(!$message){

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

    }

    $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';

    // define some default MIME types

    $this->mimeTypes['image/jpeg']='jpg';

    $this->mimeTypes['image/jpg']='jpg';

    $this->mimeTypes['image/gif']='gif';

    $this->mimeTypes['text/plain']='txt';

    $this->mimeTypes['text/html']='htm';

    $this->mimeTypes['text/xml']='xml';

    $this->mimeTypes['application/pdf']='pdf';

    }

    // add new MIME header

    function addHeader($name,$value){

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

    }

    // add HTML to message

    function addHTML($html){

    if(!$html){

    trigger_error('Invalid HTML.',E_USER_ERROR);

    }

    $this->html[]=$html;

    }

    // add new attachment

    function addAttachment($attachment){

    if(!file_exists($attachment)){

    trigger_error('Invalid attachment.',E_USER_ERROR);

    }

    $this->attachments[]=$attachment;

    }

    // get MIME Type of attachment

    function getMimeType($attachment){

    $attachment=explode('.',basename($attachment));

    if(!$mimeType=array_search(strtolower($attachment[count($attachment)-
    1]),$this->mimeTypes)){

    trigger_error('MIME Type not found.',E_USER_ERROR);

    }

    return $mimeType;

    }

    // 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";

    }

    // 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 HTML part of the message

    function buildHTMLPart(){

    if(count($this->html)>0){

    $htmlPart='';

    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

    function buildAttachmentPart(){

    if(count($this->attachments)>0){

    $attachmentPart='';

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

    $fileStr=file_get_contents($attachment);

    $fileStr=chunk_split(base64_encode($fileStr));

    $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

    function send(){

    $to=$this->recipient;

    $subject=$this->subject;

    $headers=$this->buildHeaders();

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

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

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

    }

    }

    }


    That’s was pretty illustrative, right? Having listed the full source code that corresponds to the MIME mailer class that was constructed in the last tutorial, I should assume that you’re very familiar with using it to send messages in plain text and HTML format, while working with file attachments also.

    So what’s the next step? Well, as I said in the introduction, it’d be pretty convenient to port the entire source code of the above mailer class to PHP 5. This would permit us to take advantage of its improved object model.

    Therefore, in the following section, I’m going to show you how to perform the aforementioned code migration. To learn the details of this process, please click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · In this last installment of the series, the code of the MIME mailer class built in...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT