【发布时间】:2015-08-15 04:00:54
【问题描述】:
我搜索并尝试不同的方法来生成带有附件的 php 邮件,该附件可通过客户端 (Outlook) 保存。它不起作用,我不知道为什么?
如果脚本使用此代码发送邮件:
$result = mail($mail_to, $subject, $headers);
我收到一封带有内联 base64 编码附件的电子邮件,如果以这种方式使用它:
$result = mail($mail_to, $subject, "", $headers);
或
$result = mail($mail_to, $subject, $text, $headers);
我收到了 PHP 错误:“false” - 邮件无法发送。
所以不是邮件服务器问题,我认为从 headersview 来看,它应该可以工作,并且我在 Linux 站点上没有收到邮件错误。
内嵌附件的邮件的一部分:
MIME-Version: 1.0
From: user@nowhere.xyz
Content-Type: multipart/mixed; boundary="F34A3E8D822EE5A70EEDE9C3C143243C"
--F34A3E8D822EE5A70EEDE9C3C143243C
Content-Type: multipart/alternative; boundary="A0403290562B2A2F19FB62E48C51BE33"
--A0403290562B2A2F19FB62E48C51BE33
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Hello Mr. Xyz,
Some stupid text as an example.
--A0403290562B2A2F19FB62E48C51BE33--
--F34A3E8D822EE5A70EEDE9C3C143243C
Content-Type: application/CSV; charset="ISO-8859-1"; name="trouble.csv"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="trouble.csv"
...
YWNodW5nIG5pY2h0IGVyZm9yZGVybGljaCAvIG3DtmdsaWNoIjsiIjsiIjsK
--F34A3E8D822EE5A70EEDE9C3C143243C--
匹配的PHP代码:
$text = "Hello Mr. Xyz," . $nl . "Some stupid text as an example." . $nl;
$content = chunk_split(base64_encode($file));
$headers = "MIME-Version: 1.0" . $nl;
$headers .= "From: " . $mail_from . $nl;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary1 . "\"" . $nl . $nl;
$headers .= "--" . $boundary1 . $nl;
$headers .= "Content-Type: multipart/alternative; boundary=\"" . $boundary2 . "\"" . $nl . $nl;
$headers .= "--" . $boundary2 . $nl;
$headers .= "Content-Type: text/plain" . $nl;
$headers .= "Content-Transfer-Encoding: 8bit" . $nl . $nl;
$headers .= $text . $nl;
$headers .= "--" . $boundary2 . "--" . $nl . $nl;
$headers .= "--" . $boundary1 . $nl . $nl;
$headers .= "Content-Type: application/excel; charset=\"ISO-8859-1\"; name=\"xyz.csv\"" . $nl;
$headers .= "Content-Transfer-Encoding: base64" . $nl;
$headers .= "Content-Disposition: attachment; filename=\"xyz.csv\"" . $nl . $nl;
$headers .= $content . $nl;
$headers .= "--" . $boundary1 . "--" . $nl;
那么,错误在哪里?我该如何调试这种情况? 提前谢谢...
【问题讨论】:
-
§nl可能是语法错误。有关 mail() 失败的典型原因,另请参阅 PHP mail form doesn't complete sending e-mail。虽然 MIME multipart assembling looks ok 一次,但对于此类任务,您仍然最好使用 PHPMailer/SwiftMailer。 (两者都为 SMTP 错误提供了更好的调试选项。) -
“§nl”会是一个语法错误,但这只是从原始来源到这里的帖子的转移错误。
-
问题是...:$eaders 中的双重 nl... 删除它们 我收到了一封带有附件的电子邮件,但它太短并且消息文本为空...
标签: php email csv email-attachments