Convenient way to send email from a script

Dan Coutu coutu at snowy-owl.com
Sat Nov 15 11:44:05 EST 2003


I worked this out yesterday and thought others might find it useful.

I needed to be able to send email via a shell script where the script
was operating on behalf of any number of users on the system. The script
was running under a login different from that of the individuals using
it.

As a consequence all email sent using 'mail' or 'Mail' was seen as
coming from the user under which the script ran, the individual users
had no proof that the mail ever got sent, and the recipients were
confused as to who the mail was really from.

Here's the no-code solution to the dilemna, use mutt like this:

Mutt allows you to define an EMAIL environment variable to specify
the From: address. If you define it as just a user like foo at bar.com
then it will format the from address (asuming the account running
the script is called 'sharedapp') like this:

From: sharedapp <foo at bar.com>

Well, this is probably not what you want. But if you do this:

EMAIL="Foo Bar <foo at bar.com>"

Then the from address will be a thing of beauty like this:

From: Foo Bar <foo at bar.com>

So that established the sender stuff. Now how about the other pieces?
Well we can tell mutt to send a CC to whoever we like. The subject can
also be specified on the command line, and it nicely reads from STDIN
or from a file. So commands like this are useful:

cat mymessage | mutt -c foo at bar.com -s "This is a test message" \
baz at bar.com

mutt -c foo at bar.com -s "This is a test message" baz at bar.com < mymessage

Or you can use the -f switch on mutt to specify a file instead of
using input redirection.

Hope others find this useful too!

-- 

Dan Coutu
Managing Director
Snowy Owl Internet Consulting, LLC
http://www.snowy-owl.com/







More information about the gnhlug-discuss mailing list