Home arrow PHP arrow Page 3 - A MIME Mailer Class

Adding an Attachment - PHP

Setting up a web application to send plain text email is easy. What if you need the email to handle other content, such as images or special document formats? This article explains how to design a PHP class for sending email with attachments.

TABLE OF CONTENTS:
  1. A MIME Mailer Class
  2. You've Got Mail
  3. Adding an Attachment
  4. Sending it on its way
By: Chris Root
Rating: starstarstarstarstar / 20
January 04, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

  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
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: