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

Hi Monks,

I've been using Mail::CheckUser for years, but today I noticed some inconsistent results.  Here are 2 examples (same 1 liner with different email address):

$ echo "a@b.com" | perl -MMail::CheckUser=check_email,last_check -ne 'BEGIN{$Mail::CheckUser::Skip_SMTP_Checks = 1};chomp;for $i (0..9){print "$_ -> ";if (check_email($_)){print "OK\n"}else{print last_check()->{reason}."\n"}}'
a@b.com -> DNS failure: NXDOMAIN
a@b.com -> DNS failure: NXDOMAIN
a@b.com -> OK
a@b.com -> OK
a@b.com -> DNS failure: NXDOMAIN
a@b.com -> OK
a@b.com -> DNS failure: NXDOMAIN
a@b.com -> DNS failure: NXDOMAIN
a@b.com -> OK
a@b.com -> DNS failure: NXDOMAIN

$ echo "a@abczz123ffz.com" | perl -MMail::CheckUser=check_email,last_check -ne 'BEGIN{$Mail::CheckUser::Skip_SMTP_Checks = 1};chomp;for $i (0..9){print "$_ -> ";if (check_email($_)){print "OK\n"}else{print last_check()->{reason}."\n"}}'
a@abczz123ffz.com -> OK
a@abczz123ffz.com -> DNS failure: NXDOMAIN
a@abczz123ffz.com -> DNS failure: NXDOMAIN
a@abczz123ffz.com -> OK
a@abczz123ffz.com -> DNS failure: NXDOMAIN
a@abczz123ffz.com -> DNS failure: NXDOMAIN
a@abczz123ffz.com -> OK
a@abczz123ffz.com -> OK
a@abczz123ffz.com -> OK
a@abczz123ffz.com -> DNS failure: NXDOMAIN

$ perl -MMail::CheckUser -e 'print "$Mail::CheckUser::VERSION\n"'
1.21
Questions:
Q1. Do you get similar results?
Q2. Why am I getting inconsistent results?
Q3. Can you suggest a better way of doing this check? (Basically I want to at least check the syntax of the address and that the domain exists, and I want a result within a second.)

I'm running Perl v5.10.1 on Linux.

Thanks.
Tel2

