SMTP服务器响应:501必须指定地址<address>


SMTP server response: 501 Must specify to address &lt;address&gt;

无法理解为什么我得到这个错误-都到&从电子邮件地址是有效的(我每天使用),所以不能弄清楚这是怎么发生的-任何帮助将不胜感激。

注意:这在生产中工作,但在开发中抛出错误。我在开发中有更严格的配置。注意:我在本地PC上使用smtp4dev

进行测试
    $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--)

(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:标头:

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

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>