简体   繁体   English

PHP email 附件

[英]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:我在 csv 文件的末尾得到了一些奇怪的字符,如下所示:
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.你有 2 个。
  • You should have 2 dashes only after the last boundary.您应该只在最后一个边界之后有 2 个破折号。
  • There are 2 places where Content-Transfer-Encoding needs to have a new line before.有两个地方Content-Transfer-Encoding之前需要换行。
  • You may have forgotten to base64 encode your files.可能忘记了 base64 对您的文件进行编码。
  • 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.正如 El Yobo 在评论中所说,也许您应该尝试为此使用库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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