【问题标题】:php mail attachment - error - how to debugphp邮件附件-错误-如何调试
【发布时间】: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


【解决方案1】:

这个有效....

        $text     = "Hello Mr. Xyz," . $crnl . "Some stupid text as an example."                . $crnl;
        $content  = chunk_split(base64_encode($file));

        $headers  = "MIME-Version: 1.0"                                                         . $crnl;
        $headers .= "From: " . $mail_from                                                       . $crnl;

        $headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary1 . "\""            . $nl . $crnl;
        $headers .= "Content-Type: text/plain"                                                  . $nl;
        $headers .= "--" . $boundary1                                                           . $crnl;
        $headers .= 'Content-Length: '.strlen($text)                                            . $crnl;
        $headers .= "Content-Transfer-Encoding: 8-bit"                                          . $nl . $crnl;
        $headers .= $text;
        $headers .= "--" . $boundary1                                                           . $crnl;

        $headers .= "Content-Type: application/excel; charset=\"ISO8859-1\"; name=\"xyz.csv\""  . $nl . $crnl;
        $headers .= "--" . $boundary1                                                           . $crnl;
        $headers .= "Content-Disposition: attachment; filename=\"xyz.csv\""                     . $crnl;
        $headers .= 'Content-Length: ' . strlen($content)                                       . $crnl;
        $headers .= "Content-Transfer-Encoding: base64"                                         . $nl . $crnl;
        $headers .= $content;
        $headers .= "--" . $boundary1 . "--"  

...有一个小错误。我有一个额外的 attment,但现在还可以。我们的另一件事是在正确的地方(行)使用“\n”、“\r\n”和两者的混合。

【讨论】:

    猜你喜欢
    • 2011-09-28
    • 1970-01-01
    • 2015-03-22
    • 2012-06-08
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多