Operating System - HP-UX
1752785 Members
5910 Online
108789 Solutions
New Discussion юеВ

Re: Script failing to send body of the e-mail (sometimes....)

 
SOLVED
Go to solution
James A. Donovan
Honored Contributor

Script failing to send body of the e-mail (sometimes....)

I've written a general function that will send out alert e-mail's to admins. Sometimes it includes the body of the e-mail and sometimes it doesn't, and I can't figure out why it's inconsistant.

If I run a script manually that calls this function using my user id, everything works perfectly.
If I run a script via cron using my user id, everything works perfectly.
If I run a script manually as root, everything works perfectly.
If I run a script via root's cron, everything works perfectly.
If the script is kicked off by the backup process using root's id, it only sometimes works!!!

The problem is that even though I pass $4 into the function and it's written to $MAILFILE (I've verified that it is being written!!!), when the e-mails are delivered, they are sometimes missing the message itself. Only the header portion arrives....

Also, if I do NOT pass in a message, the default message, "No text message available." is included and delivered correctly in e-mails that get sent out!

I've tried pretty much everything I can think of. Any ideas???

# send_email_alert ()
# This function delivers all e-mail alerts via sendmail, attaching the
# appropriate headers.
#
# It accepts four parameters:
# $1 : The addressee(s) (To:)
# $2 : The subject (Subject:)
# $3 : The priority (Importance: [Normal|High])
# $4 : The text body, if any
#
# When calling send_email double-quotes (e.g. "$1") should be used
# around all fields.
#
send_email_alert ()
{
MAILFILE=$(/bin/mktemp)

# Build the header...
/bin/echo "To: $1" > ${MAILFILE}
/bin/echo "Subject: $2" >> ${MAILFILE}
/bin/echo "Importance: $3" >> ${MAILFILE}

# if no text body was passed, need to default the value...
if [ "X${4}" = "X" ]
then
MSGBODY="No text message available."
else
MSGBODY="$4"
fi
/bin/echo "${MSGBODY}" >> ${MAILFILE}

# Send the e-mail
/bin/cat ${MAILFILE} | /usr/sbin/sendmail -t

# cleanup....
# /bin/rm ${MAILFILE}
return
}

 

 

-------------------------------

P.S. This Thread has been moved from HP-UX --> Languages & Scripting to HP-UX --> Messaging - Forum Moderator

Remember, wherever you go, there you are...
12 REPLIES 12
James A. Donovan
Honored Contributor

Re: Script failing to send body of the e-mail (sometimes....)

As an example of what I'm talking about:

Last night, this is one of the e-mail message files that was sent out missing the body. As you can see the temp file, a18833, contains the message "bpstart_notify.niobe_db_erhr: started: Tue 10/26/04 21:06", but the e-mail that was delivered did not!

$ cat a18833
To: dba
Subject: PSMDB-I: Backup Initiated (prod11i)
Importance: Normal
bpstart_notify.niobe_db_erhr: started: Tue 10/26/04 21:06
Remember, wherever you go, there you are...
Sundar_7
Honored Contributor

Re: Script failing to send body of the e-mail (sometimes....)

I dont see any "obvious" mistakes in the script.

But you could try onething. I am little skeptical about catting and piping to sendmail. Let the sendmail read the file directly.

/usr/sbin/sendmail -t < ${MAILFILE}

Try this if you would like and see if it helps.

Learn What to do ,How to do and more importantly When to do ?
John Poff
Honored Contributor

Re: Script failing to send body of the e-mail (sometimes....)

Hi,

Since you've verified that the $MAILFILE is getting the body written correctly to it, I would suspect something happening inside of sendmail. Have you noticed any difference in the body part of the MAILFILE for ones that didn't succeed? I wonder if it needs a blank line after the body to make it work?

One thing I have done for testing variables is to use the '-z' test to see if the variable has a zero length. Your test for the $4 parameter would look something like this:

if [[ -z $4 ]]
then
MSGBODY="No text message available."
...

