PHP
  Home arrow PHP arrow Page 3 - 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? 
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 - Adding an Attachment
    ( Page 3 of 4 )

      function add_attachment($file_path)
      {
       if($file_path != "")
       {
        $this->files[] = $this->set_file_section($file_path);
       }
      }

              function set_file_section($file_path)
       {
       if($str = file_get_contents($file_path))
       {
        $str = file_get_contents($file_path);
        $bname = basename($file_path);
             $str = chunk_split(base64_encode($str));
        $section .= "--MIME_BOUNDRY\n";
        $section .= "Content-Type: ".$this->determine_mime($file_path)."; name=\"$bname\"\n";
        $section .= "Content-disposition: attachment\n";
        $section .= "Content-Transfer-Encoding: base64";
        $section .= "\n\n";
        $section .= "$str";
        $section .= "\n\n";
        return $section;
       }
       else
       {
        $problem = "";
        if(!file_exists($file_path))
        {
         $problem = "File could not be found";
        }
        echo("<strong>unable to load specified file $file_path in set_file_section method</strong> <br>$problem");
        exit();
       }
       }

    The "add_attachment()" method is pretty simple. It accepts the string path to a file to attach as an argument. It calls the "set_file_section()" method which loads the file, encodes it and places it in the proper mime content. In order to insert the proper mime type for the file into the string, we need to send the file name to a function that determines the mime type based on the file extension. There are a lot of these functions available on various sites and more complex classes that use the mime types file that is used by Apache.

    For this class I have written a basic mime type function that supports the formats I most commonly encounter in my work. If you have additional ones that you wish to support, adding mime types to this function is as simple as adding to the switch construct.

       function determine_mime($name)
       {
        $str = basename($name);
       $name_arr = explode(".",$str);
       $len = count($name_arr) - 1;
       $name_arr[$len] = strtolower($name_arr[$len]);
       switch($name_arr[$len])
       {
        case "jpg":
         $type = "image/jpeg";
         break;
        case "jpeg":
         $type = "image/jpeg";
         break;
        case "gif":
         $type = "image/gif";
         break;
        case "txt":
         $type = "text/plain";
         break;
        case "pdf":
         $type = "application/pdf";
         break;
        case "csv";
         $type = "text/csv";
         break;
        case "html":
         $type = "text/html";
         break;
        case "htm":
         $type = "text/html";
         break;
        case "xml":
         $type = "text/xml";
         break;
       }
       return $type;
       }



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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