Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email Body is a file or template

Status
Not open for further replies.

jmanj

Programmer
May 20, 2003
298
US
I could create a simple email script like
mailx -s "Subject" email_address < $Template

My problem is embedding some coded variables in $Template
so that it will use local variables passed to the script such
name, date, etc. I could have just typed in the body of the email and use ${var} but the $Template is file created by a program and the verbiage could be different at any given time.

Is this possible?

I would appreciate any help and any sample.

manniej
 
Maybe something like this.

First, create a template that has some easy to find strings (i.e. VAR_DATE)...
Code:
Hi VAR_NAME,

This email is being sent VAR_DATE.

Today's lucky number is VAR_LUCKY.

Thanks!

Then just use sed in your script to alter and send it...

Code:
WHO=Everybody

cat message.template | \
    sed "s/VAR_DATE/$(date)/g;s/VAR_NAME/${WHO}/g;s/VAR_LUCKY/${RANDOM}/g" | \
    mailx -s "Subject" email_address

This results in something like...

Code:
Hi Everybody,

This email is being sent Thu Sep 30 10:11:57 PDT 2010.

Today's lucky number is 8724.

Thanks!


 
Thanks SamBones,
I did tried your example but I'm getting some errors.
Here my script:

#!/bin/ksh
PL=dev9
CCSDIR=$LAWDIR/$PL/lhs_scripts/interfaces

TMPWRK="Mymailbody"
VAR_DAT="10/10/2010"
VAR_WHOM="John Does"

cd ${CCSDIR}

cat message.${TMPWRK} | \
sed "s/VAR_WHO/${VAR_WHOM}/g;s/VAR_DATE/${VAR_DAT}/g" | \
mailx -s "Subject- Mail test" manniej@hls.org < $TMPWRK


I'm getting the following errors:

cat: 0652-050 Cannot open message.Mymailbody.
sed: 0602-404 Function s/VAR_WHO/John Does/g;s/VAR_DATE/10/10/2010/g cannot be parsed.


I remove "message." and seems was able to open the file
Mymailbody but still got the last 0602-404 error .
 
I replaced the double quotes with a single quotes and seems working okay but VAR_WHO and VAR_DATE was not replaced. here's the sample body of the email.

Hello VAR_WHO,

Per our records, your license is expiring on VAR_DATE. This is a reminder to begin the renewal process. The licensing boards can take 4-6 weeks for renewal, so you will want to plan accordingly. We will continue to monitor your license and update our records once we can verify renewal with the licensing board.


Thank You.
 
I'd try this:
Code:
sed "s!VAR_WHO!${VAR_WHOM}!g;s!VAR_DATE!${VAR_DAT}!g" $TMPWRK | \
mailx -s "Subject- Mail test" manniej@hls.org

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I was successful using the following syntax:

sed -e 's/VAR_WHO/'"$VAR_WHOM"'/g' -e 's/VAR_DATE/'"$VAR_DAT"'/g' Mymailbody > $TMPWRK2

mailx -s "Subject- Mail test" manniej@hls.org < $TMPWRK2


However the VAR_DAT variable should not contain any slashes (/). I'm still working on how to include slashes on variables.

Thanks a lot for all your help.
 
use another delimiter in sed instead of /

Code:
sed -e 's!VAR_WHO!'"$VAR_WHOM"'!g' -e 's!VAR_DATE!'"$VAR_DAT"'!g' Mymailbody > $TMPWRK2

HTH,

p5wizard
 
Seems like you didn't try my suggestion timestamped 1 Oct 10 3:35.
 
PHV,

Yes I will try your sample. We are aix and it didn't like double quotes (ksh). I think P5wizard sample is doable.

Thanks all.
 
Just tried P5wizard sample and it works!! The date variable is showing "10/10/2010".

Thanks a lot GUYS!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top