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


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