http://qs321.pair.com?node_id=11114009

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

Hello

I need to verify that a string is an email address, at least that it is well formed. I need to perfom this offline, so not Web check needs to be done. I an trying to use Email::Valid, however if run without an Internet connecction, it gets stucked for many seconds (I did not count, probably 20 or 30). I tried to disable some check, but with no success. Any idea?

use strict; use warnings; use Email::Valid; my $address = Email::Valid->address(-address => 'myname@mydomain.com', + -mxcheck => 0, -tldcheck => 0, -fudge=>0); print ($address ? 'yes' : 'no');

Replies are listed 'Best First'.
Re: use Email::Valid offline
by davido (Cardinal) on Mar 09, 2020 at 16:14 UTC

    I haven't been able to replicate this behavior. I tried the following:

    First, I shut off all network connections and ran this code:

    my $address = Email::Valid->address(-address => 'foo@example.com', -mx +check => 0, -tldcheck => 0, -fudge => 0); use Data::Dumper; warn Dumper $address;

    I did not block. Next I added $Email::Valid::Debug = 1; at the top of my script, and saw no debug output. Next I ran the same but with -mxcheck => 1. With that setting I did block (my network was off) and saw this output: using Net::DNS for dns query. Turning the network back on, I got a clean response but with that same debug. My next step was to override the Net::DNS::Resolver::new sub and the Net::DNS::mx sub so that they would warn if invoked. With -mxcheck => 0 I got no warning. With -mxcheck => 1 I got the warning indicating I was hitting Net::DNS code.

    Finally I spewed %INC with -mxcheck set false. Unfortunately Net::DNS does get loaded even if the check is turned off. But with no network I never blocked.

    In the POD for Email::Valid it mentioned that you can set $Email::Valid::Resolver->tcp_timeout($seconds) and $Email::Valid::Resolver->udp_timeout($seconds) to set how many seconds you're willing to wait on the network. If you continue to find a way to get blocking behavior despite turning off the external checks, set that number really low. However, in my own testing with the network turned off, those two method calls didn't seem to do what was needed; I still got blocking behavior.

    One more thing: I looked at Net::Domain::TLD and saw that it does not seem to hit the network. So you could probably enable -tldcheck => 1 without blocking.


    Dave

      Thank you for your reply.

      I always get "unresolvable name: at C:/Strawberry/perl/vendor/lib/Email/Valid.pm line 41.", no matter what I try. Even setting $Email::Valid::Resolver = 1 makes no difference, as if no option is read by the module.

      Email::Valid is up-to-date, Perl is v5.28.1

        Re-check my post: $Email::Valid::Resolver = 1 was incorrect. As far as I can tell $Email::Valid::Resolver->tcp_timeout($seconds) is correct, though doesn't seem strong enough. I apologize for the confusion.

        What version of Email::Valid and Net::DNS are you using? Line 41 of Email::Valid in the version currently available on CPAN doesn't contain code that should trigger that message.


        Dave

Re: use Email::Valid offline
by 1nickt (Canon) on Mar 09, 2020 at 16:00 UTC

    Hi, please double-check the doc for Email::Valid ... probably not directly related to your problem, but it shows a different syntax for the off-line validity check:

    $ perl -Mstrict -MEmail::Valid -wE 'say Email::Valid->address(q{myname +@mydomain.com}) ? "valid" : "failed $Email::Valid::Details check"';

    Hope this helps!

    update: refactored tone thx davido


    The way forward always starts with a minimal test.

      The only time I ever found a value in $Email::Valid::Details during my testing was when it contained "mxcheck" after enabling the -mxcheck => 1 feature, disabling networking, and waiting around for the two minute timeout.

      However, it is always correct to suggest looking at all the error messages and checking the docs when something goes wrong. So yes, correct 1nickt.


      Dave

Re: use Email::Valid offline
by 1nickt (Canon) on Mar 10, 2020 at 03:54 UTC

    Hi, chastened by my learned teacher davido for my earlier unhelpful reply, I performed penance and installed VirtualBox and a Windows 10 VM and Strawberry Perl (64-bit). I'm running Strawberry Perl v5.30.1; Email::Valid v1.202; Net::DNS v1.21; Net::DNS::Resolver v1740, all of which came with Strawberry. I tested your original code and it worked fine. Could not reproduce your issue: all behaviors as davido reported in his reply to your OP.

    Sorry cannot be more help!


    The way forward always starts with a minimal test.

      1nickt: lol, you're awesome. Sorry for coming across poorly. Your post was fine and I was wrong to suggest otherwise. How many times a day do we remind ourselves, "Look at the first error message first", "Read the documentation again", and so on. ...because those are some of the techniques that are most effective in arriving at a solution.


      Dave