g0la1q
Last Updated: February 25, 2016
·
2.717K
· jacshfr
Headshot

Emails from Node.js? Simple...maybe too simple.

Sending emails to potential clients based on interaction with a website seemed like a third party job until I found this. Nodemailer is a simple node packet that can be incorporated into your node.js file server to send emails based on user interaction with your site.

$ npm install nodemailer

Before we start, I install Nodemailer in my local directory.

var http = require('http');

function start(route, handle) {
  function onRequest(request, response) {

    console.log('request received.');
    response.writeHead(200, {"Content-Type":                          
      "text/plain"});
    response.write('Starting point.');
    response.end();
  }

http.createServer(onRequest).listen(3214);
console.log("Server has started.");
}

exports.start = start;

First I build a simple server and export the start() function.

var nodemailer = require('nodemailer');
var server = require('./server');
server.start();
// create reusable transporter object using SMTP transport
    var transporter = nodemailer.createTransport({
        service: 'Gmail',
        auth: {
            user: 'email@gmail.com',
            pass: 'password'
        }
    });

I import my server and Nodemailer. I then start the server, and, using a Nodemailer createTransport() method, I establish a connection with information from a previously created email account. This creates a reusable object that can send multiple emails to multiple addresses simultaneously or one at a time.

// setup e-mail data with unicode symbols
    var mailOptions = {
// sender address
        from: 'name ✔ <email@gmail.com>', 
// list of receivers
        to: 'friend@gmail.com',  
// Subject line
        subject: 'Testing test ✔', 
// plaintext body
        text: 'It works! ✔',
// rich text html body
        html: "<p>It works</p>",
    };

I then create the email in JSON format with all of the fields you would normally see in an email (from, to, subject, etc.). I can send as html and/or with text for plainview clients.

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        console.log(error);
    }else{
        console.log('Message sent: ' + info.response);
    }
});

Finally, using the transporter, I send the email via the method sendMail(), which sends the email and logs the outcome. Additional options include the ability to attach files and include different formats of your email for various email clients.

As you can see, with just a few simple lines of code, I can send an email with a call to the server. I could easily populate the email with user provided email or trigger specific types of emails based on user info (ex. first time user or request for specific information.)

The best part about Nodemailer is the simplicity with which it can be used. Once I discovered the package, it was just a few minutes before I was able to send emails with a simple hit on my server.

Say Thanks
Respond
Awesome Job

E4857be2 008e 11e8 8271 f03109811178
Software Engineer
·
San Francisco Bay Area (San Carlos)
·
Full Time