There's a number of email address validation modules on CPAN, e.g. Mail::RFC822::Address, RFC::RFC822::Address, Email::Valid and Mail::Address (which will attempt to extract an email address). None of these cover all the weird little corner cases.

The best tool I've found for this job is isemail, which comes with a very comprehensive test suite designed to test all those corner cases. Unfortunately it's for PHP, not Perl.

Then again you need to ask yourself what you want to do in the first place. Do you want to make sure that an email address is syntactically valid, i.e. conforming to the stipulations of all the relevant RFCs? Or do you want to make sure that an email address works, i.e. that mail sent to it will actually be received?

In practice it's almost always the latter, and your best bet then is to not bother with the RFCs at all and instead simply try to send an email to that address. An email address may be syntactically invalid, yet still work "in the wild". What's more, an email address that IS syntactically valid may not actually work; mail sent to that address may never get read by the intended recipient, or any person at all.

Here's two articles arguing this point.


In reply to Re: input - E-mail address - how to check string ? by AppleFritter
in thread input - E-mail address - how to check string ? by zalezny

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.