无法理解为什么我会收到此错误 - 无论是对于&从电子邮件地址是有效的(我每天使用它们)所以无法弄清楚这是怎么回事 - 任何帮助将不胜感激。
注意:这在生产中有效,但在dev中引发错误。我在dev中有更严格的配置。 注意:我使用smtp4dev在PC上本地测试
$to = 'myemail@mydomain.com.au';
$cc = 'myemail@mydomain.com.au';
$from = 'myemail@mydomain.com.au';
$filename = 'Invoice#'.$order_id.'.pdf';
$message = file_get_contents(ROOT_DIR.'admin/include/email-body.html');
$content = chunk_split(base64_encode($pdf_file));
$uid = md5(uniqid(time()));
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'To: '. $to . "\r\n";
$headers .= 'Cc: '. $cc . "\r\n";
$headers .= 'From: '. $from . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $message."\r\n\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$headers .= $content."\r\n\r\n";
$headers .= "--".$uid."--";
if (mail($to, $subject, $message, $headers)) {
print "SUCCESS";
} else {
print "FALIED";
}
如果我在mail()行打印变量,结果如下:
mail(<myemail@mydomain.com.au>, Company - Invoice#12451, "",
MIME-Version: 1.0
To: <myemail@mydomain.com.au>
Cc:
From: Customer Service <myemail@mydomain.com.au>
Content-Type: multipart/mixed;
boundary="2c88ff549e67c83e7a6e3df0bffe9dc9"
This is a multi-part message in MIME format.
--2c88ff549e67c83e7a6e3df0bffe9dc9 Content-type:text/html;
charset=iso-8859-1 Content-Transfer-Encoding: 7bit
---> html message body stripped <---
--2c88ff549e67c83e7a6e3df0bffe9dc9 Content-Type: application/pdf;
name="Invoice#12451.pdf" Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Invoice#12451.pdf"
--> pdf attachment stripped <--
--2c88ff549e67c83e7a6e3df0bffe9dc9--)
答案 0 :(得分:1)
(smtp4dev的作者)
这是一个更新的答案 - 我原本无法用您发布的代码重现问题,但我注意到您的第二位输出显示有时您没有CC地址。
此代码重现该问题并导致501错误。
<?php
$to = 'myemail@mydomain.com.au';
$cc = '';
$from = 'myemail@mydomain.com.au';
$headers .= 'To: '. $to . "\r\n";
$headers .= 'Cc: '. $cc . "\r\n";
$headers .= 'From: '. $from . "\r\n";
mail($to, $subject, $message, $headers);
?>
在Windows上,PHP邮件功能查看TO / CC地址的邮件头,并将每个头转换为SMTP RCPT TO命令。不幸的是,即使标题没有值,它也会这样做:
220 localhost smtp4dev ready
HELO Computer
250 Nice to meet you
MAIL FROM:<myemail@mydomain.com.au>
250 Okey dokey
RCPT TO:<myemail@mydomain.com.au>
250 Recipient accepted
RCPT TO:<>
501 Must specify to address <address>
QUIT
221 See you later aligator
因此,如果您没有要发送到的CC地址,则根本不需要包含CC:标头:
<?php
$to = 'myemail@mydomain.com.au';
$cc = '';
$from = 'myemail@mydomain.com.au';
$headers .= 'To: '. $to . "\r\n";
if ($cc) {
$headers .= 'Cc: '. $cc . "\r\n";
}
$headers .= 'From: '. $from . "\r\n";
mail($to, $subject, $message, $headers);
?>
这对我来说很好。
答案 1 :(得分:1)
对于收件人的格式,smtp4dev比大多数实际的SMTP中继有点挑剔。
不起作用:
helo me
mail from: me@here.com
rcpt to: somebody@mydomain.tld
使用:
helo me
mail from: me@here.com
rcpt to: <somebody@mydomain.tld>