Hello!
I am a newbie who's trying to code in my app a way to send email with file attachments. Here's the code I put together (thanks to Google!).
<?php
$fileatt = "/somedir";
$fileatt_type = "application/zip";
$fileatt_name = "logos.zip";
$email_from = "joe@work_email.com";
$email_subject = "attachment test-subject";
$email_txt = "attachment test-body";
$email_to = "matson@yahoo.com";
$headers = "From: ".$email_from;
$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}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
mail($email_to, $email_subject, $email_message, $headers);
?>
My dev box runs Linux and sendmail. The recipient ("joe@work_email.com") runs MS Windows and Outlook.
The code works and sends an email to the recipient. The problem is that the attached file is corrupted (read, empty). I sent gif and zip files as attachments: MS Outlook `sees` the files as gif or zip but the size is 0/zero bytes.
The weird part is that, using mutt on my dev box, I was able to successfully send GIF & ZIP as attachements.
Is it because the header is mal-formed in my code? What am I doing wrong here?
Thanks for helping.
Al.