How to send an email with Node.js
In this post we will give you information about How to send an email with Node.js. Hear we will give you detail about How to send an email with Node.js And how to use it also give you demo for it if it is necessary.
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.
Also see: 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:
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
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,<h3><br/><h3>This is amazing.</h3>'
};
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,<h3><br/><h3>This is amazing.</h3>',
attachments:[{
filename:"test-attachment.png",
path:"https://homepages.cae.wisc.edu/~ece533/images/arctichare.png"
}]
};
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.
Hope this code and post will helped you for implement How to send an email with Node.js. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs