PHP
  Home arrow PHP arrow Page 2 - A MIME Mailer Class
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

A MIME Mailer Class
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    2006-01-04


    Table of Contents:
  • A MIME Mailer Class
  • You've Got Mail
  • Adding an Attachment
  • Sending it on its way

  • 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


    A MIME Mailer Class - You've Got Mail
    ( Page 2 of 4 )

    The first order of business is to initialize some variables and provide a constructor for the class.

    <?php
    class mime_mailer
    {
     var $from;
     var $to;
     var $subject;
     var $body = "";
     var $headers;
     var $files = array();
     var $header_set = false;
     var $body_set = false;

     

    Some of these are pretty self explanatory. It's easy to see who the email is from, who it goes to, the subject and the body of the message. Other variables are provided for headers, an array is for file contents and two boolean values are intended as checks to determine that we have everything before sending the mail. It might be useful in some situations to employ email address validation before sending to help ensure that nothing goes wrong. Next is the constructor method.

      function mime_mailer($to,$from,$subject,$body)
      {
       if($to != "" && $from != "" && $subject != "")
       {
        $this->to = $to;
        $this->from = $from;
        $this->subject = $subject;
        if($body != "")
        {
         $this->body = $this->set_body($body);
        }
        $this->set_headers();
       }
       else
       {
        echo("Some constructor arguments are blank: mime_mailer");
        exit();
       }
      }

    As long as all the necessary values are valid, several variables are set with the constructor. In addition the "set_body()" method is called in order to place the body of the message inside the proper mime content. The "MIME_BOUNDRY" marks individual sections of the mail. Use two line returns before and after each content block (this goes for the attachment content as well).

      function set_body($body)
      {
       $body_str = "--MIME_BOUNDRY\n";
                 $body_str .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
       $body_str .= "Content-Transfer-Encoding: quoted-printable\n";
       $body_str .= "\n\n";
       $body_str .= "$body";
       $body_str .= "\n\n";
       $this->body_set = true;
       return $body_str;
      }

    The "set_headers()" method is also called to set some necessary mail headers. Values for email "from", "Reply-To" "X-Sender" and "Return-Path" are set from the "from" variable we set earlier.

      function set_headers()
      {
       $this->headers = "From: ".$this->from."  \r\n";
                $this->headers .= "Reply-To: ".$this->from." \r\n";
            $this->headers .= "MIME-Version: 1.0\r\n";
             $this->headers .= "Content-Type: multipart/mixed;
    boundary=\"MIME_BOUNDRY\"\n";
             $this->headers .= "X-Sender: ".$this->from."\n";
              $this->headers .= "X-Mailer: PHP4\n";
             $this->headers .= "X-Priority: 3\n";
              $this->headers .= "Return-Path: <".$this->from.">\n";
            $this->headers .= "This is a multi-part message in MIME
    format.\n";
       $this->header_set = true;
      }

    Both of the above utility methods set a variable to true to indicate that the information has been set.



     
     
    >>> More PHP Articles          >>> More By Chris Root
     

       

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek