PHP
  Home arrow PHP arrow Page 4 - A MIME Mailer Class
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

A MIME Mailer Class
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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


    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Firstly, thanks for this quick tutorial.I can't see the "body" part of the...
       · Try putting the body of the message at the beginning of the body string. This may...
       · I think i've solved the problem. In function assamble(), I put a "." before "=" in ...
       · Hello,This is a very good article you have done!A question about the mail...
       · As I understand it, The string does have to be unique, It cannot match anything that...
       · I am still using PHP 4.2.1. Will this class still work, or is only for PHP 5?
       · I developed it in PHP 4.x. It should work just fine in either.
       · I've got this working the first time up, but the email I get is encoded and no...
       · I am using MS Outlook...does that matter. my attachments are encoded so it all...
       · Thank you so much for this tutorial, it really helped me alot...i just need a...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT