in reply to instantiating an smtp object

Update: I would not use Net::SMTP. please refer to Re: instantiating an smtp object post of Email namespace's if you trust them or just write your own Sendmail.

maybe try this. use Carp; to croak" $@"; the errors.

#!/usr/bin/perl -w use strict; use 5.010; use lib "template_stuff"; use html3; sub rndStr{ join'', @_[ map{ rand @_ } 1 .. shift ] } my $new_name = rndStr( 5, 'a'..'z', 1..9); my $ref_to_name = \$new_name; my $return_page = create_page($ref_to_name); say "page is $return_page"; email_link($return_page); sub email_link{ use strict; use 5.014; use Carp; use Net::SMTP; use config2; my $link = shift; my $sub_hash = "my_ftp"; my $host = $config{$sub_hash}->{'site'}; say "host is $host"; my $phone = $config{$sub_hash}->{'phone'}; say "phone is $phone"; if (exists $ENV{USER} && $ENV{USER}) { eval q^ use Net::SMTP; my $smtp = Net::SMTP->new(Host => $host, Debug => 2) or croak "Unable to connect to '$host'. $!"; $smtp->mail($ENV{USER}); $smtp->to($phone); $smtp->data(); $smtp->datasend("From: $ENV{USER}\n"); $smtp->datasend("Subject: This is your map link\n"); $smtp->datasend("\n"); $smtp->datasend($link); $smtp->dataend(); $smtp->quit(); ^; croak "Net::SMTP fatal error: $@" if $@; } else { say '$ENV{USER} does not exists or is not defined.'; } } __END__
updated: use Carp; Carp

