Rayj2019 Posted May 13 Share Posted May 13 (edited) So, I use mail() to send form data to a client. Works great. Now I am trying to send email to the same client, but with an attachment. I know PHPmailer is preferred, but I'm old school and would like to get this to work. The file I want to send as attachment gets to the far end, but Outlook email displays the binary data instead of having the pdf as an attachment attachment and looks like this: Attached is the Legal Description for 24050240--8f082344de8525de682e9d6aa86aa83f Content-Type: application/pdf; name="Testfile1.pdf" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="Testfile1.pdf" JVBERi0xLjcNJcjIyMjIyMgNMSAwIG9iago8PC9OYW1lcyA4IDAgUi9PdXRsaW5lcyAyIDAgUi9U eXBlL0NhdGFsb2cvUGFnZXMgMyAwIFIvVmVyc2lvbi8xLjc+Pg0KZW5kb2JqCjIgMCBvYmoKPDwv VHlwZS9PdXRsaW5lcy9Db3VudCAwPj4NCmVuZG9iagozIDAgb2JqCjw8L0NvdW50IDEyL0tpZHNb OSAwIFIgNTAgMCBSIDc1IDAgUiA5OSAwIFIgMTI5IDAgUiAxNDUgMCBSIDE2OCAwIFJdL1R5cGUv UGFnZXM+Pg0KZW5kb2JqCjQgMCBvYmoKPDwvU3ViamVjdCgpL1Byb2R1Y2VyKEFzcG9zZS5QZGYg Zm9yIC5ORVQgOS4yLjEpL0NyZWF0aW9uRGF0ZShEOjIwMjQwMTI2MTE1NTQzKS9BdXRob3IoKS9N b2REYXRlKEQ6MjAyNDAxMjYxMTU1NDMpL0NyZWF0b3IoQXNwb3NlIEx0ZC4pL1RpdGxlKE11bHRp cGxlIERvY3VtZW50cyk+Pg0KZW5kb2JqCjggMCBvYmoKPDwvRGVzdHMgMTg0IDAgUj4+DQplbmRv ... ... more of this... The file size is 723709. Here is the code I am using: (I have some echo statements to help me trouble shoot.) Your help appreciated. function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { echo nl2br ("In funtion mail_attachment: filename = $filename\n"); $file = ($filename); echo nl2br ("In funtion mail_attachment: file = $filename\n"); echo nl2br ("In funtion mail_attachment: path = $path\n"); $file_size = filesize("$path/$file"); echo ("file_size = $file_size\n"); $handle = fopen($path.$filename, "rb"); echo ("handle = $handle"); //$content = file_get_contents($file); $content = fread($handle, $file_size); fclose($handle); $content = chunk_split(base64_encode($content)); echo ("Content = $content\n"); $uid = md5(uniqid(time())); $eol = PHP_EOL; // Headers $header = "From: ".$from_name." <".$from_mail.">".$eol; $header .= "Reply-To: ".$replyto.$eol; $header .= "MIME-Version: 1.0".$eol; $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\""; // // Message $header .= "--".$uid.$eol; $header .= "Content-type:text/plain; charset=iso-8859-1.".$eol; $header .= "Content-Transfer-Encoding: 8bit".$eol.$eol; //$message .= $body.$eol; $message .= "--".$uid.$eol; $message .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol; // use different content types here $message .= "Content-Transfer-Encoding: base64".$eol; $message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol; $message .= $content.$eol; $message .= "--".$uid."--"; if (mail($mailto, $subject, $message, $header)) { echo "mail send ... OK"; // or use booleans here } else { echo "mail send ... ERROR!\n"; echo nl2br ("file = $file\n"); echo nl2br ("path.filename = $path.$filename\n"); } } Edited May 13 by Rayj2019 add more info Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted May 13 Solution Share Posted May 13 The point of a library like PHPMailer isn't so that you don't know what's going on. It's so that you don't have to worry about details - as you put it, so you can "get this to work". Because here are the lines where you're doing something wrong: $eol = PHP_EOL; $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\""; $header .= "--".$uid.$eol; $header .= "Content-type:text/plain; charset=iso-8859-1.".$eol; $header .= "Content-Transfer-Encoding: 8bit".$eol.$eol; $message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol; But it's sure not obvious what's wrong about them, right? Spoiler 1. Line endings with mail() are tricky. It's more likely to get this wrong than to get this right. And not even PHP itself is able to always get it right. 2. No EOL 3. You're adding these to the headers when you should be adding them to the message 4. Missing a second EOL because the content begins afterwards Switch to PHPMailer or SwiftMailer or some other standard library so you can get this problem resolved quickly and move onto the next one. Quote Link to comment Share on other sites More sharing options...
Rayj2019 Posted May 14 Author Share Posted May 14 Thank you, thank you, thank you! You fixed it. Attachment being sent and received. And yes, I will use PHPmailer for my next project, if needed. Ray 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.