SharkBait Posted August 21, 2007 Share Posted August 21, 2007 When I am trying to send an attachment with an email it seems to go inline with the email and not attached. What am I doing wrong with my MIME headers? <?php $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_{$semi_rand}x"; $HEADERS = "MIME-Version: 1.0\n Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\""; $BODY = "This is a multi-part message in MIME Format \n\n --{$mime_boundary}\n Content-Type: text/plain; charset=\"iso-8859-1\"\n Content-Transfer-Encoding: 7bit\n\n {$message}\n\n"; $FILE = fopen("log.txt", 'rb'); //$FILETYPE = mime_content_type("log.txt"); $FILETYPE = "text/plain"; $DATA = fread($FILE, filesize("log.txt")); fclose($FILE); // Base64 encode the file data $DATA = chunk_split(base64_encode($DATA)); // Add the attachment to the message $BODY .= "--{$mime_boundary}\n Content-Type: {$FILETYPE}; name=\"log.txt\"\n Content-Disposition: attachment; filename=\"log.txt\"\n Content-Transfer-Encoding: base64\n\n {$DATA}\r\n --{$mime_boundary}--\n"; if(!mail('my@blah.com', 'Log File', $BODY, $HEADERS)) { echo "Error in sending Email"; } else { echo "Mail sent successfully"; } ?> Quote Link to comment Share on other sites More sharing options...
SharkBait Posted August 21, 2007 Author Share Posted August 21, 2007 I figured out what the problem was. The $BODY and $HEADERS had too much white space after each line so the headers were not being set up properly. All fixed now <?php // Generate Boundary String $semi_rand = md5(date('r')); $mime_boundary = "x{$semi_rand}x"; $HEADERS = "MIME-Version: 1.0\r\n". "Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\"\r\n"; $BODY = "This is a multi-part message in MIME Format \r\n". "--{$mime_boundary}\r\n". "Content-Type: text/plain; charset=\"iso-8859-1\"\n". "Content-Transfer-Encoding: 7bit\r\n". "{$message}\n\n"; $FILE = fopen("log.txt", 'rb'); //$FILETYPE = mime_content_type("log.txt"); $FILETYPE = "text/plain"; $DATA = fread($FILE, filesize("log.txt")); fclose($FILE); // Base64 encode the file data $DATA = chunk_split(base64_encode($DATA)); // Add the attachment to the message $BODY .= "--{$mime_boundary}\r\n". "Content-Transfer-Encoding: base64\n\n". "Content-Disposition: attachment; filename=\"log\"\r\n". "{$DATA}\r\n". "--{$mime_boundary}--\r\n"; if(!mail('blah@blah.com', 'Log File', $BODY, $HEADERS)) { echo "Error in sending Email"; } else { echo "Mail sent successfully"; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.