PHP
  Home arrow PHP arrow Page 4 - 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 - Sending it on its way
    ( Page 4 of 4 )

    The final two methods send the mail. If the proper headers and body information is set, the mail message is assembled from all the information previously gathered. The "assemble()" method is called to assemble the attachment data and put it together with the body of the message.

      function send()
      {
       if($this->header_set && $this->body_set)
       {
        $message = $this->assemble();
        $headers = $this->headers;
        $to_address = $this->to;
        $mail_subject = $this->subject;
        if(mail($to_address,$mail_subject,$message,$headers))
        {
         return true;
        }
        else
        {
         return false;
        }
       }
      }
      
      function assemble()
      {
       $str = $this->body;
       if(count($this->files) > 0)
       {
        $str = implode($this->files);
        $str .= "--MIME_BOUNDRY--\n";
       }
       return $str;
      }
    }
    ?>

    Formatting can be important to the success of the mailing. Attachments can fail to appear or be corrupted if the output is not correct. If you have any trouble while modifying or adding to this class, consult documentation about mime formatting to determine what might have gone wrong.

    The Code in Use

    Using the mime_mailer class is a simple matter. The first line you will need is to include the file in your application. You then initialize an instance of the class, add your attachments and send the mail. Use as many instances as you need. The to, from, subject and body information can come from any source in your application you need it to.

    include("class/mime_mailer.php");
    $to = "
    recipeint@someserver.com";
    $from = "
    mesilly@thisserver.com";
    $subject = "Season's Greetings";
    $body = "Here's some xmas cards just for you!";
    $mailer = new mime_mailer($to,$from,$subject,$body);
    $mailer->add_attachment("cards/xmascard.pdf");
    $mailer->add_attachment("cards/xmascard2.gif");
    $mailer->send();

    No Stamps to Lick

    There are quite a few applications where this basic functionality would be indispensable. Wrapping it all up into just a few lines can save a lot of time and effort. Experiment with the code, use it in your applications, and if you have any modifications that you think will be useful to others, please share them, and please email responsibly.



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