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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 6
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT