使用 PHP 邮件程序发送带附件的邮件时出错:事务失败:缺少起始边界 SMTP 代码:554
Error when sending mail with attachment using PHP mailer: Transaction failed: Missing start boundary SMTP code: 554
我正在尝试发送带有 pdf 附件的 php 邮寄电子邮件,但显示此错误:
电子邮件未发送。错误:SMTP 错误:数据不是 accepted.SMTP 服务器错误:DATA END 命令失败详细信息:事务失败:缺少起始边界
SMTP 代码:554
我尝试在各处添加 mime 边界,但结果相同。
<?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 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;
}
}
我正在尝试发送带有 pdf 附件的 php 邮寄电子邮件,但显示此错误: 电子邮件未发送。错误:SMTP 错误:数据不是 accepted.SMTP 服务器错误:DATA END 命令失败详细信息:事务失败:缺少起始边界 SMTP 代码:554
我尝试在各处添加 mime 边界,但结果相同。
<?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 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;
}
}