PHP
  Home arrow PHP arrow Page 2 - Handling Attachments in MIME Email wit...
Dev Shed Forums 
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

Handling Attachments in MIME Email with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-07-16

    Table of Contents:
  • Handling Attachments in MIME Email with PHP
  • Developing a simple MIME mailer class with PHP 4
  • Working with email attachments
  • Seeing the Mailer class in action

  • 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


    Handling Attachments in MIME Email with PHP - Developing a simple MIME mailer class with PHP 4


    (Page 2 of 4 )

    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

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

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

    }

    }


    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

    if($mailer->send()){

    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


       · This second tutorial of the series shows how to improve the initial structure of the...
     

       

    PHP ARTICLES

    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...





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