Replies are listed 'Best First'.
Re^2: instantiating an smtp object
by haukex (Archbishop) on Jun 06, 2016 at 11:00 UTC

    Hi $h4X4_|=73}{,

    Regarding the idiom eval ...; if ($@) ..., see for example Bug in eval in pre-5.14 or the "Background" section in Try::Tiny. The better idiom is eval "...; 1" or handle_error($@). And in this case I'd suggest eval BLOCK instead of eval STRING so that syntax errors can be caught at compile time.

    Also, in "say "Unable to connect to '$host'. $!"", I think that should be $@ instead of $! (at least per the documentation of Net::SMTP).

    Regards,
    -- Hauke D

    Update: s/patter?n/idiom/

      clobbered

      Carp Carp takes care not to clobber the status variables $! and $^E in the course of assembling its error messages. This means that a $SIG{__DIE__} or $SIG{__WARN__} handler can capture the error information held in those variables, if it is required to augment the error message, and if the code calling Carp left useful values there. Of course, Carp can't guarantee the latter.

      $@

      Interpolate. $@ is set if the string to be eval-ed did not compile (this may happen if open or close were imported with bad prototypes), or if Perl code executed during evaluation die()d. In these cases the value of $@ is the compile error, or the argument to die (which will interpolate $! and $? ). (See also Fatal, though.)

        Hi $h4X4_|=73}{,

        It's unfortunate that the eval ...; if ($@) ... idiom is still very prevalent, there are many examples in the literature and even the eval docs use it! However, the issues I linked to previously have been around for a long time and even though Perl 5.14 fixed some of the major issues, as far as I have read the fixes are not perfect yet. The idiom eval { ...; 1 } or ... at least allows for reliable detection of when eval failed, even when $@ gets clobbered.

        As for $! vs. $@, I had a look at the source of Net::SMTP's new and it seems that $@ is explicitly set there.

        Regards,
        -- Hauke D

Re^2: instantiating an smtp object
by Aldebaran (Curate) on Jun 07, 2016 at 07:57 UTC

    slightly injured, so terse. this is output with failed assumptions

    host is www.1and1.com $ENV{USER} does not exists or is not defined. ... Net::SMTP fatal error: Unable to connect to 'www.1and1.com'. A connect +ion attemp t failed because the connected party did not properly respond after a +period of time, or established connection failed because connected host has fail +ed to resp ond. at (eval 35) line 3. eval ' use Net::SMTP; my $smtp = Net::SMTP->new(Host => $host, Debug => 2) or croak "Unable to connect to \'$host\'. $!"; $smtp->mail($ENV{USER}); $smtp->to($phone); $smtp->data(); $smtp->datasend("From: $ENV{USER}\\n"); $smtp->datasend("Subject: This is your map link\\n"); $smtp->datasend("\\n"); $smtp->datasend($link); $smtp->dataend(); $smtp->quit(); ;' called at alpaca4.pl line 27 main::email_link('3um671.html') called at alpaca4.pl line 12 at alpaca4.pl line 42. main::email_link('3um671.html') called at alpaca4.pl line 12 ## and then this one is the right smtp address host is smtp.1and1.com Net::SMTP fatal error: Unable to connect to 'smtp.1and1.com'. A connec +tion attem pt failed because the connected party did not properly respond after a + period of time, or established connection failed because connected host has fai +led to res pond. at (eval 35) line 3. eval ' use Net::SMTP; my $smtp = Net::SMTP->new(Host => $host, Debug => 2) or croak "Unable to connect to \'$host\'. $!"; $smtp->mail("myself"); $smtp->to($phone); $smtp->data(); $smtp->datasend("From: me\\n"); $smtp->datasend("Subject: This is your map link\\n"); $smtp->datasend("\\n"); $smtp->datasend($link); $smtp->dataend(); $smtp->quit(); ;' called at alpaca4.pl line 27 main::email_link('dn75w1.html') called at alpaca4.pl line 12 at alpaca4.pl line 42. main::email_link('dn75w1.html') called at alpaca4.pl line 12

    Called 1and1. Port should be 587. Encryption should be TLS. I couldn't understand the alphanumeric for 'Larry' as pronounced by someone from India, but we worked it out because I could keep on asking questions.

      For TLS you can try Net::SMTP::TLS and take eval out. since its a bad idea to use it with Net::SMTP.

      #!/usr/bin/perl -w use strict; use 5.010; use lib "template_stuff"; use html3; sub rndStr{ join'', @_[ map{ rand @_ } 1 .. shift ] } my $new_name = rndStr( 5, 'a'..'z', 1..9); my $ref_to_name = \$new_name; my $return_page = create_page($ref_to_name); say "page is $return_page"; email_link($return_page); sub email_link{ use strict; use 5.014; use Net::SMTP; use config2; my $link = shift; my $sub_hash = "my_ftp"; my $host = $config{$sub_hash}->{'site'}; say "host is $host"; my $phone = $config{$sub_hash}->{'phone'}; say "phone is $phone"; if (exists $ENV{USER} && $ENV{USER}) { use Net::SMTP::TLS; my $mailer = new Net::SMTP::TLS( $host, #Hello => 'some.host.name', Port => 25, #redundant User => 'emailguy', Password=> 's3cr3t') or die "Unable to connect to '$host'. $@"; $mailer->mail($ENV{USER}); $mailer->to($phone); $mailer->data; $mailer->datasend($link); $mailer->dataend; $mailer->quit; } else { say '$ENV{USER} does not exists or is not defined.'; } } __END__

        Thank you for your continued attention. I can type again and give better reports of what is or is not happening. I think my values of lexical variables going into it are correct. Again, I use ellipses for values I'd rather not splash all over the net. Output:

        page is 7ae191.html host is smtp.1and1.com phone is ...@txt.att.net username is ... password is ... ******************************************************************* Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER together with SSL_ca_file|SSL_ca_path for verification. If you really don't want to verify the certificate and keep the connection open to Man-In-The-Middle attacks please set SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application. ******************************************************************* at C:/Perl/site/lib/Net/SMTP/TLS.pm line 181. invalid SSL_version specified at C:/Perl/lib/IO/Socket/SSL.pm line 389 +.

        Current script

        #!/usr/bin/perl -w use strict; use 5.010; use lib "template_stuff"; use html3; sub rndStr{ join'', @_[ map{ rand @_ } 1 .. shift ] } my $new_name = rndStr( 5, 'a'..'z', 1..9); my $ref_to_name = \$new_name; my $return_page = create_page($ref_to_name); say "page is $return_page"; email_link($return_page); sub email_link{ use strict; use 5.014; use Net::SMTP; use config2; my $link = shift; my $sub_hash = "my_ftp"; my $host = $config{$sub_hash}->{'smtp'}; say "host is $host"; my $phone = $config{$sub_hash}->{'phone'}; say "phone is $phone"; my $username = $config{$sub_hash}->{'username'}; say "username is $username"; my $password = $config{$sub_hash}->{'password'}; say "password is $password"; if (1) { use Net::SMTP::TLS; my $mailer = new Net::SMTP::TLS( $host, #Hello => 'some.host.name', Port => 587, #redundant User => $username, Password=> $password) or die "Unable to connect to '$host'. $@"; $mailer->mail("myself"); $mailer->to($phone); $mailer->data; $mailer->datasend($link); $mailer->dataend; $mailer->quit; } else { say '$ENV{USER} does not exists or is not defined.'; } } __END__

        So again, I'm wondering how I seem to be making hard what everyone else thinks is super easy....