Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Trap the error msg from Mime::Lite

by chrism01 (Friar)
on Jan 22, 2008 at 06:51 UTC ( [id://663511]=note: print w/replies, xml ) Need Help??


in reply to Re: Trap the error msg from Mime::Lite
in thread Trap the error msg from Mime::Lite

#!/usr/bin/perl -w use MIME::Lite; # send mail attachments use Sys::Hostname; # Used to insert hostname in emails use Cwd; # Get current full dir for error msgs use strict; # Enforce declarations my ( $hostname, # name of this box for sending $prog_full_name, # prog path + name $contact_email, # contact email address $msg, # msg object $sender, # sender of email $subject, # subject line in email $body_text # actual email msg text ); # Get hostname anyway we can using Sys::Hostname; # tries syscall(SYS_gethostname), `hostname`, `uname -n` $hostname = Sys::Hostname::hostname(); # Get program full path name $prog_full_name = cwd()."/${0}"; # Set the fields required by Mailer... $contact_email = ''; $sender = "$prog_full_name"; $subject = "$prog_full_name: Asset Mgr Report"; $body_text = "This is an automated message:\n\n"; # ... and send it # Header $msg = MIME::Lite->new( From => $sender, To => $contact_email, Subject => $subject, Type =>'multipart/mixed' ); # Body Content $msg->attach( Type => 'TEXT', Data => $body_text ); # Attachment $msg->attach( Type => 'text/plain', Path => "/home/chrism/t.csv", Filename => "t.csv", Disposition => 'attachment' ); # Send $msg->send or print "Error: $@\n";
Output is:
sendmail: fatal: No recipient addresses found in message header Error:
where 1st line appears in logfile, 2nd line shows error text is not in $@.
Same occurs if I use $! instead as above.

If I try this instead

eval{$msg->send}; print "Error: $@\n" if($@);
I only get the logged msg. My own error msg does not appear.

Replies are listed 'Best First'.
Re^3: Trap the error msg from Mime::Lite
by Corion (Patriarch) on Jan 22, 2008 at 06:59 UTC

    The error message

    No recipient addresses found in message header

    seems to confirm that you never set $contact_email. Did you just remove your email address from the line initializing it for privacy reasons or is the recipient really empty?

    $contact_email = 'chrism01@example.com';

    would be an example of initializing the email address. Of course, you would possibly use a local email address instead of an address that is routed through the internet.

      As per my orig post, I did it deliberately, in order to force the error, so I could test the error handling....
      Unfortunately I can't 'grab' the error msg for further processing....

        send spawns out to sendmail. If you have Net::SMTP installed you can use

        eval { $msg->send_by_smtp; }; if ($@) { # handle error }

        I'm stupid and didn't read your message, sorry!

        I think the string you see comes from the sendmail program directly. Maybe you can close STDERR in the hope that that will shut up the sendmail error output. Otherwise, you could skip using sendmail and talk SMTP directly, but not every machine is set up for that either:

        $msg->send('smtp','some.host.local');
        Hi guys,
        tried both those, but got 'Error: Failed to connect to mail server: Connection refused at ./m.pl line 53' in both cases.
        Seems it doesn't like smtp... weird huh?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://663511]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-24 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found