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

I am trying to verify all the E-mail addresses in my database without sending any actual e-mails. I'm using Email::Verify::SMTP, which seems to work, except that it reports EVERY e-mail address as invalid. Below is the actual subroutine (validate_EMAIL) preceded by some pseudo code showing how I invoke the subroutine.

The $EMAIL_ADDRESS field of each $MEMBER_rec will contain at least one address and may contain multiple addresses delimited by a semicolon. The subroutine will break this scalar into individual addresses and validate each one separately, then report the whole block back to the main section.

The original author is no longer reachable via e-mail, so I'm looking for help from anyone who has had success with this package.

## psuedo code: use Email::Verify::SMTP; $f3 = "font size=3"; $f0 = "/font"; foreach $MEMBER_rec (@MEMBER_data) { (undef,$EMAIL_ADDRESS,undef, ...) = splip(/\|/,$MEMBER_rec); &validate_EMAIL; print STDOUT "$EMAIL_ADDRESS\n"; } exit; sub validate_EMAIL { # This is important: $Email::Verify::SMTP::FROM = 'webmaster@$hostnm'; $return_EMAIL = ""; $tmp_EMAIL = $EMAIL_ADDRESS; $tmp_EMAIL =~ s/\s//g; # Get rid of any whitespace. @tmp_EMAIL = split(/;/,$tmp_EMAIL); foreach $tmp_EMAIL_lc (@tmp_EMAIL) { $tmp_EMAIL_lc =~ tr/A-Z/a-z/; # Find out if, and why not (if not): my ($is_valid, $msg) = verify_email('$tmp_EMAIL_lc'); if( $is_valid ) # Email is valid: $return_EMAIL .= "; <$f3>$tmp_EMAIL_lc<$f0>"; } else { # Email is *not* valid: $return_EMAIL .= "; <$f3 color=RED>$tmp_EMAIL_lc<$f0>"; } } $return_EMAIL =~ s/^; //; $return_EMAIL =~ s/; /<br>/g; $EMAIL_ADDRESS = $return_EMAIL; }

Replies are listed 'Best First'.
Re: Verifying Email Addresses
by choroba (Cardinal) on Apr 04, 2019 at 15:04 UTC
    Use Email::Address::XS to validate email addresses. It conforms best to the specifications and is fast.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Verifying Email Addresses
by AnomalousMonk (Archbishop) on Apr 04, 2019 at 17:25 UTC
    # Find out if, and why not (if not):
    my ($is_valid, $msg) = verify_email('$tmp_EMAIL_lc');

    One possible reason the  $tmp_EMAIL_lc e-mail address is always bad is that the single-quotes of the expression  '$tmp_EMAIL_lc' do not interpolate, so what is passed to  verify_email() is always the literal string '$tmp_EMAIL_lc'.

    c:\@Work\Perl\monks>perl -wMstrict -le "my $tmp_EMAIL_lc = 'this will not interpolate'; print '>$tmp_EMAIL_lc<'; " >$tmp_EMAIL_lc<
    Use double-quotes or, better still, no quotes at all.


    Give a man a fish:  <%-{-{-{-<

      We might be getting closer. Now the response is "Cannot open socket to 'some-host-name'" and each 'some-host-name' is different.

        # This is important:
        $Email::Verify::SMTP::FROM = 'webmaster@$hostnm';

        I just noticed this statement from the OP. I haven't looked at Email::Verify::SMTP to figure out what setting this variable is supposed to do, but I just want to point out another potential single-quote vs double-quote interpolation problem.

        If the quoted statement is correct as it stands, fine. If a double-quoted string should actually be used, be aware that  @arrays double-quote interpolate:

        c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $hostnum = [ qw(array elements will double-quote interpolate) ]; dd $hostnum; ;; my $scalar = qq{webmaster@$hostnum}; print qq{>$scalar<}; " ["array", "elements", "will", "double-quote", "interpolate"] >webmasterarray elements will double-quote interpolate<

        In this example, I have made  $hostnum an array reference. If it is not, you should get a message like

        c:\@Work\Perl\monks>perl -wMstrict -le "my $hostnum = 'not an array reference'; my $scalar = qq{webmaster@$hostnum}; print qq{>$scalar<}; " Can't use string ("not an array reference") as an ARRAY ref while "str +ict refs" in use at ...
        if you have enabled warnings and strict in your code! If you have not enabled these important Perl protective measures (and as a Perl novice, you always should), Perl will happily give you something like
        c:\@Work\Perl\monks>perl -le "my $hostnum = 'not an array reference'; my $scalar = qq{webmaster@$hostnum}; print qq{>$scalar<}; " >webmaster<
        Again, I have no idea what the quoted code should really be doing. I just want to alert you to another possible problem.

        Note that in my code examples, I use  qq{...} in place of the  "..." double-quote operator. I do this because of the way the Windoze command line (mis)handles  " (double-quote) characters. See Quote and Quote-like Operators in perlop for info on all Perl quoting operators.

        Some more very useful reading is toolic's Basic debugging checklist.


        Give a man a fish:  <%-{-{-{-<

Re: Verifying Email Addresses
by thanos1983 (Parson) on Apr 04, 2019 at 15:25 UTC

    Hello knuppn,

    If you do not mind using alternative modules or even regex see Email validation using Regular Expression in Perl.

    Finally another nice "similar" question was asked before Valid Email Filter. Maybe you will find it interesting or you might pick up some ideas.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!