PHP
  Home arrow PHP arrow Page 2 - Sending MIME Email with PHP
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

Sending MIME Email with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2008-07-09

    Table of Contents:
  • Sending MIME Email with PHP
  • Building a MIME mailer class
  • Completing the definition of the Mailer class
  • Sending MIME-compliant email messages

  • 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


    Sending MIME Email with PHP - Building a MIME mailer class


    (Page 2 of 4 )

    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

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

    }

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


       · This first part of the series walks you through creating the bare bones structure of...
       · Why not use Zend_Mail or ezComponents mail class or classes from OmniTI or...
       · Thank you for the comments on my PHP article. Definitively, the goal of the article...
     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - 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





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