Returned mail is causing script to crash

T

Two Heads

Hi,

I apologise if this is not strictly a Perl question but I am not sure
where else to post this.

One of my scripts sends an automated email to the customer. However if
the email address is no longer valid the server(?) seems to return a
message to the script informing it that the mail attempt failed and a
copy of the mail is held in a certain folder.

However the script is not expecting this message and the message seems
to want to print itself to the screen causing the script to fail. The
code for the mail routine is as follows;

open(MAIL, "|$sendmailLoc $tenantMail")|| die "Cant send mail!\n";
print MAIL "Subject: Rent Account Arrears Warning\n";
print MAIL "From: $clientTitle\n";
print MAIL "Message from $clientTitle\n
For the attention of: $tenant\n\n";
print MAIL "$arrsMessage\n\n";
print MAIL "Account balance $curr_bal\n";
close (MAIL);

Is there a way I can catch the error message so that I can forward it
elsewhere?

Thanks for your interest,
Mark
 
A

Anno Siegel

Two Heads said:
Hi,

I apologise if this is not strictly a Perl question but I am not sure
where else to post this.

One of my scripts sends an automated email to the customer. However if
the email address is no longer valid the server(?) seems to return a
message to the script informing it that the mail attempt failed and a
copy of the mail is held in a certain folder.

However the script is not expecting this message and the message seems
to want to print itself to the screen causing the script to fail.

The subject says "crash", your text says "fail". How about telling
us what actually happens?
The
code for the mail routine is as follows;

open(MAIL, "|$sendmailLoc $tenantMail")|| die "Cant send mail!\n";
print MAIL "Subject: Rent Account Arrears Warning\n";
print MAIL "From: $clientTitle\n";
print MAIL "Message from $clientTitle\n
For the attention of: $tenant\n\n";
print MAIL "$arrsMessage\n\n";
print MAIL "Account balance $curr_bal\n";
close (MAIL);

Is there a way I can catch the error message so that I can forward it
elsewhere?

No idea. The code above is useless, depending, as it does, on the
values of half a dozen variables we don't know.

Post a self-contained example.

Anno
 
G

gnari

However the script is not expecting this message and the message seems
to want to print itself to the screen causing the script to fail. The
code for the mail routine is as follows;

open(MAIL, "|$sendmailLoc $tenantMail")|| die "Cant send mail!\n";
print MAIL "Subject: Rent Account Arrears Warning\n";
print MAIL "From: $clientTitle\n";
print MAIL "Message from $clientTitle\n
For the attention of: $tenant\n\n";
print MAIL "$arrsMessage\n\n";
print MAIL "Account balance $curr_bal\n";
close (MAIL);

Is there a way I can catch the error message so that I can forward it
elsewhere?

test the result of close() and look at $! and $?

see perldoc -f close

gnari
 
B

Brian McCauley

Two said:
One of my scripts sends an automated email to the customer. However if
the email address is no longer valid the server(?) seems to return a
message to the script informing it that the mail attempt failed and a
copy of the mail is held in a certain folder.

However the script is not expecting this message and the message seems
to want to print itself to the screen causing the script to fail.

I suspect you are using the phrase "causing the script to fail" in an
unconventional way.
The
code for the mail routine is as follows;

open(MAIL, "|$sendmailLoc $tenantMail")|| die "Cant send mail!\n";
print MAIL "Subject: Rent Account Arrears Warning\n";
print MAIL "From: $clientTitle\n";
print MAIL "Message from $clientTitle\n
For the attention of: $tenant\n\n";
print MAIL "$arrsMessage\n\n";
print MAIL "Account balance $curr_bal\n";
close (MAIL);

Is there a way I can catch the error message so that I can forward it
elsewhere?

Piping both to and from a subprocess is complex. (For details see FAQ
"How can I open a pipe both to and from a command?").

Better to redirect STDERR to a temporary file then rewind the file read
it back. (For details see numerous previous threads).

http://groups.google.com/groups?q=redirect+STDERR+temporary+file+group:comp.lang.perl.*

Note - you are not using the normal options on sendmail (see FAQ: "How
do I send mail?"). There are good reasons why these are the normal
options. For example by choosing to put $tenantMail into the command
line rather than using the more usual -t approach you open up a whole
can of worms with respect to shell metacharacters in $tenantMail.

You may also find that telling sendmail not to attempt to return errors
syncronously (an effect of one of the aforementioned options) helps a
bit but there are some errors (such as local mailq filesystem full) that
will always be returned syncronously. (For details RTFM on sendmail).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top