PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - Sending MIME-compliant email messages
    ( Page 4 of 4 )

    I will now set up a simple hands-on example that illustrates how the "Mailer" class can be used to send MIME email messages. Thus, below I included the complete definition of the class, along with a short script that will send a trivial message by using the class in question.

    Here's the corresponding code sample:


    // include MIME mailer class


    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;

    }

    }


    // create a new instance of the 'Mailer' class

    $mailer=&new Mailer('alejandro@mydomain.com,'mybuddy@yourdomain.com','Testing mailer class','Hello buddy. How are you?');

    // send MIME email message

    if($mailer->send()){

    echo 'Message was sent successfully.';

    }


    As you can see, sending MIME-compliant messages with this basic mailer class is a no-brainer process that can be tackled with minor hassles. In the above example, I first created a new instance of the class with the appropriate message's arguments (i.e. the respective sender, recipient, subject, and text) and then sent the message via the "send()" method. It's that simple.

    Of course, there's plenty of room to improve the functionality of the previous mailer class, but all of these possible modifications will be implemented throughout the course of the upcoming tutorials in the series. In the meantime, play around with the class and use it to familiarize yourself with sending MIME-compliant email messages with PHP.

    Final thoughts

    In this first chapter of the series, I walked you through building a basic mailer class that has the capacity to send text-based messages that fit the specification of MIME email. As you saw earlier, the class is very simple to code and use. And due to its flexible structure, it can be extended with minor efforts.

    In the next article, I'll be teaching you how to improve the functionality of the class in order to provide it with the capacity to send different types of attachments. Don't miss the next article!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek