Matthew Gatland

Email Notifications from Node

October 22, 2014

Warning: this is an old post and this method probably won’t work any more. These days (in 2017), I use Sendgrid, a special automated email service, rather than trying to automatically send an email from Gmail.


I wanted to be notified whenever someone logged into Let’s Be Ducks. Ducks is a Node application hosted by AppFog.

I looked into Twitter first, but after reading the twitter API docs, email seemed a bit easier. Using emailjs and Gmail, I had everything working in less than an hour.

Step one: Create a Gmail account.

Step two: Add emailjs to the package.json file:

"dependencies": {
      "emailjs": "0.3.12"
    }

Step three: run npm install.

Step four: Add this code to the server:

var email = require('./node_modules/emailjs/email');

    var mailserver  = email.server.connect({
       user:    "[email protected]", 
       password: process.env.emailpassword, 
       host:    "smtp.gmail.com", 
       ssl:     true
    });

    sendEmail = function (message) {
        mailserver.send({
           text:    message, 
           from:    "Ducks Alerts <[email protected]>", 
           to:      "[email protected]",
           subject: "ducks"
        }, function(err, message) { console.log(err || message); });  
    };

    sendEmail("Testing the email system.");

The password is set through an environment variable. In AppFog, there is a UI to configure these.

At this point, the app will send me emails.

After that, I just had to configure my mailbox:

Done! Note that Gmail limits you to 99 emails a day using this method.