PHP mail() sending attachment

I came across a script to do what I’ve been trying to do for a while - that is to use php mail() to send an email with an attachment. It has taken some working on and I’m almost there - it sends the attachment BUT something is wrong with the encoding. Any ideas what I’ve got wrong please…

$to = "you@yourdomain.com>";
$from = "me@mydomain.co.uk>";
$subject = "Here is your attachment";
$fileAttName = "mypdf.pdf";
$fileAttExtn = substr(strrchr($fileAttName, '.'), 1);
$fileAtt = getcwd()."/".$fileAttName;
$fileAttType = "application/pdf";
$headers = "From: $from";

$file = fopen($fileAtt, 'rb');
$fileAttData = fread($file, filesize($fileAtt));
fclose($file);
$data = chunk_split(base64_encode($fileAttData));

$mime_boundary = md5(time());
$headers .= "\
MIME-Version: 1.0\
" .
'Content-Type: multipart/mixed; boundary='.$mime_boundary."\
" .
'--'.$mime_boundary."\
" .
'Content-Type: text/plain; charset="iso-8859-1"'."\
" .
'Content-Transfer-Encoding: 7bit';
$message = "This is a multi-part message in MIME format.\
\
";
$message .= "--".$mime_boundary."\
" .
'Content-Type: '.$fileAttType.'; name="'.$fileAttName.'"'."\
" .
'Content-Disposition: attachment; filename="'.$fileAttName.'"'."\
" .
'Content-Transfer-Encoding: base64'."\
" .
$data .= "\
--".$mime_boundary."--\
";

if ( @mail($to, $subject, $message, $headers) ) echo "<p>The email was sent.</p>";
else echo "<p>There was an error sending the email.</p>";

Hah! Spotted my mistake

$data .= "\
--".$mime_boundary."--\
";

should be

$data . "\
--".$mime_boundary."--\
";

I see the original is by Kevin Yank. I found the script on another website without attributing it to KY.

I noticed two errors, just remove the “>” tags from the $to and $from variable values.


<?php
$to          = "you@yourdomain.com";
$from        = "me@mydomain.co.uk";
$subject     = "Here is your attachment";
$fileAttName = "mypdf.pdf";
$fileAttExtn = substr(strrchr($fileAttName, '.'), 1);
$fileAtt     = getcwd() . "/" . $fileAttName;
$fileAttType = "application/pdf";
$headers     = "From: $from";

$file        = fopen($fileAtt, 'rb');
$fileAttData = fread($file, filesize($fileAtt));
fclose($file);

$data = chunk_split(base64_encode($fileAttData));

$mime_boundary = md5(time());

$headers .= "\\r\
MIME-Version: 1.0\\r\
" . 
			'Content-Type: multipart/mixed; boundary=' . $mime_boundary . "\\r\
" . '--' .
$mime_boundary . "\\r\
" . 'Content-Type: text/plain; charset="iso-8859-1"' ."\\r\
" . 'Content-Transfer-Encoding: 7bit';

$message = "This is a multi-part message in MIME format.\\r\
\\r\
";

$message .= "--" . $mime_boundary . "\\r\
" . 'Content-Type: ' .
$fileAttType . '; name="' .
$fileAttName . '"' . "\\r\
" . 'Content-Disposition: attachment; filename="' .
$fileAttName . '"' . "\\r\
" . 'Content-Transfer-Encoding: base64' . "\\r\
" .
$data .= "\\r\
--" . $mime_boundary . "--\\r\
";

if (mail($to, $subject, $message, $headers))
    echo "<p>The email was sent.</p>";
else
    echo "<p>There was an error sending the email.</p>";

Thanks Ernie. Actually the > aren’t in my actual script - I left them in when anonymising the script. Strange, the attachment opened on my Linux machine but not on Windows.

Anyway I have gone back to KY’s original article http://articles.sitepoint.com/article/advanced-email-php and start from there…