Using Variable As Input For mailx

All,

I'm having difficulty using a variable as the body of an e-mail; I suspect it has to do w/quoting, but am not sure. Right now I have separate functions for sending information and for sending errors. I'd like to set a variable (BODY) to use as input to mailx so I can have a generic function to send mail from my script. The code in the send_event_request function works fine; I can use it to send mail, but can't seem to find a way to use the BODY variable. Also, I'm having problems getting the newlines to work in BODY. (Note: All other variables are set elsewhere in the script and work fine.)

Thanks in advance!

BODY="Please run Event $EVENT (${DC}CAHOLD) for the $UL_TIME Upload. Thanks!\n\n"
BODY=$BODY"Stats For $UL_FILE\n"
BODY=$BODY"----------------------\n"
BODY=$BODY"Records  : $LINE_COUNT\n\n"
BODY=$BODY"Started  : $START_TIME\n"
BODY=$BODY"Finished : $FINISH_TIME\n\n"
BODY=$BODY"*** NOTE ***\n"
BODY=$BODY"This is not a production script.\n"
BODY=$BODY"A production script would have been e-mailed to the ollowing users:\n"
BODY=$BODY"$CURRENT_USERS\n\n"
BODY=$BODY"-----------------------------------------------------------------------------------------\n"
BODY=$BODY"This e-mail was automatically generated by $SCRIPT_NAME on $HOST_NAME. Please do not reply."

send_event_request()
{
   echo "Sending e-mail request..."

   SUBJECT="TEST"
   TO="me@mydomain.com"

   `mailx -s "$SUBJECT" "$TO" <<-EOF #$BODY would go here
Please run Event $EVENT (${DC}CAHOLD) for the $UL_TIME Upload. Thanks!

Stats For $UL_FILE
----------------------
Records  : $LINE_COUNT

Started  : $START_TIME
Finished : $FINISH_TIME

*** NOTE ***
This is not a production script.

-----------------------------------------------------------------------------------------
This e-mail was automatically generated by $SCRIPT_NAME on $HOST_NAME. Please do not reply.`
   ~. 2>/dev/null
   EOF 2>/dev/null
}

To get your $BODY text to span multiple lines try using a here document, e.g.

BODY=`cat<<EOF
blah blah
blah
fish 
EOF`

It looks to me like the closing backtick should be after the EOF 2>/dev/null, but
why do you want backticks around the mailx anyway?

mailx -s "$SUBJECT" "$TO" <<EOF
"$BODY"
EOF 2> /dev/null
#note > to force new file, while >> append to existing
echo "New Body here" >tmp_body.txt
echo "Here is more important info" >>tmp_body.txt
echo "and my final data." >>tmp_body.txt
mailx -s "important stuff" someone@somewhere.com <tmp_body.txt

spirtle,

I have the backticks because that's the example I found. I'm still a noob and don't know any better. Anyway, thanks for the suggestion! Here's the final code:

   BODY=`cat <<EOF
Please run Event $EVENT (${DC}CAHOLD) for the $UL_TIME Upload. Thanks!

Stats For $UL_FILE
----------------------
Records  : $LINE_COUNT

Started  : $START_TIME
Finished : $FINISH_TIME

*** NOTE ***
This is not a production script.

-----------------------------------------------------------------------------------------
This e-mail was automatically generated by $SCRIPT_NAME on $HOST_NAME. Please do not reply.
EOF`

`mailx -s "$SUBJECT" "$TO" <<EOF
$BODY`
EOF 2>/dev/null

And I do have to use both sets of backticks, otherwise it won't work for me (HP-UX B.11.23 U ia64 in ksh). It kept giving me a "ulgen_report.bak[24]: syntax error at line 172 : `<<' unmatched" error. So I added the 2nd set of backticks and it works great.

Joey,

Thanks for the suggestion too. I had thought of it, but kind of wanted to keep all the code in a variable. I may use it instead, I don't know. But thanks to you, I have that option.