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

I put this question in an old topic, but it was a little bit of time ago... so not sure if it will be seen, so creating a new one... Sorry if that is wrong to do...

copied from older topic:

What will make this work?
use Test::Trap qw/ :output(systemsafe) /; $_sendError = ""; trap { $msg->send('smtp', 'mail.wholewellnessclub.com', AuthUser=> +'somemailuser@somedomain.com', AuthPass=>'$_encEmPass') || $_sendErro +r = $@ }; $_ and $_sendError = $_ for $trap->die, $trap->stdout, $trap->stde +rr; if($_sendError) { return 0; } else { return 1; }

I don't want to print anything, I just need to catch the error IN a variable so that I can have my system continue. It emails me any errors that the programming catches.

Thank you for any advice you can offer for this to work.
Right now I get this error:
Can't modify logical or (||) in scalar assignment at /path/to/data/fil +e/Send_Mime_Lite_Email.data line 39, near "$@ }" # That code is above...

Thanks again in advance.

Replies are listed 'Best First'.
Re: using Mime::Lite
by jethro (Monsignor) on Aug 18, 2009 at 23:22 UTC
    This is the old problem of precedence. Assignement '=' has a lower precedence than '||', so you try to assign a value to a boolean expression.

    Simple solution: Use 'or' instead of '||' and 'and' instead of '&&'

      Thanks that got rid of the error, however, it still does not send the emails out...

      Here is the file contents inside the subroutine.
      my ($__to,$__cc,$__bcc,$__from,$__subject,$__html_message,$__text_mess +age,$__importance,$__xpriority,$__x_ms_priority) = @_; use MIME::Lite; my $_sendIp = $ENV{REMOTE_ADDR} || "127.0.0.1"; my $msg = MIME::Lite->new( 'From' => $__from, 'To' => $__to, 'Cc' => $__cc, 'Bcc' => $__bcc, 'subject' => $__subject, 'Type' => 'multipart/mixed', "X-Mailer" => "$_co_domain Mailer - Version 2.0", 'X-Organization' => "$_co_name", "X-Mail-Sent-For" => "$_co_name or www.$_co_domain/$_un; From +IP: $_sendIp", 'X-Priority' => $__xpriority, 'X-MSMail-Priority' => $__xpriority ); $msg->attach( 'Type' =>'TEXT', 'Data' =>"$__text_message", 'Disposition' => "inline" ); $msg->attach( 'Type' =>'HTML', 'Data' =>"$__html_message", 'Disposition' => "inline" ); my $_encEmPass = "43616c9765365f5f2640f311e1w1d5d6t2f3eacw052459ed4t4" +; $_encEmPass = decryptPass($_encEmPass); use Test::Trap qw/ :output(systemsafe) /; my $_sendError = ""; trap { $msg->send() or $_sendError = $@ }; $_ for $trap->die, $trap->stdout, $trap->stderr; if(!$_sendError and $_) { $_sendError = $_; } if($_sendError) { return (0,"$_sendError"); } else { return (1,""); }
      Do you see why that would not send the email? I get not errors returned but no emails get sent out for some reason.

      Thank you Perl Monks for your help and advice.

        What do you expect $_ for $trap->die, $trap->stdout, $trap->stderr; to do? It seems a noop to me. And $_ will be empty after that for-loop because the scope of the $_ in the for-loop ends with the loop. So testing $_ afterwards makes no sense, the if statement will never be executed

        Maybe you wanted to do print $_ for $trap->die, $trap->stdout, $trap->stderr; so that you at least can see when some message is printed on STDOUT or STDERR?

        Or you might have something like this in mind:

        my $allmessages.= $_ for $trap->die, $trap->stdout, $trap->stderr; if (!$_sendError and $allmessages) { $_sendError = $allmessages; }

        which would concatenate the values from all three methods and return that later