Monday, March 9, 2009

Simple Setup for Sending Email from the Command-Line in Ubuntu

In setting up my new Ubuntu box, I wanted to set up some automatic backups. I did that, putting the backup script in a cronjob. But what if the backup fails? How will I know, unless I remember to check on it every day? How can I send myself an email from the script when there's an error?


Email is logical choice, but nothing was installed already that would allow me to send email from the command line, and hence, from a script. I just want to use my ISP instead of creating some sort of complicated configuration. In other words, it doesn't make sense to me to host an SMTP server.

This is a really simple idea, except that, in typical Linux fashion, there are 14 different ways to set up email, approximately :).

After doing some searching and reading, here's what I came up with: msmtp and mailx

Install msmtp and mailx:
sudo apt-get install msmtp mailx

Setup your msmtprc file:
cat >~/.msmtprc <<END
account rr
host smtp-server.my-isp.com
from waters@my-isp.com
user waters
password my-password

account default : rr

END
Make sure it's owner-only:
chmod 600 ~/.msmtprc 

Send an email!
echo "test message" | mailx -s "test" my_yahoo_email@yahoo.com 

I have to give some credit to the following blog. It's basically the same thing, but he's adding in gmail as the SMTP server. The msmtp man page also has some information on this.

http://www.klenwell.com/press/2009/03/ubuntu-email-with-mailx/

No comments: