Sending HTML E-Mails with file attachments with one easy function!

There is a mobile optimized version of this page, view AMP Version.

Quite a few years ago I was having difficulty with sending HTML emails, on some clients they worked fine but for others they would come through all garbled. so I decided to research a correct approach, which I implemented into a function.

A little while later I wanted to also send file attachments in the emails, so again I did some research (and some trial and error) and finally got it working! I expanded my mail function with the new functionality and have been using it in projects ever since.

Today I have decided to share this function so that others can weep with joy at how simple it will now be to send emails, html emails and attachments!

<?php
/*
m_mail function created by:
	Dean Williams
	http://dean.resplace.net

m_mail function usage:
----------------------
	$to 		= to email address, can add multiple using ,
	$from 		= from email address
	$subject	= subject of the email (plain text)
	$html		= email content, preferably html.
	$text		= plain text version of the email, send false and it will strip html from the $html version.
	$file_up	= the files to attach to the message, you can either send links or file data
				eg.
				array(
					"file1" => array(
						"name" => "filename.txt",
						"link" => "/path/to/file.txt"
					),
					"file2" => array(
						"name" => "filename2.txt",
						"data" => "[raw filedata here]"
					)
				);
*/
function m_mail($to, $from, $subject, $html, $text = '', $file_up = array()) {
    $eol = PHP_EOL;
    if(!$text) {
        $text = strip_tags(preg_replace('/<br[^>]*>/im', $eol, $html));
    }
	if (!$html) {
		$html = $text;
	}
   
    $random_hash = md5(time());
    $random_hash_2 = md5(time() + 1);
   
    $body = '--PHPmixed'.$random_hash_2 . $eol .
    'Content-Type: multipart/alternative; boundary= PHPalt' . $random_hash . $eol .
    $eol . '--PHPalt' . $random_hash . $eol .
    'Content-Type: text/plain; charset="iso-8859-1"' . $eol .
    'Content-Transfer-Encoding: 7bit' . $eol . $eol .
    $text .
    $eol . '--PHPalt' . $random_hash . $eol .
    'Content-Type: text/html; charset="iso-8859-1"' . $eol .
    'Content-Transfer-Encoding: 7bit' . $eol . $eol .
    $html . $eol;
   
    if(is_array($file_up) && count($file_up)) {
        $body .= '--PHPalt' . $random_hash . '--' . $eol;
   
        foreach($file_up as $file) {
			if (isset($file['link'])) {
				$file_n = file_get_contents($file['link']);
			} else {
				$file_n = $file['data'];
			}
            $attachment = chunk_split(base64_encode($file_n), 76, $eol);
           
            $body .= '--PHPmixed' . $random_hash_2;
            $body .= $eol . 'Content-Type: {"application/octet-stream"}; name="' . urlencode($file['name']) . '"' . $eol;
            $body .= 'Content-Disposition: attachment; filename="' . urlencode($file['name']) . '"' . $eol;
            $body .= 'Content-Transfer-Encoding: base64' . $eol . $eol . $attachment . $eol . $eol;
        }
    }
   
    $headers = 'From: ' . $from . $eol .
    'Reply-To: ' . $from . $eol .
    'MIME-Version: 1.0' . $eol .
    'Content-Type: multipart/mixed; boundary= PHPmixed' . $random_hash_2 . $eol .
    'Content-Transfer-Encoding: 7bit' . $eol .
    'X-Mailer: PHP/' . phpversion() . $eol .
    'This is a MIME encoded message.';
   
    if (mail($to, $subject, $body, $headers)) {
        return true;
    } else {
        return false;
    }
}
?>

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips