in reply to input - E-mail address - how to check string ?

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.

  • Comment on Re: input - E-mail address - how to check string ?

Replies are listed 'Best First'.
Re^2: input - E-mail address - how to check string ?
by Discipulus (Canon) on Feb 25, 2015 at 12:51 UTC
    instead of 'try to send an email to that address' you can, strip the domain part, lookup that domain for MX record, establish an SMTP sesssion and issue a "RCPT TO" or "VRFY" command and wait for a '200 OK' answer. Less spammy.

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      establish an SMTP sesssion and issue a "RCPT TO" or "VRFY" command and wait for a '200 OK' answer.

      In our days, no sanely configured internet mail server will expose its account list by giving a clear positive or negative response to the VRFY or RCPT TO commands. Typically, the answer to VRFY is something like "I won't tell you" (my local Exim answers "252 Administrative prohibition"), and RCPT TO will be accepted no matter what mail account you try. The mailserver will decide after(!) the SMTP dialog how to handle that mail, and may send back a "mail not deliverable" mail. That may take hours or days, depending on the mail server configuration (greylisting).

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)