sweetblood has asked for the wisdom of the Perl Monks concerning the following question:

I need to verify that a variable has a valid email address format or set a default, otherwise sendmail rejects the email. So I'm using a rudimentary regex to validate the variable. I'm just not sure if it's a reasonable test.
Here's what I'm doing:
$from_addr = 'me@here.com' unless ($from_addr =~ /^\w.*?@.*?\..*$/);
Does this make sense?

TIA

20040408 Edit by BazB: Changed title from 'Yet Another RE for email address'

Replies are listed 'Best First'.
Re: Yet Another Email Validation question
by gryphon (Abbot) on Apr 06, 2004 at 15:48 UTC

    Greetings sweetblood,

    Check out Email::Valid. I'm a big fan of letting someone else do my work for me, and the module looks fast.

    use Email::Valid; my $valid_email = (Email::Valid->address($unverified_email)) ? $unverified_email : undef;

    gryphon
    code('Perl') || die;

    20040408 Edit by BazB: Changed title from 'Re: yare for email address'

      Yes, this looks good. My only problem is I won't be able to get it installed for quite awhile as the company has to do a review prior to installing any modules. This is a lengthy process. This is why I'm opting for a regex method.

      Thanks!

        The company has to review any newly installed module? Sensible. But what about your code, doesn't that have to be reviewed? What if you just make Email::Valid part of your application? Or just copy and paste the relevant code? How long did your company take to review perl itself?

        Anyway, your regex is just too trivial to take serious. It would fail on every email address I create for registrating at a website. Rejecting email addresses that don't start with a \w character is as useful as rejecting street names that don't start with a vowel. You're rejecting perfectly valid email addresses, and on the other hand, you are accepting anything that starts with a \w character, and contains a @ and a dot. Which would mean you'd accept the text of this posting as a valid address, yet reject "this is valid"@example.com.

        Abigail

Re: Yet Another Email Validation question
by Roy Johnson (Monsignor) on Apr 06, 2004 at 16:02 UTC
    You might try stealing the code from Email::Valid, if you can't install it. Otherwise, you just need to consider what constitutes a "valid" email address format for the purposes of your application.

    At the very least, I think you want to change the dots in your regex to \S.


    The PerlMonk tr/// Advocate
      Excellent suggestion!

      Thanks!

Re: Yet Another Email Validation question
by waswas-fng (Curate) on Apr 06, 2004 at 16:11 UTC
    You could always look at the source for Email::Valid and incorperate it directly into your app. Just for reference the regex used in that module is a wee bit more complex than yours:


    -Waswas
    A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.