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  
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 - 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


       · 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 3 hosted by Hostway
    Stay green...Green IT