Send emails from command line

One Google I/O later, I’m back. This year’s IO was even more exciting especially with Android M coming and the ability to send push notifications to your android device from the browser! (Damn :P) Anyway, I have always wanted to have a handy command line emailing utility which can be patched up in a shell script and be leveraged to do lot of exciting stuff. The idea of setting up and configuring an SMTP server is ambitious but tedious at the same time (something I’ll tinker with later :)).

After reading a lot of posts on askubuntu.com and stackoverflow.com, I decided to give Postifx a try. I’ll focus specifically on getting it up to speed with Gmail as it is the most common use case. There are tons of articles out there on getting the config right, but what worked for me was the howto given at https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/. There’s an interesting hak5 video as well on setting up postfix with linux box. The CA Certificate(s) can be obtained from https://www.thawte.com/roots/.

While it is good for testing, it didn’t seem like a reliable solution for Gmail especially because Google has now started blocking log-in attempts from what it considers “Insecure” devices. So you need to go disable that check which poses a security threat but then it will work for the most part.

I needed a more Oauth kind of fool-proof solution that works from a single command on my terminal. Enter NODEJS! There are a bunch of emailing utilities built for NodeJs but the most popular and easiest to use for me was Nodemailer. So I decided to go with this.

The simplest use case for this would be:

 

However, the mails might not go through because of the added security feature by Google as mentioned before. Thankfully, the developers provides another package XOauth2 which can be used with nodemailer to send emails accessing the Gmail API the ‘proper’ way.

Let’s call the file that sends emails using the gmail API as ‘sendmail.js’. The file looks something like this:

So , in essence we just need to plugin values for clientId, clientSecret and refreshToken. We need to create a project on the Google Developers Console. Give the project any suitable name and configure it as a web-app. Enable access for the Gmail API by going to APIs. The important part to get the refreshToken from your local machine is to set the Redirect URIs and JavaScript origins to your local server. I’ll be using an express server for this example running on localhost:3000. So the credentials screen for my project looks like:

googleDevelopersConsole

Now that we have our clientId and clientSecret fields, accessing the refreshToken is a two fold process detailed here in the official documentation:

  • Make a get request to “https://accounts.google.com/o/oauth2/auth?redirect_uri=http%3A%2F%2F127.0.0.1%3A3000&response_type=code&client_id=<yourClientId>&scope=https%3A%2F%2Fmail.google.com%2F&approval_prompt=force&access_type=offline”. The redirect_uri and client_id have to be configured as per the credentials provided in our project description on the developer console. When this link is embedded in our server (I just plugged it in the index.jade view). When you click on that link, you’ll be prompted to allow access to your account and once you do that you should see an Authorization Code in your DEBUG console as the result of that response or in the address bar of your browser.
    AuthorizationCode
  • Once we have the authorization code we need to make a POST request with the given credentials to obtain a refreshToken. I plugged it in the ‘users’ route of my server code:

     

    RefreshToken

That’s about it. When you go to localhost:3000/users after plugging in the required values. You should see something like the following in your DEBUG console. Make note of the refreshToken and plug-in the three values in ‘sendmail.js’ file we created initially. We have successfully configured nodeJs and authorized it to create OAuth Tokens for us using the refreshToken and sending emails is as simple as doing this:

We can have the “To”, “Subject” and “Body” fields be fetched from the command line args as well and it should be a fun exercise for someone looking to experiment with this code snippet. NodeJs is indeed a really powerful utility right now that makes writing server code a cake walk because of the sheer ease of learning Javascript and wide acceptability of it. I just stumbled across another amazing project that combines NodeJs and Webkit letting us write Desktop Applications in HTML5! Stay tuned for that :)