send emails from terminal

28 Nov 2010

In some systems the command mail is installed by default and as its name suggests it’s used to send/read emails (usually between users of the same system), for this to work a mail server needs to be installed locally, I’m not sure about others but it sounds like a lot of work for me. I just want a light client from where I could send some emails. After searching on Internet I came to Sendemail, a perl script who connects to external smtp servers and use them to deliver messages:

$ sudo apt-get install -y sendemail libio-socket-ssl-perl libnet-ssleay-perl
$ sendemail -f from@foo.org \
  -u title -m message       \
  -t to@bar.com             \
  -s mail.foo.com:26        \
  -xu user -xp password

Or with gmail:

$ sendemail -f from@foo.org \
  -u title -m message       \
  -t to@bar.com             \
  -s smtp.gmail.com:2587    \
  -o tls=yes -xu user -xp password

Now, since gmail blocks hosts per ip, it sometimes doesn’t work when it used from new locations, it can be very annoying. Fortunately, there are other ways to send emails within a system, my favorite method is to use http://mailgun.com. When using mailgun you only need an account in such service and curl installed in your system. I’ve created a script who wraps the required logic and just send emails.

$ wget https://raw.github.com/javier-lopez/learn/master/sh/tools/mailgun
$ sh mailgun --api xxx "address@to.com" "message"