Sending an email from the terminal using javascript

For those interested, this is how I went about sending an email from a javascript script.  I used NodeJS to process the javascript.  I used the mailer module (node_mailer) to send the email through SMTP.  I used the SendGrid SMTP service.

STEP 1 - Get NodeJS

I decided to use the apt-get package manager for installing NodeJS because it is easy.

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

STEP 2 - Get NPM (node package manager)

I had no problems installing NPM with the package managed NodeJS.

curl | sh

STEP 3 - Get node mailer module

Installing the mailer module (node_mailer) is easy enough using npm but I had some problems with the specific version which I downloaded.  First, to install the mailer module:

npm install mailer

If you happen to get the version of node_mailer which I had, version 0.6.6 you may need to change the version of nodemailer (not to be confused with node_mailer). The issue is described here:  This is how I did changed versions.

cd node_modules/mailer
npm install nodemailer@0.1.20

STEP 4 - Setup Sendgrid or another SMTP service

STEP 5 - Write script

Following the examples from a couple of different websites I was able to craft a script which sent an email the way I wanted.  Here is an example of my script:

#!/usr/bin/env node

// Get the first parameter which is the log file
var logFile = process.argv[2]

// Get the back log for the email
var fs = require('fs');
var emailLog = fs.readFileSync(logFile).toString()
var email = require('mailer')

  host : "",                   // smtp server hostname
  port : "25",                                  // smtp server port
  domain : "",             // domain used by client to identify itself to server
  to : "",
  from : "",
  subject : "Backup Log - " + new Date(),
  body: emailLog,
  authentication : "login",                     // auth login is supported; anything else is no auth
  username : "",           // Base64 encoded username
  password : "yourPassword"                         // Base64 encoded password

function(err, result){
  if(err){ console.log(err); }
I did a little bit of parameter and file processing so I could use a different log file when the script is called, but that is of course optional.

STEP 6 - Run the script

Make sure to set execution permissions. 
chmod +x emailScript.js
Then run.
./emailScript.js /root/log/logFile.log