Sending email on the command line is nothing new; In fact, it is not even all that exciting. When I got Nagios up and running at home, I needed an easy way to have it email me whenever there was an alert. When I worked at Voxer as an Operations Engineer I created a program to allow Nagios to generate HTML emails - now all I needed at home was to allow outbound email.

This, however, turned out to be more difficult than I thought, mostly because I was (and largely, still am) ignorant to the intricacies of the various email protocols in use today on the Internet. Joyent released a blog post when I was setting this all up that went over the various options for sending email from a SmartOS server. One of the options they mentioned but didn’t choose to implement really caught my eye: Google’s free SMTP server. Since my main email address is a gmail address (well, Google Apps), this route seemed perfect.

gmailx

gmailx was born! From the documentation:

Send email easily on the command line without running a server

This program is basically mailx but hardwired for Gmail (hence the name). Usage is simple:

$ cat message.txt
To: test@gmail.com
Subject: hello there!

This is a test!
$ cat message.txt | gmailx -t
message sent: 250 2.0.0 OK 1436563766 e109si12320482qgf.118 - gsmtp

See the project page for more information

https://github.com/bahamas10/node-gmailx

issues

This program worked perfectly as-is on my home/residential ISP. However, when setting it up at my parents house (for a Raspberry Pi that emails if water is detected on the floor), the connection to Gmail kept timing out.

After some debugging I realized that their ISP was blocking connections on port 25, so an alternative method must be used.

Using the nodemailer module for Node, this was accomplished by allowing a JSON config to be passed with -C to be passed directly to the createTransport method of nodemailer. So now:

$ cat config.json
{
  "service": "gmail",
  "auth": {
    "user": "sender@gmail.com",
    "pass": "password"
  }
}
$ echo hi | gmailx -C config.json -s 'subject' recipient@example.com

License

All code mentioned above is released under the MIT License