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

I have written a program that does a newsletter for our business. I am using MIME::Lite as the module for doing the sending. Here is the subroutine that I am passing data to from a Database.
sub sendEmail { my($email, $c_first) = @_; $msg = MIME::Lite->new( To =>$email, From =>'marketing@taieriprint.co.nz', Subject =>$subject, Type =>'multipart/related' ); $msg->attach(Type => 'text/html', Data => qq{<html> <HEAD> <TITLE>Taieri Print Ltd</TITLE> </HEAD> <body bgcolor="#DBDBDB"> <table align="center" width="645" border="0" bgcolor="whit +e"> <tr><td align="center"><img src="cid:tplogo.gif"></td> </tr><tr height="3" CELLPADDING=20><td align="right"><font siz +e="2" face="Microsoft Sans Serif">$Month $Year</font></td></tr></tabl +e> <table CELLPADDING=20 align="center" width="645" bgcolor="whit +e"> <tr><td align="left"><font face="Microsoft Sans Serif" size="2 +">Dear $c_first,</font><br> <font face="Microsoft Sans Serif" size="2">$message</td></tr>< +/font> <tr><td align="center"><font size="1" face="Microsoft Sans Ser +if"><br>We hope you have enjoyed recieving this message. However, if you'd rather not recieve future newsletters from u +s, then please <a href="mailto:mailbird&#64;taieriprint.co.nz?subject=Unsubsc +ribe"> click here</a> to unsubscribe. </td></tr></table> </body> </html> } ); $msg->attach(Type => 'image/gif', Id => 'tplogo.gif', Path => 'C:/mailbird/tplogo_new.gif', ); if ($query->param('attach')) { $msg->attach(Type => 'application/pdf', Path => $attachment, Filename => $filename); } $msg->send('smtp', 'smtp.taieriprint.co.nz') or warn print "Una +ble to send email: $_"; return; }
I am wanting to write to a file any emails that are unable to be sent, rather than the application 'dying'. This will ensure that a bogus email address doesn't stop the rest of the emails being sent. For the $msg->sent part of the subroutine I have tried using or warn and printing to a file with no success. Can someone please give me a idea on how to achieve this goal. Thankyou very much.

Replies are listed 'Best First'.
Re: Email error checking
by GrandFather (Saint) on Sep 22, 2005 at 21:58 UTC

    Interesting to see someone just down the road from me visiting the Monastery :).

    I'm probably completely missing the point, but it looks like you could simply use an if to check the result from the send like this:

    if (! $msg->send('smtp', 'smtp.taieriprint.co.nz')) { # handle bad send here }

    The other part if the problem would seem simply that you should chuck the email body into a string rather than embedding it as a parameter so you get $msg->attach(Type => 'text/html', $body); and have $body available to write out to a file if you need to.

    If this doesn't answer your actual problem, you better show us an error message or some other information about the way your script is failing.


    Perl is Huffman encoded by design.
Re: Email error checking
by saberworks (Curate) on Sep 22, 2005 at 22:01 UTC
    In general when you want to stop a module or function from dying, you wrap the call in an eval{ } block. Then you can test $@ to see if an error occurred. For example:
    eval { $msg->send(); }; if($@) { # We had an error, log it to a file... $log->add_message($email_address, $@); }
    This prevents the script from dying if send(); fails. Of course, you want to make sure sending is working properly to begin with (maybe your script is failing for some reason other than a bad email address - you can print the value of $@ to see why it is failing).

    Update: Uh, let me clarify. That will catch the $msg->send() function ONLY IF it calls die() (raises an exception). So if the send() function returns true or false based on success or failure, you should check other solutions in this thread.
Re: Email error checking
by Happy-the-monk (Canon) on Sep 22, 2005 at 22:01 UTC

    For the $msg->sent part of the subroutine I have tried using or warn and printing to a file with no success.

    Either use warn or print.

    To print to a file, you need to specify the filehandle on the print line. The file needs to be open first, so first use
        open( FILEHANDLE, ">> $filename" ) or die qq(unable to open "$filename" because of: $!);
    to achieve that.

    What did you try to print to file?

    Cheers, Sören