Check email addresses for validity
use Mail::CheckUser qw(check_email); my $ok = check_email($email_addr); use Mail::CheckUser qw(:constants check_email last_check) my $ok = check_email($email_addr); print "DNS timeout\n" if last_check()->{code} == CU_DNS_TIMEOUT; use Mail::CheckUser; my $res = Mail::CheckUser::check_email($email_addr);
This Perl module provides routines for checking validity of email address.
It makes several checks:
It checks the syntax of an email address.
It checks if there any \s-1MX\s0 records or A records for the domain part of the email address.
It tries to connect to an email server directly via \s-1SMTP\s0 to check if mailbox is valid. Old versions of this module performed this check via the \s-1VRFY\s0 command. Now the module uses another check; it uses a combination of \s-1MAIL\s0 and \s-1RCPT\s0 commands which simulates sending an email. It can detect bad mailboxes in many cases.
If is possible to turn off some or all networking checks (items 2 and 3). See \*(L"\s-1GLOBAL\s0 \s-1VARIABLES\s0\*(R".
This module was designed with CGIs (or any other dynamic Web content programmed with Perl) in mind. Usually it is required to quickly check e-mail addresses in forms. If the check can't be finished in reasonable time, the e-mail address should be treated as valid. This is the default policy. By default if a timeout happens the result of the check is treated as positive. This behavior can be overridden - see \*(L"\s-1GLOBAL\s0 \s-1VARIABLES\s0\*(R".
In many cases there is no way to detect the validity of email addresses with network checks. For example, non-monolithic mail servers (such as Postfix and qmail) often report that a user exists even if it is not so. This is because in cases where the work of the server is split among many components, the \s-1SMTP\s0 server may not know how to check for the existence of a particular user. Systems like these will reject mail to unknown users, but they do so after the \s-1SMTP\s0 conversation. In cases like these, the only absolutely sure way to determine whether or not a user exists is to actually send a mail and wait to see if a bounce messages comes back. Obviously, this is not a workable strategy for this module. Does it mean that the network checks in this module are useless? No. For one thing, just the \s-1DNS\s0 checks go a long way towards weeding out mistyped domain parts. Also, there are still many \s-1SMTP\s0 servers that will reject a bad address during the \s-1SMTP\s0 conversation. Because of this, it's still a useful part of checking for a valid email address. And this module was designed such that if there is exists possibility (however small) that the email address is valid, it will be treated as valid by this module.
Another warning is about $Mail::CheckUser::Treat_Timeout_As_Fail global variable. Use it carefully - if it is set to true then some valid email addresses can be treated as bad simply because an \s-1SMTP\s0 or \s-1DNS\s0 server responds slowly.
Another warning is about $Mail::CheckUser::Treat_Full_As_Fail global variable. Use it carefully - if it is set to true then some valid email addresses can be treated as bad simply because their mailbox happens to be temporarily full.
This simple script checks if email address \*(C`[email protected]\*(C' is valid.
use Mail::CheckUser qw(check_email last_check);
my $email = '[email protected]';
if(check_email($email)) { print "E-mail address <$email> is OK\n"; } else { print "E-mail address <$email> isn't valid: ", last_check()->{reason}, "\n"; }
Validates email address $email. Return true if email address is valid and false otherwise. Returns detailed result of last check made with \*(C`check_email\*(C' as hash reference: { ok => OK, code => CODE, reason => REASON }
True if last checked email address is valid. False otherwise.
A number which describes result of last check. See \*(L"\s-1CONSTANTS\s0\*(R".
A string which describes result of last check.
Constants used by \*(C`last_check\*(C' to describe result of last check can be exported with
use Mail::CheckUser qw(:constants)
List of all defined constants:
Check is successful.
Bad syntax of email address.
Mail domain mentioned in email address is unknown.
Timeout has happen during \s-1DNS\s0 checks.
User is unknown on \s-1SMTP\s0 server.
Timeout has happen during \s-1SMTP\s0 checks.
All \s-1SMTP\s0 servers for mail domain were found unreachable during \s-1SMTP\s0 checks.
Mailbox is temporarily full but probably a valid username.
It is possible to configure \*(C`check_email\*(C' using the global variables listed below. If true then do only syntax checks. By default it is false. If it is true then do not try to connect to mail server to check if a user exists. If this is true, and $Mail::CheckUser::Skip_Network_Checks is false, only syntax and \s-1DNS\s0 checks are performed. By default it is false. By default Net::Ping is used to determine remote reachability of \s-1SMTP\s0 servers before doing \s-1SMTP\s0 checks. Setting this to true skips this check. By default it is false. \s-1MAIL/RCPT\s0 check needs an email address to use as the 'From' address when performing its checks. The default value is \*(C`[email protected]\*(C'. Sender domain used in \s-1HELO\s0 \s-1SMTP\s0 command. If undef Net::SMTP is allowed to use its default value. By default it is undef.
Timeout in seconds for network checks. By default it is 60. If it is true \*(C`Mail::CheckUser\*(C' treats checks that time out as failed. By default it is false. If it is true \*(C`Mail::CheckUser\*(C' treats error \*(L"552 mailbox full\*(R" as an invalid email and sets \s-1CU_MAILBOX_FULL\s0. By default it is false. Override with customized Net::DNS::Resolver object. This is used to lookup \s-1MX\s0 and A records for the email domain when network checks are enabled. If undef, Net::DNS::Resolver->new will be used. The default value is undef. If it is true then enable debug output on \*(C`STDERR\*(C'. By default it is false.
Ilya Martynov [email protected]
Rob Brown [email protected]
Module maintained at Source Forge ( http://sourceforge.net/projects/mail-checkuser/ ).
Copyright (c) 1999-2003 by Ilya Martynov. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
$Id: CheckUser.pm,v 1.46 2003/09/18 23:51:36 hookbot Exp $
perl\|(1).