简体   繁体   中英

Error when sending mail with attachment using PHP mailer: Transaction failed: Missing start boundary SMTP code: 554

Im trying to send php mailer email with pdf attachment and its showing this error: E-mail not sent. Error: SMTP Error: data not accepted.SMTP server error: DATA END command failed Detail: Transaction failed: Missing start boundary SMTP code: 554

I tried to add mime boundaries all over the place, but had same result.

<?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;
}
?>

I expect email to be sent with attachment but see no starting boundary error.

Firstly, I really hope you're not actually using PHP 5.2.

You're having a good go at shooting yourself in the foot here:

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

The reason you use PHPMailer is so that you don't have to do things like this; it's taken care of for you. When you try to subvert it like this, it's just going to make a mess, as you're finding.

You've gone to the effort of wrapping your PHPMailer code in a try/catch block, but you have not told PHPMailer to throw exceptions (by passing true to the constructor), and your catch block is empty, so it's doing nothing.

I've rewritten it to fix these things, and group settings more logically:

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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