douzhanjia0773 2014-04-10 18:18
浏览 33
已采纳

使用PHP PEAR Mail类检查域是否存在

I noticed that my server has been returning this error when trying to send email to an invalid domain:

Standard Message:   Failed to set sender: user@invaliddomain.coom [SMTP: Invalid response code received from server (code: 553, response: 5.1.8 ... Domain of sender address user@invaliddomain.coom does not exist)]
Standard Code:  10004
DBMS/User Message:  
DBMS/Debug Message:

Is there a way to check the domain first before attempting to send the email? I have a feeling I could also handle this on the SMTP server end by squelching this error, but I like the idea of being able to test an email domain first before sending it. Thanks for your ideas!

Here is the pertinent code just for reference (variables are filtered in from a form):

$headers['To'] = $to_address;
$headers['From'] = $from;
$headers['Reply-To'] = $from;
$headers['Subject'] = $subject;
$this->setHTMLBody($body);
$body = $this->get(array('text_charset' => 'utf-8'));
$headers = $this->headers($headers, true);
$message =& Mail::factory('smtp');
$mail = $message->send($to_address,$headers,$body);
  • 写回答

1条回答 默认 最新

  • dongshixingga7900 2014-04-10 20:31
    关注

    You could use Net_DNS2 to determine if the domain exists and if so, send the email on it's merry way.

    include "Net/DNS2.php";
    $r = new Net_DNS2_Resolver();            
    try {
        $result = $r->query($domain, 'MX');    
    } catch(Net_DNS2_Exception $e) {
        $result = null;         
    }
    if ($result !== null) {
        // send email...
    }
    

    Naturally, I'd suggest some level of caching so you aren't repeating lookups.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加