Our blog

 

PHP Email Attachment Function

Sending an email with PHP is pretty straight forwards. It's very useful for emailing reports generated from cron jobs etc.

Sometimes though you need your application to email an attachment.

After a load of messing around I have hacked together this email attachment PHP function.

PHP:
  1. function email_attachment($to_email, $email, $subject,$our_email_name, $our_email, $file_location, $default_filetype='application/zip'){
  2.     $email = '<font face="arial">' . $email . '</font>';
  3.     $fileatt = $file_location;
  4.     if(function_exists(mime_content_type)){
  5.         $fileatttype = mime_content_type($file_location);
  6.     }else{
  7.         $fileatttype = $default_filetype;;
  8.     }
  9.     $fileattname =basename($file_location);
  10.     //prepare attachment
  11.     $file = fopen( $fileatt, 'rb' );
  12.     $data = fread( $file, filesize( $fileatt ) );
  13.     fclose( $file );
  14.     $data = chunk_split( base64_encode( $data ) );
  15.     //create mime boundary
  16.     $semi_rand = md5( time() );
  17.     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  18.     //create email  section
  19.     $message = "This is a multi-part message in MIME format.\n\n" .
  20.     "--{$mime_boundary}\n" .
  21.     "Content-type: text/html; charset=us-ascii\n" .
  22.     "Content-Transfer-Encoding: 7bit\n\n" .
  23.     $email . "\n\n";
  24.      //create attachment section
  25.     $message .= "--{$mime_boundary}\n" .
  26.      "Content-Type: {$fileatttype};\n" .
  27.      " name=\"{$fileattname}\"\n" .
  28.      "Content-Disposition: attachment;\n" .
  29.      " filename=\"{$fileattname}\"\n" .
  30.      "Content-Transfer-Encoding: base64\n\n" .
  31.      $data . "\n\n" .
  32.      "--{$mime_boundary}--\n";
  33.      //headers
  34.     $exp=explode('@', $our_email);
  35.     $domain = $exp[1];
  36.     $headers = "From: $our_email_name<$our_email>" . "\n";
  37.     $headers .= "Reply-To: $our_email"."\n";
  38.     $headers .= "Return-Path: $our_email" . "\n";    // these two to set reply address
  39.     $headers .= "Message-ID: <".time()."@" . $domain . ">"."\n";
  40.     $headers .= "X-Mailer: Edmonds Commerce Email Attachment Function"."\n";          // These two to help avoid spam-filters
  41.     $headers .= "Date: ".date("r")."\n";
  42.     $headers .= "MIME-Version: 1.0\n" .
  43.                     "Content-Type: multipart/mixed;\n" .
  44.                     " boundary=\"{$mime_boundary}\"";
  45.     
  46.     mail($to_email,$subject,$message, $headers, '-f ' . $our_email) or die ('<h3 style="color: red;">Mail Failed</h3>');
  47. }

The PHP and Email Journey Continues :-)

For a fully featured email attachment class this is definitely worth checking out. If you just want a quick function though without any bells and whistles then you should find my function will do the trick.

More Reading:

  • no matching posts found..
14 Comments

Colleen
September 6th, 2008

This works beautifully if 'allow_url_open' is switched on but most servers have it switched off for security reasons. I have been trying to replace the relevant bits of code with CURL but an struggling with opening the file and reading the data.

 

Joseph
September 7th, 2008

If you want to attach a remote file (accessed via a URL) then I would recommend downloading the file and storing it locally before trying to send it as an attachment using this function.

 

Markus
October 22nd, 2008

Excellent work.

I've read quite a few out-dated tutorials that don't seem to work, but this one worked like a charm and was easy to understand/modify.

Thanks!

 

admin
October 23rd, 2008

Thanks for the comment :-)

if you have managed to improve the function at all it would be great if you could share it here.

 

Awesome! Thank you. I have just been through every script on page one of google for "php email atatchment" and none of them worked. Your code works perfectly though. Thanks!

 

admin
December 10th, 2008

Glad you like :-)

 

andre
February 28th, 2009

Finally someone who has a made a normal useable function.
It seems that nobody can do this in the php community.

 

vlad
April 21st, 2009

Man, I have spent so much time trying to build a function which would send a html email with sound file wav attachment. Any other tutorial I googled out did not work.

This one worked right away!!!

Thank you, thank you, thank you. My day is brighter. :)

 

joseph
April 22nd, 2009

nice one :-)

 

[...] of sending an email with attachment, the closest I could get to a working example I found at EdmondsCommerce.co.uk so primary credit goes to him. However I have made some minor modifications which I think make it a [...]

 

Paul
September 30th, 2009

This is the best code I have found so far for this job, but still isnt working for me. I am trying to send a word doc, but its ending up as a zipped file of folders containing .xml files. Anyone know how to resolve this??

 

admin
October 8th, 2009

This is an issue with the new docx format

see http://msdn.microsoft.com/en-us/library/aa338205.aspx

 

[...] Then, email the file with: PHP Email Attachment Function | Edmonds Commerce Blog [...]

 

kash
January 31st, 2010

how to create browse and attach. I have a html form that provides option to attach and send email. But I want it to browse and attach multiple files.
How to?
Thanks.
Kash.

 

 

Leave a Reply