sending email using bash script

Hello:

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)

Posted on Mar 28, 2006 2:01 PM

Reply
9 replies

Mar 28, 2006 9:13 PM in response to Nils C. Anderson

Well, I may have been a little too quick on the helpful button. Putting the stuff from Mail -s to EOF in my script didn't work, and trying what you have in Terminal command line gives:

Quicksilver:~ jv$ Mail -s "test mesg" jv <<EOF >
Test Message.
${VAR1} ${VAR2}
EOF

-bash: syntax error near unexpected token `newline'
Quicksilver:~ jv$

Mar 28, 2006 9:21 PM in response to Community User

and I think I was a little too quick on the helpful button -- so still need some assistance...

This script:
#!/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"

gives this mail:
This is a test:
${VARIABLE[1]}
${VARIABLE[2]}
This is the last line of the test message.
EOF
echo "script completed"

Rolling back to:
mail -s "a test email" jv <<EOF
restored the in-line variable expansion, so for whatever reason, putting 'EOF' vs EOF hosed it. Space or no space between << and EOF in the "mail -s" line has no effect. Arrrrggggggh!

Mar 28, 2006 10:08 PM in response to j.v.

well some progress has been made. It's kinda kludgey, but this shell script:

#!/bin/bash

VARIABLE[1]="The 12,345 quick brown foxes "
VARIABLE[2]="jumped over the 67,890 lazy dogs."
echo "This is a test:
${VARIABLE[1]}
${VARIABLE[2]}
This is the last line of the test message." | mail -s "a test email" jv

echo "script completed"

does this in Terminal:
Quicksilver:~ jv$ sh test.sh
script completed
Quicksilver:~ jv$

and I get an email from jv (from myself) that says:

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.

So it's starting to look like all I have to do now is figure out how to change the "From:" line so it appears that the mail comes from what is essentially an email alias ("watchdog@myDomain.org") instead of from the Mac OS X account short user name.

Mar 30, 2006 1:05 AM in response to j.v.

Hi j.v.,

The > after EOF is just a typo (or may be added by the Discussion ?) and you must delete it; other > are prompts from the interactive shell. Andy's post shows an interactive use of shell, not a shell script (note the shell prompt % in front of the commands). A typical use of here document may look like

command <<ENDOFDATA
....
....
ENDOFDATA

There must be no spaces before and after ENDOFDATA. The word ENDOFDATA can be EOF or any other string which is guaranteed not to appear in the text (the .... in the example above).

You can modify the From: header by using sendmail command (postfix has it as a compatibility interface):

/usr/sbin/sendmail -t <<EndOfMessage
Subject: test mail
To: jv
From: watchdog
This is a test:
${VARIABLE[1]}
${VARIABLE[2]}
This is the last line of the test message.
EndOfMessage

There must be a blank line between the headers and the mail body.

I assume that you send these mails only to users on your local Mac. Please do not send mails to remote users by using the sendmail command unless you know what you are doing completely.

PowerMac G4 Mac OS X (10.4.5)

Mar 30, 2006 10:58 AM in response to Jun T.

Hello Jun:

I cut and pasted your script into a new script to see if it worked. It does. Awesome! This is exactly what I was wanting to do. Problem solved.

I had seen a discussion about that sendmail -t somewhere while doing a google search, but it seems like something in the syntax of the example I found, for multi-line messages, was goofy. And when I went looking for it again, I could never it.

Your assumption as to how I will use this information is correct. It is strictly for use on my own "localhost" subnet and my own personal postfix-enabled mail account. There is no intent to ever use it for any email that goes off my 127.0.0.1 subnet.

Thanx for the help!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

sending email using bash script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.