سؤال

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);
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top