JP
Sundar_7
Honored Contributor

Re: Script failing to send body of the e-mail (sometimes....)

I thought in the same line for a minute
:-). But since Jim has mentioned that the temp file is actually being written to, any problem with the if [ "X${4}" is ruled out.
Learn What to do ,How to do and more importantly When to do ?
John Poff
Honored Contributor

Re: Script failing to send body of the e-mail (sometimes....)

Sundar,

I understand. I just added that as an afterthought about testing the variable. Our minds running along the same line? That's scary. My mind usually runs in circles. :)

Another idea I just had is that you could try and catch the return code from sendmail to see if it is complaining about anything. It might not help much but it might be worth looking at.

JP
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Script failing to send body of the e-mail (sometimes....)

1. Be sure there is an empty line between the headers and the body, even if the body is empty

2. Be sure there is no line in the body tht consists of a single dot, because it will end the body

I'd personally would do it with Perl :)

--8<---
#!/opt/perl/bin/perl
use strict;
use warnings;
@ARGV >= 3 or die "usage: $^0 |
use Mail::Sendmail;
my ($to, $sbj, $imp, @bdy) = @ARGV;
if (@bdy == 1 && -f $bdy[0]) {
$/ = undef;
@ARGV = (@bdy);
@bdy = <>;
}
my $bdy = join " " => @bdy;
$bdy =~ m/\S/ or $bdy = "No Text message available.";
sendmail (To => $to, Subject => $sbj, Importance => $imp, Message => $bdy);
-->8---

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
James A. Donovan
Honored Contributor

Re: Script failing to send body of the e-mail (sometimes....)

Sundar,

Tried: /usr/sbin/sendmail -t < ${MAILFILE}
No change in behaviour.

John,

The return code of sendmail itself is always 0, as an e-mail is being sent successfully, it's just that the body is sometimes missing.

procura,

I'll try adding the initial blank line for tonight's run.

fi
/bin/echo "" >> ${MAILFILE}
/bin/echo "${MSGBODY}" >> ${MAILFILE}

/usr/sbin/sendmail -t < ${MAILFILE}

If I can't figure out why this is happening, I just may "borrow" your perl script...

I've already tried adding "." as the last line in the message, and that fails.
I've tried adding 2, 5 and 10 second sleeps between the last write to $MAILFILE and the invocation of sendmail, and that fails too.

...maybe the following is a clue, maybe not...these two e-mails were generated for delivery last night. The first got sent out missing the body. The second was sent with the body included. The only difference between the two function calls is their location in the script. The first call is roughly half-way through, the second call is the 2nd last statement of the script, and the last time that the function is called.

[tank|jdonovan]
$ cat a00827
To: dba
Subject: PSMDB-I: Backup Completed (prod11i)
Importance: Normal
Testing: e-mail alert. At this point bcvs are unmounted

[tank|jdonovan]
$ cat a01113
To: dba
Subject: PSMDB-I: Backup Completed (prod11i)
Importance: Normal
bpend_notify.niobe_db_erhr ended: Thu 10/28/04 02:21
Remember, wherever you go, there you are...
Mark Greene_1
Honored Contributor

Re: Script failing to send body of the e-mail (sometimes....)

While I think that Merijn's got the bunny on this one, I will suggest instead of passing the text via the commandline and $4, that you cat the /tmp file in this line:

bin/echo "${MSGBODY}" >> ${MAILFILE}

like this:

bin/echo "/tmp/[filename]" >> ${MAILFILE}

and see if that changes your results any.

mark
the future will be a lot like now, only later
Steven E. Protter
Exalted Contributor

Re: Script failing to send body of the e-mail (sometimes....)

Script looks good.

Perhpas try and trap a return code from critical steps.

rc=$?
if [ $rc -ne 0 ]
then
echo "oops Jim, something went wrong: $rc"
fi

Add to the echo statement so you know what line of code blew up.

I'm attaching a script, based no something I stole from an itrc document. It doesn't do that but I would not try my code as a first resort.

Regards,

Steve
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com