Perl 101: Things Every Perl Programmer Should Know.

Email Addressing, Processing, and Handling

How do I see if an email address is valid?

In general, you can't. There are several methods available to see if it looks reasonable, but there is no way to determine if an address is actually deliverable without actually attempting delivery.

Using regular expressions:

    # Match basically blah@blah.blah
    if ( $addr =~ /^\S+\@\S+\.\S+$/ ) {
        print "Looks OK";
    }

If you're doing any real work, you may wish to look to one of the modules available on http://search.cpan.org, such as:

Email::Address
Email::Valid

Email::Valid makes it easy to determine if an email address is well-formed:

    use Email::Valid;

    print ( Email::Valid->address( 'someone@gmail.com' ) ? 'Yes' : 'No' ); # prints Yes
    print ( Email::Valid->address( 'someone#gmail.com' ) ? 'Yes' : 'No' ); # prints No

Email::Valid can also tell you why an address is invalid:

    print "Invalid address: $Email::Valid::Details \n"
        unless Email::Valid->address( 'you#foo.bar' );

Want to contribute?

Submit a PR to github.com/petdance/perl101


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.