Replies are listed 'Best First'.
Re: Mail::CheckUser inconsistent results
by Anonymous Monk on Aug 26, 2015 at 07:23 UTC

    Q1. Do you get similar results?

    Kinda sorta , see below

    Q2. Why am I getting inconsistent results?

    Networks are networks, they're inconsistent by nature

    Or maybe old module needs TLC? Bug #106606 for Mail-CheckUser: Missing code checks in check_user_on_host

    I'm guessing probably a timeout occurs or the domain check is skipped the times you get validation

    Q3. Can you suggest a better way of doing this check? (Basically I want to at least check the syntax of the address and that the domain exists, and I want a result within a second.)

    I might give Email::Valid a try

    #!/usr/bin/perl -- BEGIN{$Mail::CheckUser::Skip_SMTP_Checks = 1}; use strict; use warnings; use Mail::CheckUser qw/ check_email last_check /; use Email::Valid; for (qw/ a@b.com a@abczz123ffz.com /){ for my $i (0..9){ print "$_ -> "; my $tt = time; #~ Alpha($_); Omega($_); print " ", time-$tt, "\n"; } } sub Omega { my $ev = Email::Valid->new( -mxcheck => 1, ); if ( $ev->address( "$_" ) ){ print "OK"; }else{ print "NOT OK ", $ev->details ; } } sub Alpha { if ( check_email($_)){ print "OK" }else{ print last_check()->{reason}; } } __END__ a@b.com -> DNS failure: NXDOMAIN 0.293894052505493 a@b.com -> DNS failure: NXDOMAIN 0.206084966659546 a@b.com -> DNS failure: NXDOMAIN 0.219376087188721 a@b.com -> DNS failure: NXDOMAIN 0.218585014343262 a@b.com -> DNS failure: NXDOMAIN 0.249444961547852 a@b.com -> DNS failure: NXDOMAIN 0.250465869903564 a@b.com -> DNS failure: NXDOMAIN 0.249492883682251 a@b.com -> DNS failure: NXDOMAIN 0.249480009078979 a@b.com -> DNS failure: NXDOMAIN 0.249181985855103 a@b.com -> DNS failure: NXDOMAIN 0.251276016235352 a@abczz123ffz.com -> DNS failure: NXDOMAIN 0.251024007797241 a@abczz123ffz.com -> DNS failure: NXDOMAIN 0.248931884765625 a@abczz123ffz.com -> DNS failure: NXDOMAIN 0.250422954559326 a@abczz123ffz.com -> DNS failure: NXDOMAIN 0.249350070953369 a@abczz123ffz.com -> DNS failure: NXDOMAIN 0.250164985656738 a@abczz123ffz.com -> DNS failure: NXDOMAIN 0.251390933990479 a@abczz123ffz.com -> DNS failure: NXDOMAIN 0.248254060745239 a@abczz123ffz.com -> DNS failure: NXDOMAIN 0.281542062759399 a@abczz123ffz.com -> DNS failure: NXDOMAIN 0.240454912185669 a@abczz123ffz.com -> DNS failure: NXDOMAIN 0.254595994949341 a@b.com -> NOT OK mxcheck 0.291569948196411 a@b.com -> NOT OK mxcheck 0.208410024642944 a@b.com -> NOT OK mxcheck 0.219083070755005 a@b.com -> NOT OK mxcheck 0.218981981277466 a@b.com -> NOT OK mxcheck 0.24937891960144 a@b.com -> NOT OK mxcheck 0.249850988388062 a@b.com -> NOT OK mxcheck 0.328232049942017 a@b.com -> NOT OK mxcheck 0.250256061553955 a@b.com -> NOT OK mxcheck 0.265328884124756 a@b.com -> NOT OK mxcheck 0.247688055038452 a@abczz123ffz.com -> NOT OK mxcheck 0.392891883850098 a@abczz123ffz.com -> NOT OK mxcheck 0.250204086303711 a@abczz123ffz.com -> NOT OK mxcheck 0.249541044235229 a@abczz123ffz.com -> NOT OK mxcheck 0.453320026397705 a@abczz123ffz.com -> NOT OK mxcheck 0.218268156051636 a@abczz123ffz.com -> NOT OK mxcheck 0.218549966812134 a@abczz123ffz.com -> NOT OK mxcheck 0.250627994537354 a@abczz123ffz.com -> NOT OK mxcheck 0.312192916870117 a@abczz123ffz.com -> NOT OK mxcheck 0.234641075134277 a@abczz123ffz.com -> NOT OK mxcheck 0.250532865524292
      Awesome answer, Anonymous Monk!

      Thanks very much for all that.

      But I don't see any inconsistent results with your code. Some of that output was from Mail::CheckUser, right?

      tel2

        Hi again Anonymous Monk.

        Your output looks doubled.  Did you run it twice - once with Alpha($_) uncommented and once with Omega($_) uncommented, or what?

        Strangely, today when I run my one liners on my webhost I seem to be getting 100% positive (i.e. wrong) results, regardless of the domain, as long as the address syntax is correct.  For example:

        $ echo "a@abczz123ffz.com" | perl -MMail::CheckUser=check_email,last_check -ne 'BEGIN{$Mail::CheckUser::Skip_SMTP_Checks = 1};chomp;for $i (0..9){print "$_ -> ";if (check_email($_)){print "OK\n"}else{print last_check()->{reason}."\n"}}'
        a@abczz123ffz.com -> OK
        a@abczz123ffz.com -> OK
        a@abczz123ffz.com -> OK
        a@abczz123ffz.com -> OK
        a@abczz123ffz.com -> OK
        a@abczz123ffz.com -> OK
        a@abczz123ffz.com -> OK
        a@abczz123ffz.com -> OK
        a@abczz123ffz.com -> OK
        a@abczz123ffz.com -> OK
        
        Just tried it on a different host and the results are 100% negative (i.e. correct).  Any ideas?  If I take this up with the webhost, what should I say?

        Thanks.

Re: Mail::CheckUser inconsistent results
by Monk::Thomas (Friar) on Aug 26, 2015 at 11:03 UTC

    Q3. Can you suggest a better way of doing this check? (Basically I want to at least check the syntax of the address and that the domain exists, and I want a result within a second.)

    Maybe you're already doing that, but in case not: Definitely cache the result. I recommend this because it's very likely that a) the senders are regularly mailing to the roughly same recipients b) the validity of a recipient is probably quite static. It doesn't really make sense to check if an email address is valid multiple times a day.

    You could then use this list of 'probably valid' email addresses and try to actually talk to the receiving mail server and see if it would accept an email with this recipient (no need to actually send the mail - you can abort the exchange as as soon as the mail server accepts the recipient). If the address is rejected then you can update the status from 'probably valid' to 'invalid'.

      Thanks for your comments, Thomas.

      I don't think caching would be worthwhile in my case, because:
      1. Each address will rarely be checked. It's basically a check to make sure a subscriber's address is valid when it's entered into my database.
      2. The status of validity could change as domains disappear, etc (though this would also be rare).

      However, I'm interested in what you're saying about talking to the receiving mail server.  Are you able to point me in the right direction for that kind of coding, please?

      tel2

        However, I'm interested in what you're saying about talking to the receiving mail server. Are you able to point me in the right direction for that kind of coding, please?

        I assume that would be the SMTP option you disabled