php - Creating CSV on the Fly and Emailing, attachment not being created -
i inherited php task fellow developer. need make csv on fly , email. creating csv content working, validated , fine. when testing email , csv producing email , attachment both blank no text content? made sure script clear of hidden characters (tabs, etc) , when retesting email has content... there no csv atttachment!
this php code:
// make csv & email $csvstring = chunk_split(base64_encode($csvstring)); //csv produced earlier , valid // create email , send off $mailsubject = "daily csv site"; $from = "root@localhost.com"; $headers = "from: " . $from ."\n"; $headers .= 'mime-version: 1.0' . "\n"; $headers .= 'content-type: multipart/mixed; boundary="----=_ nextpart_001_0011_1234abcd.4321fdac"' . "\n"; $message = 'this multi-part message in mime format. ------=_nextpart_001_0011_1234abcd.4321fdac content-type: text/plain; charset="us-ascii" content-transfer-encoding: 7bit hello oh look! attached csv! regards ------=_nextpart_001_0011_1234abcd.4321fdac content-type: application/octet-stream; name="'; $message .= "$cvsname"; $message .= '" content-transfer-encoding: base64 content-disposition: attachment; filename="'; $message .= "$cvsname"; $message .= '" '; $message .= "$csvstring"; // encoded $message .= ' ------=_nextpart_001_0011_1234abcd.4321fdac-- '; // send email if (mail($email, $mailsubject, $message, $headers, "-f$from")) { echo("message sent!"); } else { echo("message delivery failed..."); }
this email produced, notice there's no csv attached email contain encoded csv text/values. can 1 help?
this source code of produced email:
date: fri, 05 apr 2013 14:39:52 +0200 subject: daily csv site to: root@localhost.com from: root@localhost mime-version: 1.0 content-type: multipart/mixed; boundary="-----=_nextpart_001_0011_1234abcd.4321fdac" multi-part message in mime format. -----=_nextpart_001_0011_1234abcd.4321fdac content-type: text/plain; charset="us-ascii" content-transfer-encoding: 7bit hello oh look! attached csv! regards -----=_nextpart_001_0011_1234abcd.4321fdac content-type: application/octet-stream; name="marcel.preukschat-requested-19-03-2013.csv" content-transfer-encoding: base64 content-disposition: attachment; filename="marcel.preukschat-requested-19-03-2013.csv" marcel.preukschat-requested-19-03-2013.csv -----=_nextpart_001_0011_1234abcd.4321fdac
i think has unnecessary line break near boundary definition. let's make boundary little shorter make things more obvious. see this example of multipart message:
$headers .= 'content-type: multipart/mixed; boundary="nextpart_001"' . "\n";
and parts below:
$message = 'this multi-part message in mime format. --nextpart_001 content-type: text/plain; charset="us-ascii" content-transfer-encoding: 7bit hello oh look! attached csv! regards --nextpart_001 content-type: application/octet-stream; name="';
and
$message .= ' --nextpart_001-- ';
Comments
Post a Comment