email - How to send a mail with a specific template to users from a file in Linux (template contains info from the file)? -
basically have take surname, lastname, username, , grade file , have send email user ('username') looks this:
"dear 'surname' 'lastname'!
grade on 'class name' exam 'grade'."
the 'class name' given via parameter list of shell, rest of info in file.
so went step step. first printed out awk desired layout
awk '{print "dear",$1,$2 "!","\nyour grade on '$class' exam ",$4;}' in.txt works fine. tested if mailing works , does:
$ mail -s 'subj' bando < /tmp/msg.txt so need write output textfile can send specific user.
this problems kicked in. i've tried several versions, tried printing string file there wrong , don't know what. same echo , cat. tried chopping in smaller pieces still nothing.
try awk command read in.txt , send email:
class='9th' awk '{msg="dear " $1 " " $2 "!,\nyour grade on '$class' exam " $4; system("echo \"" msg "\" | mail -s 'subj' " $3);}' in.txt where in.txt file looks this:
cat in.txt lname fname foobar@example.com 7.6 explanation:
awk command parses in.txt file $1, $2, $3, $4 placeholders.
then formats mail message in variable msg.
finally calls system function execute echo "<msg>" | mail -s 'subj' $3 echo msg variable , pipe mail command send it. backslashes needed there enclose msg variable double quotes.
Comments
Post a Comment