php 邮件附件-错误-如何调试

php mail attachment - error - how to debug

我搜索并尝试了不同的方法来生成 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;

所以,bug 在哪里?我该如何调试这种情况? 提前致谢...

这个有效....

        $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”和两者的混合。