Comprehensive Guide to Using Nodemailer for Sending Emails in Node.js

Comprehensive Guide to Using Nodemailer for Sending Emails in Node.js

Welcome to our thorough guide on Nodemailer, a powerful module for sending emails using Node.js. In this guide, we will introduce Nodemailer, discuss its various APIs, and provide plenty of code examples to help you get started quickly.

What is Nodemailer?

Nodemailer is a module for Node.js applications that allows easy email sending. It works with a variety of email services and includes features like sending HTML emails, attachments, and more.

Setting Up Nodemailer

  const nodemailer = require('nodemailer');

  let transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
          user: 'your-email@gmail.com',
          pass: 'your-email-password'
      }
  });

Sending a Basic Email

  let mailOptions = {
      from: 'your-email@gmail.com',
      to: 'recipient-email@gmail.com',
      subject: 'Hello from Nodemailer',
      text: 'This is a test email sent from Node.js using Nodemailer'
  };

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

Sending HTML Emails

  let mailOptions = {
      from: 'your-email@gmail.com',
      to: 'recipient-email@gmail.com',
      subject: 'Hello from Nodemailer',
      html: '

Hello

This is a test email sent from Node.js using Nodemailer

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

Adding Attachments

  let mailOptions = {
      from: 'your-email@gmail.com',
      to: 'recipient-email@gmail.com',
      subject: 'Hello from Nodemailer',
      text: 'Please find the attachment below.',
      attachments: [
          {   // File on disk as an attachment
              filename: 'text.txt',
              path: '/path/to/file.txt' // stream this file
          }
      ]
  };

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

Using SMTP Configuration

  let transporter = nodemailer.createTransport({
      host: 'smtp.mailtrap.io',
      port: 587,
      auth: {
          user: 'your-username',
          pass: 'your-password'
      }
  });

Complete Application Example

  const express = require('express');
  const nodemailer = require('nodemailer');
  const app = express();
  const port = 3000;

  app.get('/send', (req, res) => {
      let transporter = nodemailer.createTransport({
          service: 'gmail',
          auth: {
              user: 'your-email@gmail.com',
              pass: 'your-email-password'
          }
      });

      let mailOptions = {
          from: 'your-email@gmail.com',
          to: 'recipient-email@gmail.com',
          subject: 'Hello from Nodemailer',
          text: 'This is a test email sent from Node.js using Nodemailer'
      };

      transporter.sendMail(mailOptions, function(error, info){
          if (error) {
              return res.status(500).send(error.toString());
          }
          res.send('Email sent: ' + info.response);
      });
  });

  app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`);
  });

By following the examples in this guide, you should now have a strong understanding of how to use Nodemailer to send emails from a Node.js application. Whether you need to send plain text emails, HTML emails, or emails with attachments, Nodemailer has you covered.

Hash: 67f58c00c7d5bc0382f4eae9d3fccb4e1706a867d3f2cc2f65e36794bc32202b

Leave a Reply

Your email address will not be published. Required fields are marked *