How to send an email with Node.js - TechvBlogs

How to send an email with Node.js

Email is one of the most used tools for communication in web applications because it helps you reach your users directly and build your brand.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

3 years ago

TechvBlogs - Google News

Nodejs is a server-side javascript runtime environment built on the chrome v8 engine. It allows developers to use javascript on the server-side.

Nodejs is cross-platform. It is available for Windows, Linux, and Mac. As a part of this tutorial, Ubuntu 20.04 and Nodejs 14.15.0 versions are used to code and send mail.

In this tutorial, you'll learn how to send emails with HTML content and attachments using the  nodemailer  module, as well as set up  Mailtrap , a fake SMTP server, for testing your code.

Prerequisites

To follow along, you will need to have  Node.js  and  npm  (Node Package Manager) installed locally.

To test sending emails from a local development machine, without having to configure a server, we will be using Mailtrap.

Read Also:  How to Install Node.js and NPM On Ubuntu 20.04

Getting Started

Let's npm init before installing our packages!

npm init -y

The  -y flag provided will skip the step-by-step tool to scaffold out your project.

Now let's install the  nodemailer module using  npm:

npm install nodemailer

With the model ready for use, let's create an  index.js file in our project directory.

In Node.js, the  require syntax is used to load modules in to your code:

const nodemailer = require('nodemailer');

Using SMTP for Nodemailer Transport

The  Simple Mail Transfer Protocol  (SMTP) is a protocol for sending e-mail messages between servers. Most e-mail systems that send mail over the Internet supports SMTP-based sending.

Creating a  nodemailer transport is as easy as calling the following method with a few parameters:

let transport = nodemailer.createTransport(options[, defaults])

Testing our Code with Mailtrap

Mailtrap is a "fake SMTP server" used for development purposes. Instead of having to test your code with your own email account, and potentially flooding your inbox with test emails, you can instead use Mailtrap as the endpoint.

Create a new account on Mailtrap if you don't already have one, and then create a new inbox and get your credentials:

How to send an email with Node.js - TechvBlogs

Now, all we need to do is put the credentials into  nodemailer's transport object:

let transport = nodemailer.createTransport({
    host: 'smtp.mailtrap.io',
    port: 2525,
    auth: {
       user: '<USERNAME HERE>',
       pass: '<PASSWORD HERE>'
    }
});

With all of that setup, we can go ahead and send our first test email:

const message = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Test Mail From Nodemailer',
    text: 'Hello, This is amazing.'
};
transport.sendMail(message, function(err, info) {
    if (err) {
      console.log(err)
    } else {
      console.log(info);
    }
});

Run this code using the following command:

node index.js

How to send an email with Node.js - TechvBlogs

Sending an HTML Email

Nowadays, emails tend to be styled, colorful, with big buttons and exciting and engaging visuals. To achieve this, we embed HTML into our emails.

You can create the HTML emails and send them just as easily as you could send a plain text email. All it takes is to use the  html parameter in the  message object instead of  text:

const message = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Test HTML Mail From Nodemailer',
    html: '<h1>Hello,<h2><br/><h2>This is amazing.</h2>'
};

How to send an email with Node.js - TechvBlogs

Sending Email with an Attachment

You can also send attachments by mail.

const message = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Test Mail with Attachment From Nodemailer',
    html: '<h1>Hello,<h2><br/><h2>This is amazing.</h2>',
    attachments:[{
        filename:"test-attachment.png",
        path:"https://homepages.cae.wisc.edu/~ece533/images/arctichare.png"
    }]
};

How to send an email with Node.js - TechvBlogs

And you can use a file on disk as an attachment too. They don't need to be web-based.

If you have any queries or doubts about this topic please feel free to  contact us . We will try to reach you.

Comments (0)

Comment


Note: All Input Fields are required.