<?php
/*
 * PHP GenerateMIMEMail Function
 * Version 1.0 - April 2003
 * Version 2.0 - August 2005 - added attachments, boundary tweaking
 * (c) 2003,2005 Paul Gregg <pgregg@pgregg.com>
 * http://www.pgregg.com
 *
 * Function: Generate a valid MIME email with html and plain text elements
 *           and attach any files as needed
 *
 * This file should be included by other php scripts
 * 
 * Open Source Code:   If you use this code on your site for public
 * access (i.e. on the Internet) then you must attribute the author and
 * source web site: http://www.pgregg.com/projects/
 * You must also make this original source code available for download
 * unmodified or provide a link to the source.  Additionally you must provide
 * the source to any modified or translated versions or derivatives.
 *
 */

// Function to generate a valid MIME email with html and plain text elements
// and attach any files as needed

function GenerateMIMEMail($html$plain=NULL$attachments=array(), $boundarybase=NULL) {

  if (
is_null($plain)) // crude, but effective if you layout the html nicely
    
$plain str_replace('&nbsp;'' 'strip_tags($html));
    
  if (
is_null($boundarybase))
    
$boundarybase '==pgregg_Multipart_Boundary';
  
  
$mime_boundary1 $boundarybase '1_' md5(time()); 
  
$mime_boundary2 $boundarybase '2_' md5(time()+1); 


  
// Lets generate the email headers we are going to use
  
$headers '';

  
// Add MIME headers
  
$headers .= "MIME-Version: 1.0\r\n";
  
$headers .= "Content-type: multipart/mixed;\n boundary=\""$mime_boundary1 '";' "\r\n";

    
  
// Now generate the body of the email
  
$body = <<<EOMSG
This is a MIME message. If you are reading this text, you
might want to consider changing to a mail reader that
understands how to properly display MIME multipart messages.

--
$mime_boundary1
Content-Type: multipart/alternative;
  boundary="
$mime_boundary2"

--
$mime_boundary2
Content-Type: text/plain
Content-Transfer-Encoding: 7bit


EOMSG
  . 
$plain . <<<EOMSG


--
$mime_boundary2
Content-Type: text/html
Content-Transfer-Encoding: 7bit


EOMSG
  . 
$html . <<<EOMSG

--
$mime_boundary2--


EOMSG;


  
// Lets attach any files we were given 
  
if (is_array($attachments) && count($attachments) > 0) {
    foreach (
$attachments as $tmpfile) {
      
$tmpfilename basename($tmpfile);
      if (
function_exists('mime_content_type'))
        
$tmp_mimetype mime_content_type($tmpfile);
      else
        
$tmp_mimetype 'application/octet-stream';
      if (
is_readable($tmpfile)) {
        
$body .= sprintf("--%s\nContent-Type: %s\n name=%s\nContent-Transfer-Encoding: base64\nContent-Disposition: attachment;\n filename=\"%s\"\n\n",
            
$mime_boundary1$tmp_mimetype$tmpfilename$tmpfilename );
        
$body .= chunk_split(base64_encode(file_get_contents($tmpfile))) . "\n"
      }
    }
  }
  
  
// Terminate the outer MIME boundary
  
$body .= '--' $mime_boundary1 "--\n";

  
// We now have our headers, body and attachments correctly encoded

  
return array($headers$body);
}
?>