【问题标题】:Error when sending mail with attachment using PHP mailer: Transaction failed: Missing start boundary SMTP code: 554使用 PHP 邮件程序发送带有附件的邮件时出错:事务失败:缺少起始边界 SMTP 代码:554
【发布时间】:2019-10-10 16:51:53
【问题描述】:

我正在尝试发送带有 pdf 附件的 php mailer 电子邮件并显示此错误: 电子邮件未发送。错误:SMTP 错误:数据未接受。SMTP 服务器错误:DATA END 命令失败详细信息:事务失败:缺少开始边界 SMTP 代码:554

我尝试在所有地方添加哑剧边界,但结果相同。

<?php
include dirname(__FILE__) . '/../views/pdf-receipt.php';

function sendMailTest(
    $recipient_mail, $recipient_name, $from_mail, $from_name, $subject, $body, $body_without_html, $attachments=NULL
) {
    require 'folder/PHPMailer-5.2.26/PHPMailerAutoload.php';
    $mail = new PHPMailer;

    try {
        $mail->isSMTP();
        $mail->setFrom($from_mail, $from_name);
        $mail->addAddress($recipient_mail, $recipient_name);
        $mime_boundary = "Name of company".md5(time());
        $mail->SMTPDebug = true;
        $mail->Host = 'email-smtp.eu-west-1.amazonaws.com';
        $mail->Username = 'XXX';
        $mail->Password = 'XXX';

        $mail->Encoding = 'quoted-printable';
        $mail->addCustomHeader('Content-ID', '20cca', 'Content-Type', 'multipart/mixed', 'boundary='.$mime_boundary.'\n');
        $mail->Body = "--$mime_boundary\n";

        $mail->CharSet = 'UTF-8';
        $mail->Subject = utf8_encode($subject);

        $mail->Body .= "Hey\n";

        $mail->AltBody .= "--$mime_boundary\n".$body_without_html;

        $mail->SMTPAuth = true;

        $mail->SMTPSecure = 'tls';
        $mail->Port = 000;

        $mail->isHTML(true);
        $data = '%PDF-1.2 6 0 obj << /S /GoTo /D (chapter.1) >>';       
        // if i comment line below email is sent properly, 
        // if i use AddAttachment local pdf file its working also, 
        // but i use TCPD generated PDF and attaching it, if i will 
        // just write it out to the message body i will see it as a 
        // text or if i will attach 'addStringEmbeddedImage' its 
        //displayed correctly but on same line as message text

        // $attachments is pretty much this:
        // $pdf->Output("test.pdf", "S");
        // tried this $mail->Body .= "--$mime_boundary\n"; before next line
        // did not help
        $mail->AddStringAttachment($attachments, 'base64', 'application/pdf');

        if($mail->send()) {
            return;
        }
    } catch (Exception $e) {}

    echo 'E-mail not sent. Error: ', $mail->ErrorInfo, PHP_EOL;
}
?>

我希望发送带有附件的电子邮件,但没有看到起始边界错误。

【问题讨论】:

    标签: php amazon-web-services phpmailer php-5.2


    【解决方案1】:

    首先,我真的希望您实际上并没有使用 PHP 5.2。

    你在这里玩得很开心:

        $mail->addCustomHeader('Content-ID', '20cca', 'Content-Type', 'multipart/mixed', 'boundary='.$mime_boundary.'\n');
        $mail->Body = "--$mime_boundary\n";
    

    你使用 PHPMailer 的原因是你不必做这样的事情;它已经为你处理好了。当你试图像这样颠覆它时,它只会弄得一团糟,正如你所发现的那样。

    您已经努力将 PHPMailer 代码包装在 try/catch 块中,但您没有告诉 PHPMailer 抛出异常(通过将 true 传递给构造函数),并且您的 catch 块是空的,所以它什么也没做。

    我已对其进行了重写以解决这些问题,并更合乎逻辑地分组设置:

    include dirname(__FILE__) . '/../views/pdf-receipt.php';
    
    function sendMailTest(
        $recipient_mail,
        $recipient_name,
        $from_mail,
        $from_name,
        $subject,
        $body,
        $body_without_html,
        $attachments = null
    ) {
        require_once 'folder/PHPMailer-5.2.26/PHPMailerAutoload.php';
        $mail = new PHPMailer(true);
    
        try {
            $mail->isSMTP();
            $mail->SMTPDebug = 2;
            $mail->Host = 'email-smtp.eu-west-1.amazonaws.com';
            $mail->SMTPSecure = 'tls';
            $mail->Port = 587;
            $mail->SMTPAuth = true;
            $mail->Username = 'XXX';
            $mail->Password = 'XXX';
    
            $mail->CharSet = 'UTF-8';
            $mail->setFrom($from_mail, $from_name);
            $mail->addAddress($recipient_mail, $recipient_name);
            $mail->Subject = $subject;
    
            $mail->Body .= $body;
            $mail->AltBody = $body_without_html;
    
            $mail->isHTML(false);
            $data = '%PDF-1.2 6 0 obj << /S /GoTo /D (chapter.1) >>';
    
            $mail->addStringAttachment($data, 'receipt.pdf');
    
            $mail->send();
        } catch (Exception $e) {
            echo 'E-mail not sent. Error: ', $mail->ErrorInfo, PHP_EOL;
        }
    }
    

    【讨论】:

    • 非常感谢您清理代码,实际上就像在我开始清理我编写的所有额外代码并给出结果之前的 10 分钟一样,我现在面临的唯一问题是在mac mailer pdf上显示与邮件正文内联,如果我将它发送到outlook,那么它只是一个附件并且它没有显示在邮件正文中,这很奇怪
    • 您无法控制客户端如何处理附件。 Apple Mail 具有非常好的 PDF 支持,这在 Windows 上并不存在,所以这是意料之中的。
    猜你喜欢
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2012-05-24
    • 2014-02-17
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多