Wednesday, November 18, 2009

Mutt: Scripting mail

With 'mutt' we can generate e-mails and have them sent to our default e-mail address, in this case me@domain.com
mutt -s "Subject line here" me@domain.com < /path/to/message
is the correct command for this e-mail address. You can find more details at http://www.cyberciti.biz/tips/sending-mail-with-attachment.html or the man page of mutt.

In the end, my script looks like this:
# Sends the IP address to default address
to="me"
from="noreply"
domain="domain.com"
temp=`mktemp -t`
ifconfig ppp0 > $temp
mutt -e "set from=$from@$domain" -s "IP address - $HOSTNAME" "$to@$domain" < $temp

$HOSTNAME is set based off the hostname of the computer. In my case this name is specific to the location and is unique. This helps me know which school I'm contacting.
The other main addition is the "-e" command.
If you look online you'll find that in order to send from a specific domain, you need to set up a muttrc file. Since we're running a script, and it makes more sense to keep everything together, I decided it's better to just add the "set from=" command in the script instead of an outside file.
The "-e" command lets you do this. It must be in quotes, otherwise it'll throw an error.

This is also added to the ip-up script that is run when ppp0 makes a connection.

You'll then need to make sure that postfix is installed. The best way, is to set up postfix and have it send through a designated email address. I simply set up a "noreply" email address on our domain that I would log into to send the email.
You can find instructions here: http://serverfault.com/questions/119278/configure-postfix-to-send-relay-emails-gmail-smtp-gmail-com-via-port-587/119296#119296

No comments:

Post a Comment