Topics

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' );

We want your feedback

If we can improve perl101.org in any way, please let us know with this form.

Your name
Your email
Your comment