dongpaocuan7498 2011-08-29 07:15
浏览 163
已采纳

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

Cannot understand why I am getting this error - both to & from email addresses are valid (I use them daily) so cannot figure out how this is happening - any help would be appreciated.

NOTE: This is working in production but is throwing errors in dev. I have stricter config in dev. NOTE: I am testing locally on PC using 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' . "
";
    $headers .= 'To: '. $to . "
";
    $headers .= 'Cc: '. $cc . "
";
    $headers .= 'From: '. $from . "
";
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"

";
    $headers .= "This is a multi-part message in MIME format.
";
    $headers .= "--".$uid."
";
    $headers .= "Content-type:text/html; charset=iso-8859-1
";
    $headers .= "Content-Transfer-Encoding: 7bit

";
    $headers .= $message."

";
    $headers .= "--".$uid."
";
    $headers .= "Content-Type: application/pdf; name=\"".$filename."\"
";
    $headers .= "Content-Transfer-Encoding: base64
";
    $headers .= "Content-Disposition: attachment; filename=\"".$filename."\"

";
    $headers .= $content."

";
    $headers .= "--".$uid."--";

    if (mail($to, $subject, $message, $headers)) {
      print "SUCCESS";
    } else {
      print "FALIED";
    }

Here is the result if I print the variables on the mail() line:

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--)
  • 写回答

2条回答 默认 最新

  • duandang2838 2011-09-01 06:07
    关注

    (Author of smtp4dev here)

    This is an updated answer - I couldn't originally reproduce the problem with the code you posted, but I noticed that your second bit of output shows that sometimes you don't have a CC address.

    This code reproduces the problem and results in the 501 error.

    <?php
        $to = 'myemail@mydomain.com.au';   
        $cc = '';  
        $from = 'myemail@mydomain.com.au';
        $headers .= 'To: '. $to . "
    ";
        $headers .= 'Cc: '. $cc . "
    ";
        $headers .= 'From: '. $from . "
    ";
    
        mail($to, $subject, $message, $headers);
    ?>
    

    On Windows the PHP mail function looks at the message headers for TO/CC addresses and converts each header to a SMTP RCPT TO command. Unfortunately it does this even if the header has no value:

    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
    

    The fix is therefore is that you need to not include the CC: header at all if you don't have a CC address to send to:

    <?php
        $to = 'myemail@mydomain.com.au';   
        $cc = '';  
        $from = 'myemail@mydomain.com.au';
        $headers .= 'To: '. $to . "
    ";
    
        if ($cc) {
            $headers .= 'Cc: '. $cc . "
    ";
        }
        $headers .= 'From: '. $from . "
    ";
    
        mail($to, $subject, $message, $headers);
    ?>
    

    This worked fine for me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献