I'm trying to email a file from the server. So far I put together something, but I can't seem to get it to work. I think the problem may lie in the fopen, or fread functions below (I'm not very familiar with either). Can someone check out these three lines and let me know what's wrong.
The email sends fine, and the email has an attachment, but the attachment is always corrupted in some way (image is broken, zip file wont open, etc.)
Also, I believe the MIME encoding is fine, but it would be great if someone took a second look at that. Thanks alot.
Jabbamonkey
$file_name; // Name of the file on the server, determined before
$file_type; // File type, determined before
$pathonserver = "/usr/home/v1/x1234567/html/upload/";
$fileonserver = $pathonserver."".$file_name;
$email_from = "Someone"; // Who the email is from
$email_subject = "Email Attachment from Server";
$email_message = "Testing Attachment from Server";
$email_to = "person@domainname.com";
// #### POSSIBLE PROBLEM ####
$file = fopen($fileonserver,'rb');
$data = fread($file,filesize($fileonserver));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==_=_Multipart_Boundary_x{$semi_rand}x";
$headers = "From: " . $email_from .
"\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$data = chunk_split(base64_encode($data));
$email_message = "--{$mime_boundary}\n" .
"Content-Type: text/html;\n" .
" charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: {$file_type};\n" .
" name=\"{$file_name}\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_name}\"\n" .
// $data . "\n\n" .
"\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);