简体   繁体   中英

PHP email attachments

I'm trying to send emails with multiple attachments.

This is an example:
$uid = md5(uniqid(time()));
$header = "From: Test <test@test.com>\r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: multipart/mixed; boundary=\"". $uid. "\"\r\n\r\n";
$header.= "--". $uid. "\r\n";
$header.= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header.= "Test Email\r\n\r\n";
$header.= "--". $uid. "\r\n";
$header.= "Content-Type: text/x-csv; name=\"1.csv\"\r\n";
$header.= "Content-Transfer-Encoding: base64\r\n";
$header.= "Content-Disposition: attachment; filename=\"1.csv\"\r\n\r\n";
$header.= $csvfile1. "\r\n\r\n";
$header.= "--". $uid. "--";
$header.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header.= "Test Email\r\n\r\n";
$header.= "--". $uid. "\r\n";
$header.= "Content-Type: text/x-csv; name=\"2.csv\"\r\n";
$header.= "Content-Transfer-Encoding: base64\r\n";
$header.= "Content-Disposition: attachment; filename=\"2.csv\"\r\n\r\n";
$header.= $csvfile2. "\r\n\r\n";
$header.= "--". $uid. "--";
$header.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header.= "Test Email\r\n\r\n";
$header.= "--". $uid. "\r\n";
$header.= "Content-Type: text/x-csv; name=\"3.csv\"\r\n";
$header.= "Content-Transfer-Encoding: base64\r\n";
$header.= "Content-Disposition: attachment; filename=\"3.csv\"\r\n\r\n";
$header.= $csvfile3. "\r\n\r\n";
$header.= "--". $uid. "--";

And I get some strange characters at the end of the csv files, something like this:
w־סֽד·ֽסמ׳fק־ןo,
‰z{©}ךֺxn+¢”j״¶'Eט²¶*Jײֻrֵz°¶+·$j״¶'5ל†·u+¶+‰ֻ{²װj״¶',

What could be the problem?

Thanks.

Some issues:

  • There should be only one new line before a boundary. You have 2.
  • You should have 2 dashes only after the last boundary.
  • There are 2 places where Content-Transfer-Encoding needs to have a new line before.
  • You may have forgotten to base64 encode your files.
  • Your code looks like the result of some terrible copy-pasting.
  • Maybe other.

As El Yobo said in a comment, maybe you should try using a library for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM