nodejs
  1. nodejs-email

Node.js Email

In Node.js, there are several packages available to send email using SMTP (Simple Mail Transfer Protocol) protocol. In this tutorial, we'll discuss how to send emails using the Nodemailer package.

Syntax

The basic syntax for sending an email using Nodemailer is as follows:

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        user: 'youremail@gmail.com',
        pass: 'yourpassword'
    }
});

let mailOptions = {
    from: 'youremail@gmail.com',
    to: 'myfriend@yahoo.com',
    subject: 'Sending Email using Node.js',
    text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
    if (error) {
        console.log(error);
    } else {
        console.log('Email sent: ' + info.response);
    }
});

Here, we have created a transporter object using our email credentials and email provider details. We have then created a mailOptions object that contains the details of the email we want to send. Finally, we have used the transporter object to send the email, and logged the response or error in the console.

Example

Let's say we want to send an email from our Gmail account to a friend using Nodemailer. Here's how we can implement it:

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        user: 'youremail@gmail.com',
        pass: 'yourpassword'
    }
});

let mailOptions = {
    from: 'youremail@gmail.com',
    to: 'myfriend@yahoo.com',
    subject: 'Sending Email using Node.js',
    text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
    if (error) {
        console.log(error);
    } else {
        console.log('Email sent: ' + info.response);
    }
});

Output

When we run the example code above, the output will be:

Email sent: 250 2.0.0 OK  1628792570 d4sm20850509pfi.64 - gsmtp

This is because the email was sent successfully and the info.response value contains the server response.

Explanation

In the example above, we have used the Nodemailer package to send an email from a Gmail account to a friend's Yahoo account. We created a transporter object using the SMTP details of our Gmail account, and a mailOptions object containing the details of the email we want to send.

We then used the transporter object to send the email, passing the mailOptions object as a parameter to the function. Finally, we logged the response or error in the console.

Use

Sending email using Node.js can be useful for a variety of reasons, such as sending notifications, alerts, or newsletters. You can use Nodemailer to send one-off emails, or you can use it to send bulk emails.

Important Points

  • Be sure to use a secure and reliable SMTP server to send emails.
  • Always sanitize user inputs to prevent email injection attacks.
  • Consider using email templates to make your emails more consistent and professional.

Summary

In this tutorial, we discussed how to send emails using Node.js and the Nodemailer package. We covered the syntax, example, output, explanation, use, and important points of sending email using Node.js. With this knowledge, you can now send emails using Node.js to provide timely notifications, alerts, or newsletters to your users.

Published on: