| 1 |
<?php
|
| 2 |
// $Id: email_verify.inc.php,v 1.1 2009/02/04 22:01:18 dbr Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Check the email for email_verify module.
|
| 6 |
*/
|
| 7 |
function _email_verify_check($mail) {
|
| 8 |
if (!valid_email_address($mail)) {
|
| 9 |
// The address is syntactically incorrect.
|
| 10 |
// The problem will be caught by the 'user' module anyway, so we avoid
|
| 11 |
// duplicating the error reporting here, just return.
|
| 12 |
return;
|
| 13 |
}
|
| 14 |
|
| 15 |
$host = substr(strchr($mail, '@'), 1);
|
| 16 |
|
| 17 |
// Let's see if we can find anything about this host in the DNS
|
| 18 |
if (!checkdnsrr($host, 'ANY')) {
|
| 19 |
return t('Email host %host invalid, please retry.', array('%host' => "$host"));
|
| 20 |
}
|
| 21 |
|
| 22 |
// What SMTP servers should we contact?
|
| 23 |
$mx_hosts = array();
|
| 24 |
if (!getmxrr($host, $mx_hosts)) {
|
| 25 |
// When there is no MX record, the host itself should be used
|
| 26 |
$mx_hosts[] = $host;
|
| 27 |
}
|
| 28 |
|
| 29 |
// Try to connect to one SMTP server
|
| 30 |
foreach ($mx_hosts as $smtp) {
|
| 31 |
|
| 32 |
$connect = @fsockopen($smtp, 25, $errno, $errstr, 15);
|
| 33 |
|
| 34 |
if (!$connect) continue;
|
| 35 |
|
| 36 |
if (ereg("^220", $out = fgets($connect, 1024))) {
|
| 37 |
// OK, we have a SMTP connection
|
| 38 |
break;
|
| 39 |
}
|
| 40 |
else {
|
| 41 |
// The SMTP server probably does not like us
|
| 42 |
// (dynamic/residential IP for aol.com for instance)
|
| 43 |
// Be on the safe side and accept the address, at least it has a valid
|
| 44 |
// domain part...
|
| 45 |
watchdog('email_verify', "Could not verify email address at host $host: $out");
|
| 46 |
return;
|
| 47 |
}
|
| 48 |
}
|
| 49 |
|
| 50 |
if (!$connect)
|
| 51 |
return t('Email host %host is invalid, please contact us for clarification.', array('%host' => "$host"));
|
| 52 |
|
| 53 |
$from = variable_get('site_mail', ini_get('sendmail_from'));
|
| 54 |
|
| 55 |
// Extract the <...> part if there is one
|
| 56 |
if (preg_match('/\<(.*)\>/', $from, $match) > 0) {
|
| 57 |
$from = $match[1];
|
| 58 |
}
|
| 59 |
|
| 60 |
$localhost = $_SERVER["HTTP_HOST"];
|
| 61 |
if (!$localhost) // Happens with HTTP/1.0
|
| 62 |
//should be good enough for RFC compliant SMTP servers
|
| 63 |
$localhost = 'localhost';
|
| 64 |
|
| 65 |
fputs($connect, "HELO $localhost\r\n");
|
| 66 |
$out = fgets($connect, 1024);
|
| 67 |
fputs($connect, "MAIL FROM: <$from>\r\n");
|
| 68 |
$from = fgets($connect, 1024);
|
| 69 |
fputs($connect, "RCPT TO: <{$mail}>\r\n");
|
| 70 |
$to = fgets($connect, 1024);
|
| 71 |
fputs($connect, "QUIT\r\n");
|
| 72 |
fclose($connect);
|
| 73 |
|
| 74 |
if (!ereg ("^250", $from)) {
|
| 75 |
// Again, something went wrong before we could really test the address,
|
| 76 |
// be on the safe side and accept it.
|
| 77 |
watchdog('email_verify', "Could not verify email address at host $host: $from");
|
| 78 |
return;
|
| 79 |
}
|
| 80 |
|
| 81 |
if (
|
| 82 |
// This server does not like us
|
| 83 |
// (noos.fr behaves like this for instance)
|
| 84 |
ereg("(Client host|Helo command) rejected", $to) ||
|
| 85 |
|
| 86 |
// Any 4xx error also means we couldn't really check
|
| 87 |
// except 450, which is explcitely a non-existing mailbox:
|
| 88 |
// 450 = "Requested mail action not taken: mailbox unavailable"
|
| 89 |
ereg("^4", $to) && !ereg("^450", $to)) {
|
| 90 |
|
| 91 |
// In those cases, accept the email, but log a warning
|
| 92 |
watchdog('email_verify', "Could not verify email address at host $host: $to");
|
| 93 |
return;
|
| 94 |
}
|
| 95 |
|
| 96 |
if (!ereg ("^250", $to)) {
|
| 97 |
watchdog('email_verify', "Rejected email address: $mail. Reason: $to");
|
| 98 |
return t('%mail is invalid, please contact us for clarification.', array('%mail' => "$mail"));
|
| 99 |
}
|
| 100 |
|
| 101 |
// Everything OK
|
| 102 |
return;
|
| 103 |
}
|