[drupal-devel] [feature] Better validation of new users email
address when creating account
Karsten Müller
kmue at mac.com
Thu Feb 24 17:11:56 UTC 2005
I drupal'ized the validate_email function found at
http://yser.de/validate_email_php.tar.gz . The attached "module" could
be used or check_email() can be integrated into user.module.
Unfortunately the PHP getmxrr() is not implemented on Windows platform.
My user_validate_mail() in user.module looks like this:
function user_validate_mail($mail) {
global $base_url;
if (!$mail) return t('You must enter an e-mail address.');
if (!valid_email_address($mail)) {
return t('The e-mail address %mail is not valid.', array('%mail' =>
"<em>$mail</em>"));
}
else if (function_exists('check_email')) {
$host = parse_url($base_url);
$host = $host['host'];
if ($check = check_email($mail,$host)) {
if ($check[1] != 1) {
return t('The check of e-mail address %mail returns:<br
/>%res', array('%mail' => "<em>$mail</em>",'%res' => "$check[0]"));
}
}
}
}
Cheers,
Karsten
-------------- next part --------------
<?php
// $Id$
define('CHECK_EMAIL_DEBUG', 1);
/**
* Implementation of hook_help().
*/
function check_email_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Provides a function for checking email addresses.');
}
}
/* Id: chk_mail.php3,v 1.10 2001/02/22 14:43:38 ele Exp $ */
/*
* check_mail: PHP-function for checking an email-address
* Copyright (C) 2001 Gabriele Kayser <k at yser.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* GPL is found at: http://www.gnu.org/copyleft/gpl.html
*
* Usage: check_email($email,$HTTP_SERVER_VARS[SERVER_NAME])
* Returnvalues: array($message,$success,$server_answer)
* Message: reason for failure or empty
* Success: 0 = failed, no MX-Record for Domain;
* -1 = failed;
* 1 = success
* Server Answer: only if exists
*/
/**
* Drupalized by kmue at drupal.org
*/
function check_email($email,$sender_domain) {
list($user, $domain) = split('@', $email, 2);
$sender = "info@" .$sender_domain;
$success = -1;
$connect_success = "";
$connect_out = "";
$server_answer = "";
/* returnmessages */
$dns_failed = t('Ihre e-Mail Adresse ist nicht korrekt!<br />Es konnte kein Mailserver für <strong>%domain</strong> gefunden werden.<br />Bitte Überprüfen Sie Ihre Eingabe.', array('%domain' => $domain));
$connect_failed = t('Kein Mailserver für <strong>%email</strong> erreichbar<br />Ihre e-Mail Adresse konnte nicht Überprüft werden.', array('%email' => $email));
$helo_failed = t('HELO for %domain failed', array('%sender_domain' => $sender_domain));
$from_failed = t('MAIL FROM: %sender failed', array('%sender' => $sender));
$rcpt_failed = t('Der Benutzer <strong>%user</strong> ist bei <strong>%domain unbekannt</strong>!<br />Bitte Überprüfen Sie Ihre Eingabe.', array('%user' => $user, '%domain' => $domain));
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("email: $email, sender_domain: $sender_domain, domain: $domain");
}
/* check input */
if (!$email) {
return array(t('Internal error: $email is missing'),-1,'');
}
else if (!$sender_domain) {
return array(t('Internal error: $sender_domain is missing'),-1,'');
}
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("Input OK");
}
/* exists mxrr for $domain */
if (!getmxrr($domain,$mxhosts,$weight)) {
$message = $dns_failed;
$success = 0;
} else {
$i = 0; /* order mxhosts by weightness */
while ($i < sizeof($mxhosts)) {
$hosts[$weight[$i]][$i] = $mxhosts[$i];
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("MX RR for $domain $i: $weight[$i] * $mxhosts[$i]");
}
$i++;
}
ksort($hosts);
$h = 0;
$i = 0;
while (list($key,$value) = each($hosts)) {
$mxhosts[$i] = $value;
$weight[$i] = $key;
while (list($key_name,$hostname) = each($mxhosts[$i])) {
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("$i-$h: $key_name: $weight[$i] * $hostname");
}
$mx_sorted[$h] = $hostname;
$weight_sorted[$h] = $weight[$i];
$h++;
}
$i++;
}
$mxhosts = $mx_sorted;
$weight = $weight_sorted;
unset($weight_sorted);
unset($mx_sorted);
unset($hosts); /* end order mxhosts by weightness */
/* connect */
for ($i = 0; ($i < count ($mxhosts) && $connect_success <> 1); $i++) {
$smtp_socket = fsockopen ($mxhosts[$i], 25);
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("Ask-$i: $mxhosts[$i] * $weight[$i]");
}
/* SMTP-Socket open */
if ($smtp_socket) { /* ----------------------------------------------- SMTP-Socket */
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("Socket $smtp_socket open");
}
$s = 0;
$c = 0;
$out = "";
socket_set_blocking ($smtp_socket, false);
do {
$out = fgets ($smtp_socket, 2500);
if (ereg ( "^220", $out)) {
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("S:$s C:$c OUT: $out");
}
$connect_out = $out;
$s = 0;
$out = "";
$c++;
} elseif (($c > 0) && ($out == "")) {
break;
} else {
$s++;
}
if ($s == 99999) {
break;
}
} while ($out == "");
if (!ereg("^220",$connect_out)) {
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("No Connection to $mxhosts[$i] ($connect_out)");
}
$message = $connect_failed;
$server_answer = htmlentities($connect_out);
fclose($smtp_socket);
} else { /* start communication ---------------------------------- HELO */
$connect_success = 1;
socket_set_blocking ($smtp_socket, true);
$helo_success = 0;
fputs ($smtp_socket, "HELO $sender_domain\r\n");
$z = 0;
$helo_output = fgets ($smtp_socket, 2000);
if (ereg("^250",$helo_output)) { $helo_success = 1; }
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("$z: HELO SENT: $sender_domain");
drupal_set_message("$z: HELO GET: $helo_output");
}
socket_set_blocking ($smtp_socket, false);
do {
$helo_output = fgets ($smtp_socket, 2000);
$z++;
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("$z: HELO GET: $helo_output");
}
if ($z > 10) { break; }
} while (ereg("^[a-zA-Z0-9]",$helo_output)); /* ------------ end HELO */
socket_set_blocking ($smtp_socket, true);
if ($helo_success == 1) { /* ------------- helo_success => MAIL FROM */
fputs ($smtp_socket,'MAIL FROM: <'. $sender .">\r\n");
$from_output = fgets ($smtp_socket, 2000);
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("MAIL FROM SENT: <$sender>");
drupal_set_message("MAIL FROM GET: $from_output");
}
if (ereg("^250",$from_output)) { /* from_success => RCPT TO */
fputs ($smtp_socket, "RCPT TO: <$email>\r\n");
$rcpt_output = fgets ($smtp_socket, 2000);
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("RCPT TO SENT: <$email>");
drupal_set_message('RCPT TO GET: '. htmlentities($rcpt_output));
}
if (ereg("^250",$rcpt_output)) {
$success = 1;
$message = "";
} else {
$message = $rcpt_failed;
$server_answer = htmlentities($rcpt_output);
}
} else { /* ---------------------- end from_success RCPT TO */
$message = $from_failed;
$server_answer = htmlentities($from_output);
} /* end if (ereg("^250",$from_output) ------------- MAIL FROM */
} else {
$message = $helo_failed;
$server_answer = htmlentities($helo_output);
} /* end if $helo_success */
} /* end if ^220 $connect_out */
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("Send: QUIT");
}
fputs($smtp_socket,"QUIT\r\n");
if (CHECK_EMAIL_DEBUG > 0) {
drupal_set_message("Close $smtp_socket");
}
fclose($smtp_socket);
} /* end if smtp_socket open ----------------------------------------- SMTP-Socket */
} /* end for connect $mxhosts[$i] */
} /* end exists mxrr for $domain */
return array($message,$success,$server_answer);
}
?>
More information about the drupal-devel
mailing list