19 December, 2016 Tags:

Sending mails with Nodejs and Nodemailer

At some point your application may need to inform the user with an email, like confirmation e-mails, password recovery emails, informational emails. There are several reasons why your application has to act as a pseudo-email client.

On this article I’ll demonstrate how to send emails using nothing other than Nodejs and the Nodemailer module.

About Nodemailer

It is a properly tested Nodejs module that allows sending emails from a nodejs application. The project has existed since 2010 and today is a standard option in the community for sending emails. It was initially developed by Andris Reinman.

Sending an email

Start by creating a directory and within it create a mail.js file:

mkdir sendmail
cd sendmail

Inside the mail.js file enter the following code:

const nodemailer = require("nodemailer");

let transporter = nodemailer.createTransport({
    host: "smtp.yourdomain.com",
    port: 465,
    secure: true,
    auth: {
        user: "[email protected]",
        pass: "yoursuperscretpassword"
    }
});

let mailOptions = {
    from: '"Your Name" <[email protected]>',
    to: "[email protected]",
    subject: "Nodejs - Testing Nodemailer ??",
    text: "This test was successful"
}

transporter.sendMail(mailOptions, (err, info) => {
    if(err){
        console.log(err);
    }else{
        console.log("Your mail was sent");
    }
});

Replace with real data and then run it:

node mail.js

If your data is correct then the terminal will return:

Your mail was sent

What really happened

Ok, in the above script the nodemailer module was imported. To send emails with it, you need a transporter, which is the object that will perform the task of sending mails. The nodemailer allows you to create transporters using the createTransport () method, which in turn receives an object with the data from your mail server as a parameter.

const nodemailer = require("nodemailer");

let transporter = nodemailer.createTransport({
    host: "smtp.yourdomain.com",
    port: 465,
    secure: true,
    auth: {
        user: "[email protected]",
        pass: "yoursuperscretpassword"
    }
});

Having the transporter created and well configured, one can already define the data of the mail, as destination, subject and body of the email. Through an object we pass these definitions as follows:

let mailOptions = {
    from: '"Your Name" <[email protected]>',
    to: "[email protected]",
    subject: "Nodejs - Testing Nodemailer ??",
    text: "This test was successful"
}

Once the transporter and the object with the mail settings are created, we can proceed with sending the mail using the sendMail() method that is available in the transporter as soon as it is created.

The sendMail() method receives as parameters the object with the mail definitions and a callback function to handle the errors and the message of success:


transporter.sendMail(mailOptions, (err, info) => {
    if(err){
        console.log(err);
    }else{
        console.log("Your mail was sent");
    }
});

Conclusion

With this basic script you can send mails from your node application. This is a rather short and objective post to demonstrate how mails are sent from Node applications. For more information I recommend reading the official documentation.

OTHER ARTICLES