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;
$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.

    解决 无用
    打赏 举报

相关推荐 更多相似问题