Sending HTML emails with attachment with PHP

This function allows you to send an HTML email with a file attachment. To do this it’s necessary to read…

January 10, 2010

This function allows you to send an HTML email with a file attachment. To do this it’s necessary to read the file and encode it with base 64 and split it in small chunks, and write everything using mail headers.

The $fileatt parameter identify the filename to be sent. The $replyto is optional and allows you to specify a different reply-to address. The $from parameter specify the sender address. The other parameters are the same of the ones of the mail php function.

It returns true if succeeded.

function mail_file( $to, $subject, $messagehtml, $from, $fileatt, $replyto="" ) {
		// handles mime type for better receiving
		$ext = strrchr( $fileatt , '.');
		$ftype = "";
		if ($ext == ".doc") $ftype = "application/msword";
		if ($ext == ".jpg") $ftype = "image/jpeg";
		if ($ext == ".gif") $ftype = "image/gif";
		if ($ext == ".zip") $ftype = "application/zip";
		if ($ext == ".pdf") $ftype = "application/pdf";
		if ($ftype=="") $ftype = "application/octet-stream";
		// read file into $data var
		$file = fopen($fileatt, "rb");
		$data = fread($file,  filesize( $fileatt ) );

		// split the file into chunks for attaching
		$content = chunk_split(base64_encode($data));
		$uid = md5(uniqid(time()));

		// build the headers for attachment and html
		$h = "From: $from\r\n";
		if ($replyto) $h .= "Reply-To: ".$replyto."\r\n";
		$h .= "MIME-Version: 1.0\r\n";
		$h .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
		$h .= "This is a multi-part message in MIME format.\r\n";
		$h .= "--".$uid."\r\n";
		$h .= "Content-type:text/html; charset=iso-8859-1\r\n";
		$h .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
		$h .= $messagehtml."\r\n\r\n";
		$h .= "--".$uid."\r\n";
		$h .= "Content-Type: ".$ftype."; name=\"".basename($fileatt)."\"\r\n";
		$h .= "Content-Transfer-Encoding: base64\r\n";
		$h .= "Content-Disposition: attachment; filename=\"".basename($fileatt)."\"\r\n\r\n";
		$h .= $content."\r\n\r\n";
		$h .= "--".$uid."--";

		// send mail
		return mail( $to, $subject, strip_tags($messagehtml), str_replace("\r\n","\n",$h) ) ;



I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of and and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “Sending HTML emails with attachment with PHP”

15 thoughts

  1. lee murray says:

    the problem with this script is that you can only send one attachment at a time.

  2. Vadim says:

    Thank you, this is exactly what I have needed.

  3. Tim says:

    This is excellent thanks.

  4. Dan says:

    Excellent, thanks for the snippet well done.

  5. Ayo says:

    This is absolutely beatiful. It’s a perfect code. The 1st of its kind that I’ve found on these Php mail/attachment issue. I’m so glad I stumbled on this website/post.
    Imagine I’ve been frustrated for over 1 month searching online a code that could do this for me.
    Thanks and keep it up!

  6. sudeep says:


  7. Hemant Vaniya says:

    Thank you very much, it is working excellent.

  8. az says:

    a bit shitty – no file validation

  9. angel16mk says:

    what does ‘rb’means in the 13-th row?

  10. Giulio Pons says:

    rb means read for binary files, such as jpegs.

  11. calady says:

    guys u need to help me coz i think am running out.i try to design a website where there is a button when clicked is supposed to attach a file to a website and i dont know where to start

  12. File is attached , but blank attachment.
    Is there any code missing?

  13. Norm Jurgen says:

    You’re wonderful! Thank you for sharing your mail_file function.

  14. Tim B says:

    The line that defines $htmlMsg had the surrounding html and body tags stripped off during the upload.

Comments are closed


WP doesn’t send email? try this

Snippets for sending emails with Wordpress

February 8, 2020

Modify the language attribute based on category in WordPress

How to modify the language attribute in your Wordpress theme using a specific value

November 7, 2019

WordPress anyone can register, but no email confirmation

Many times it has happened to me that my WordPress installation properly send emails using wp_mail directly, specifing also the…

August 25, 2015

Embedding images in HTML or CSS with PHP

This small function returns an encoded string to embed images, inline, inside your html/css code and reduce the number of…

December 4, 2013

Block junk emails, spammers and temporary emails

If you need an Email Validator Function, consider this version that includes also the check against common temporary mail services…

December 3, 2013

Reading mp3 informations with php (id3 tags)

Inside mp3 files there are often some usefull informations stored. Those data are called “id3 tags” and deal with Author…

February 22, 2010