After going through the first 100 results for my "email attachment" search with no answers, I decided to post. Below is the code to send an email with an attached file from a program I'm working on. The email will send, but the attachment doesn't come through. However, the email displays the boundaries (random hash). I'm thinking I probably have a simple syntax error, but haven't been able to find it for two days. Any suggestions?
function sendReport($emailRecipients, $newOutFile, $debug, $myEmail)
{
$email = $myEmail;
$to = $emailRecipients;
$from = "Admin";
$subject = "Report";
$fileatt = $newOutFile;
$fileatttype = "application/csv";
$fileattname = "test.csv";
$headers = "From: $from";
if ($debug)
{
echo "newOutFile is: $newOutFile\n";
}
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
$data = chunk_split(base64_encode($data));
ob_start();
echo "This is a multi-part message in MIME format.\n\n" . "-{$mime_boundary}\n" . "Content-Type: text/plain";
echo "charset=\"iso-8859-1\"n" . "Content-Transfer-Encoding: 7bit\n\n" ;
echo "\n\n";
echo "–{$mime_boundary}\n" . "Content-Type: {$fileatttype};\n" . "name=\"{$fileattname}\"\n" . "Content-Disposition: attachment;\n ";
echo "filename=\"{$fileattname}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "-{$mime_boundary}-\n";
$message = ob_get_clean();
if(mail($to, $subject, $message, $headers))
{
if ($debug)
{
echo "email was sent with attachment: $newOutFile\n";
}//end if debug
else
{
echo "email failed to send";
}//end else
}//end if mail
}//end function sendReport
No replies to this topic
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









