Mail with File Attachment via PHP

Hey, I am trying to send email with an attached file via PHP script. I tried two methods (below) but neither one seems to work correctly. In both cases, the file contents are being included in the body and not as an attachment. Can anyone help me with this or see the problem? Thanks!

Method 1:

random_hash = md5(date('r', time()));

$headers = 'MIME-Version: 1.0' . "\\r\
"; $headers .= 'From: Backlinkbuild <support@backlinkbuild.com>' . "\\r\
"; $headers .= 'Cc: Backlinkbuild <orders@backlinkbuild.com>' . "\\r\
"; $headers .= "\\r\
Content-Type: multipart/mixed; boundary=\\"PHP-mixed-".$random_hash."\\"";

$attachment =
chunk_split(base64_encode(file_get_contents($_FILES['File1']['tmp_name'])));


$fileatt_name = $_FILES['File1']['name'];// Name of file

$output = "
   &#8211;PHP-mixed-$random_hash;
   Content-Type: multipart/mixed; boundary='PHP-alt-$random_hash'
   &#8211;PHP-alt-$random_hash
   Content-Type: multipart/mixed; charset='iso-8859-1'
   Content-Transfer-Encoding: 7bit

   Hello,
   This is testing email with attachment. Please find attachment.

   &#8211;PHP-alt-$random_hash
   Content-Type: multipart/mixed; charset='iso-8859-1'
   Content-Transfer-Encoding: 7bit

   Hello,
   This is testing email with attachment. Please find attachment.

   &#8211;PHP-alt-$random_hash&#8211;

   &#8211;PHP-mixed-$random_hash
   Content-Type: application/vnd.ms-excel; name=$fileatt_name
   Content-Transfer-Encoding: base64
   Content-Disposition: attachment

   $attachment
   &#8211;PHP-mixed-$random_hash&#8211;";


mail($order['Email'],$subj,$output,$headers);

Method 2:

$attachment =
chunk_split(base64_encode(file_get_contents($_FILES['File1']['tmp_name'])));

$fileatt = $_FILES['File1']['tmp_name']; // Location of file on server
$fileatt_type = $_FILES['File1']['type'];// Type of file being sent
$fileatt_name = $_FILES['File1']['name'];// Name of file
$fileatt_path = $_FILES['File1']['tmp_name'];

   // Read the file to be attached ('rb' = read binary)
   $tmp_name1 = $_FILES['file1']['tmp_name'];
   $file = $_FILES['File1']['tmp_name'];
   $data = file_get_contents($_FILES['File1']['tmp_name']);

   // Generate a boundary string
   $semi_rand = md5(time());
   $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
   $headers .= 'From: Backlinkbuild <support@backlinkbuild.com>' . "\\r\
";
   $headers .= 'Cc: Backlinkbuild <orders@backlinkbuild.com>' . "\\r\
";
   // Add the headers for a file attachment
   $headers .= "\
MIME-Version: 1.0\
" .
   "Content-Type: multipart/mixed;\
" .
   " boundary=\\"{$mime_boundary}\\"";
   // Add a multipart boundary above the plain message
   $message = "This is a multi-part message in MIME format.\
\
" .
   "--{$mime_boundary}\
" .
   "Content-Type: text/html; charset=\\"iso-8859-1\\"\
" .
   "Content-Transfer-Encoding: 7bit\
\
" .
   $message . "\
\
";
   $message .="This is an automated email sent from a php script\
";

   // Base64 encode the file data
   $data = chunk_split(base64_encode($data));
   // Add file attachment to the message
   $message .= "--{$mime_boundary}\
" .
   "Content-Type: {$fileatt_type};\
" .
   " name=\\"{$fileatt_name}\\"\
" .
   "Content-Disposition: attachment;\
" .
   " filename=\\"{$fileatt_name}\\"\
" .
   "Content-Transfer-Encoding: base64\
\
" .
   $data . "\
\
" .
   "--{$mime_boundary}--\
";  




mail($order['Email'], $mail_subject, $message, $headers);

In the first code, I have no idea what you are doing. What is -php-alt?

The second code is closer, but having single space characters at the start of header lines will break it.
e.g.


" boundary=\\"{$mime_boundary}\\"";

should be


"boundary=\\"{$mime_boundary}\\"";

Thanks for the tip but still doesn’t work… probably fix one of the problems though. :slight_smile: Any other suggestions?

How about taking advantage of others who have already solved this problem, by using something like PHP Mailer.
http://phpmailer.worxware.com/

Have you solved your problem yet?

I have a similar prob. I’ve not had time to look at your entire script but have you tried

"Content-Type: multipart/mixed; boundary=\\"{$mime_boundary}\\"";

ie all on one line on option 2?