Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Email Validation

by Anonymous Monk
on Nov 14, 2001 at 16:24 UTC ( #125298=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I recently developed a script for someone that I host on my server, they run a site which allows users to enter their email address to signup for a contest, At the moment I only have a very simple regex to validate the syntax of the email - this contest site gets about 1,000 signups a day, of which of the emails the server sends out, I'm getting about 50 bounce and just sit on the server waiting for a retry, mainly this appears to be due to users spelling their email address wrong..

Then I stumbled upon Email::Valid yesterday which appears to also do an mx check on a domain, my concern with so many emails to check that this will cause a very high load on the server..
Anyone have any opinions?

Comment on Email Validation
Re: Email Validation
by rob_au (Abbot) on Nov 14, 2001 at 16:48 UTC
    While it is unlikely that MX domain checks alone will cause high server load (depending on the network and DNS configuration), you can disable this functionality within Email::Valid by setting the parameter -mxcheck to 0 - This allows you to merely check the email as valid with regard to RFC822 specification or alike. eg.

    print (Email::Valid->address( -address => 'email@address.com', -mxcheck => 0 ) ? 'yes' : 'no');

    With regard to the number of email addresses getting bounced and ending up in retry or bad message queues, it may be worth either modifying your MTA configuration to minimise retries before delivery failure or rewriting the mail code to attempt SMTP delivery to the lowest preference mail exchanger directly and discarding the message if delivery is not achieved within a set number of attempts. You could also set up a mailbox directed to /dev/null for bounce messages.

    There is of course a balance which you will be able to find between server load elevated by Email::Valid MX domain lookups and repeated MTA delivery attempts. My guess is that the load increase attributable to MX domain lookups will be negligible.

    Good luck.

     

    Ooohhh, Rob no beer function well without!

Re: Email Validation
by Kage (Scribe) on Nov 14, 2001 at 21:53 UTC
    I found the absolute beth method of doing an E-Mail Validation is just to push the email address into a variable and do this:
    if ($form{'email'} !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z0-9]+)(\]?) +$/) { $invalid="true"; } else { $invalid="false"; }
    This checks to make sure it has a @, has text before the @, text after the @, and a . after the @, and checks to make sure it is only alphanumeric charactors and no spaces. Very efficient.
      This regex fails for many valid addresses and it will pass on some invalid ones. Try:
      postmaster@localhost (valid) @@domain.com (invalid) "my name" <myaddress@mydomain.com> (valid) "my name with spaces"@mydomain.com (valid) me@--.com (invalid)
      This checks to make sure it has a @, has text before the @, text after the @, and a . after the @, and checks to make sure it is only alphanumeric charactors and no spaces
      This is not the correct definition of a valid email address. Check RFC822 for the full specification.

      For me, the best method is to try sending an email to the address. If you don't want to do that then I'd follow the advice of the other posters, who suggest Mastering Regular Expressions, Email::Valid / RFC::RFC822::Address / Mail::Address or others.

      Update: I would also look at the FAQ: How do I check a valid mail address?

      Simon Flack ($code or die)
      $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
      =~y'_"' ';eval"die";print $_,lc substr$@,0,3;
Re: Email Validation
by McD (Chaplain) on Nov 14, 2001 at 22:15 UTC
    Email::Valid is probably your best bet - any of the less rigourous methods proposed are simply biding time until they fail to validate an otherwise perfectly functional address.

    Email addresses are suprisingly complicated beasties - Mastering Regular Expressions has a regex to match valid addresses which will curl your hair - you can find it here.

    But in any case, you need to deal with failure notices - any mail, even mail to valid addresses, can fail to deliver for a variety of reasons.

    Peace,
    -McD

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://125298]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-09-21 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (169 votes), past polls