* 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(' ', ' ', 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 = << 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); } ?>