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.

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

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导