How To Send Email in Nodejs With Nodemailer

How To Send Email in Nodejs With Nodemailer


Jan 19 2024

When it comes to sending emails in Nodejs applications, nodemailer is the most popular package for this purpose.

Let's take a look at it.

What is Nodemailer?

Nodemailer is an easy-to-use library that allows sending email in Nodejs applications. It provides a simple and powerful API for sending email using different transport methods, such as SMTP, Sendmail, and Amazon SES.

In addition, it supports multiple email services including Gmail, Yahoo, and Outlook offering a user-friendly interface and providing a flexible set of APIs that can be used to configure email messages.

Now, let's see the implementation of it in Nodejs.

Creating A Transporter

Firstly install nodemailer with npm or yarn and import it into your application. Then, create a transporter object that provides reusable component for sending a email.

This object will include informations about host and credentials for sender email.

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    auth: {
        user: 'example@gmail.com',
        pass: 'password'
    }
})

If you want to set different hosts, such as Yahoo and Outlook, you can use the following host values:

 gmail: "smtp.gmail.com"
 yahoo: "smtp.mail.yahoo.com"
 outlook: "smtp.office365.com"

Sending Text Email

Now, you can send email with the Nodemailer sendEmail method that accepts an object about the message that will be sent, the message object includes:

  • the sender email address,
  • the recipient email address,
  • the email subject,
  • the email type (text or html),
  • the email content,
  • an array of attachments.
let message = {
    from: 'example@gmail.com',
    to: 'recipient@example.com',
    subject: 'Test Email with Attachment',
    text: 'Hello, this is a test email with an attachment!',
    attachments: [
        {
      filename: `file.pdf`,
      path: path/to/file,
      cid: `index`,
        }
    ]
};

transporter.sendMail(message, (err, info) => {
    if (err) {
        console.log(err);
    } else {
        console.log('Email sent: ' + info.response);
    }
});

Sending HTML Email

Nodemailer also allows sending emails with HTML content. To send an email with HTML content, simply add your HTML to the message object.

let message = {
    from: 'example@gmail.com',
    to: 'recipient@example.com',
    subject: 'Test Email with HTML Content',
    html: ‘<h1>Hello</h1>'
};

transporter.sendMail(message, (err, info) => {
    if (err) {
        console.log(err);
    } else {
        console.log('Email sent: ' + info.response);
    }
});

Conclusion

Overall, this guide provides a straightforward and flexible way to send emails using Nodejs with the nodemailer package, with support for different email providers, authentication, and attachment options.

If you want to learn more about nodejs, then you can check the following articles:

Thank you for reading.