sending email using bash script
I am working on writing a bash script to notify one or more users by email of certain events. Run from the Terminal command line, and having the script "echo" text of (what would be) a form letter with in-line variable expansion (i.e., ${VARIABLE}), all seems to work as anticipated. Eventually, I want cron to launch this shell script, and send an email to an "on-subnet" user (I have postfix enabled on my Mac, and there are multiple local user accounts).
I found some stuff on the web about sending mail from bash scripts, and so I made a small little test script, that reads like this:
#!/bin/bash
VARIABLE[1]="The 12,345 quick brown foxes "
VARIABLE[2]="jumped over the 67,890 lazy dogs."
mail -s "a test email" jv << EOF
This is a test:
${VARIABLE[1]}
${VARIABLE[2]}
This is the last line of the test message.
.
EOF
echo "script completed"
It worked... almost... It sent a local email to my postfix mail account that read like this:
This is a test:
The 12,345 quick brown foxes
jumped over the 67,890 lazy dogs.
This is the last line of the test message.
.
EOF
echo "script completed"
So, I have two questions. First, the easy one (I hope):
How do I delimit the end of the text, that I want to be the message body of the email, from portions of the script that follow said email text?
Next question is a little more involved. You know how, in Mail.app, if you go to Mail Preferences>Accounts>Account Information, you can put multiple email addresses, comma-delimited, in the "Email Address" field? So, if a person entered "userName@home.net, userName@work.com, userName@school.edu" in this field, then, even though (s)he may be at home, and using their home ISP's mail server, (s)he could send an email apparently from either their home, work, or school email address. Of course, the mail headers clearly would show it came from and through their home machine and home ISP, but it would be displayed in the recipient's Mail client viewer as having come from one of userName@home.net, userName@work.com, or userName@school.edu.
I'd like to do something similar here, whereby the email (that is being sent to one or more local users' postfix account on my computer) would apparently be sent from "watchdog@localhost" rather than from "jv@localhost" like it seems to do by default. Whatever account the script is run from (or presumbably, whose cron tab is launching the script) is what the "From" address is set to.
I'd rather not create an additional mail account, because I am using Mac OS X built-in accounts for the postfix mailboxes (I don't want to have to maintain a plaintext username:password file in postfix, and I don't want to create an additional user account on the computer).
So, is there a way to specify an alternate "From" username when invoking the mail -s ${SUBJECT} ${RECIPIENT} command in a bash script? Or is there a different, alternate mail command that will let me do so? (please include a description of syntax and how I'd package the above message text for the alternate method).
Thanks in advance, all!
2001 Quicksilver G4, Mac OS X (10.4.5)