function email_verify_check in Email Verify 5
Same name and namespace in other branches
- 6 email_verify.module \email_verify_check()
- 7.2 email_verify.module \email_verify_check()
- 7 email_verify.module \email_verify_check()
Verifies whether the given mail address exists.
Parameters
mail the email address to verify: @return NULL if the address exists, as far as we could check. an error message if we found a problem with the address
2 calls to email_verify_check()
- email_verify_checkall in ./
email_verify.module - Look though the whole user base for invalid emails. Can be very long when hosts timeout.
- email_verify_edit_validate in ./
email_verify.module
File
- ./
email_verify.module, line 48
Code
function email_verify_check($mail) {
if (!valid_email_address($mail)) {
// The address is syntactically incorrect.
// The problem will be caught by the 'user' module anyway, so we avoid
// duplicating the error reporting here, just return.
return;
}
$host = substr(strchr($mail, '@'), 1);
// Let's see if we can find anything about this host in the DNS
if (!checkdnsrr($host, 'ANY')) {
return t('Email host %host does not seem valid', array(
'%host' => "{$host}",
));
}
// What SMTP servers should we contact?
$mxHosts = array();
if (!getmxrr($host, $mxHosts)) {
// When there is no MX record, the host itself should be used
$mxHosts[] = $host;
}
// Try to connect to one SMTP server
foreach ($mxHosts as $smtpServer) {
$connect = @fsockopen($smtpServer, 25, $errno, $errstr, 15);
if (!$connect) {
continue;
}
if (ereg("^220", $out = fgets($connect, 1024))) {
// OK, we have a SMTP connection
break;
}
else {
// The SMTP server probably does not like us
// (dynamic/residential IP for aol.com for instance)
// Be on the safe side and accept the address, at least it has a valid
// domain part...
watchdog('email_verify', "Could not verify email address at host {$host}: {$out}");
return;
}
}
if (!$connect) {
return t('Email host %host does not seem valid, it does not answer', array(
'%host' => "{$host}",
));
}
$from = variable_get('site_mail', ini_get('sendmail_from'));
// Extract the <...> part if there is one
if (preg_match('/\\<(.*)\\>/', $from, $match) > 0) {
$from = $match[1];
}
$localhost = $_SERVER["HTTP_HOST"];
if (!$localhost) {
// Happens with HTTP/1.0
//should be good enough for RFC compliant SMTP servers
$localhost = 'localhost';
}
fputs($connect, "HELO {$localhost}\r\n");
$out = fgets($connect, 1024);
fputs($connect, "MAIL FROM: <{$from}>\r\n");
$from = fgets($connect, 1024);
fputs($connect, "RCPT TO: <{$mail}>\r\n");
$to = fgets($connect, 1024);
fputs($connect, "QUIT\r\n");
fclose($connect);
if (!ereg("^250", $from)) {
// Again, something went wrong before we could really test the address,
// be on the safe side and accept it.
watchdog('email_verify', "Could not verify email address at host {$host}: {$from}");
return;
}
if (ereg("(Client host|Helo command) rejected", $to)) {
// This server does not like us, accept the email address
// (noos.fr behaves like this for instance)
watchdog('email_verify', "Could not verify email address at host {$host}: {$to}");
return;
}
if (!ereg("^250", $to)) {
watchdog('email_verify', "Rejected email address: {$mail}. Reason: {$to}");
return t('Email address %mail does not seem valid', array(
'%mail' => "{$mail}",
));
}
// Everything OK
return;
}