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
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
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)
http://npmjs.org/
I had no problems installing NPM with the package managed NodeJS.
curl http://npmjs.org/install.sh | sh
STEP 3 - Get node mailer module
http://blog.nodejitsu.com/sending-emails-in-node
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: https://github.com/Marak/node_mailer/issues/41. 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
http://ubuntuforums.org/showpost.php?p=10525545&postcount=50
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') email.send({ host : "smtp.sendgrid.com", // smtp server hostname port : "25", // smtp server port domain : "serverDomain.com", // domain used by client to identify itself to server to : "someone@somewhere.com", from : "server@serverDomain.com", subject : "Backup Log - " + new Date(), body: emailLog, authentication : "login", // auth login is supported; anything else is no auth username : "yourUsername@emailService.com", // Base64 encoded username password : "yourPassword" // Base64 encoded password }, function(err, result){ if(err){ console.log(err); } });
STEP 6 - Run the script
chmod +x emailScript.js
./emailScript.js /root/log/logFile.log