In this article, we will see how to send email using node js. In this tutorial, we will see send mail in node.js using nodemailer module. The nodemailer module makes it easy to send emails in node.js, with the help of the nodemailer example, we will send emails with basic HTML content.
So, let's see how to send mail using nodemailer in node js, sending mail in node js using nodemailer, send mail using mailtrap in node js, how to send email in node js express, how to send email using nodemailer with mailtrap, node js send an email with mailtrap, how to send mail using SMTP in node js, how to send SMTP mail in node js.
NodeMailer is very famous and easy to use for sending emails in nodejs. Also, We can use Mailtrap accounts.
In this step, we will create a node js application using the below commands.
mkdir send_email_in_nodejs
cd send_email_in_nodejs
npm init
In this step, we will install the NodeMailer module using the below command
npm install nodemailer
After you have downloaded the Nodemailer module, you can include the module in any application:
const nodemailer = require('nodemailer');
Nodemailer’s API is simple and requires the following points:
If you don’t have a Mailtrap account, follow these steps:
If already have a Mailtrap account then integrate Nodemailer in SMTP Settings like the below screenshots.
In this step, we will send an email with HTML content with configuration.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: "smtp.mailtrap.io",
port: 2525,
auth: {
user: "your_username",
pass: "******"
}
});
var mailOptions = {
from: 'from_email@gmail.com',
to: 'to_email@gmail.com',
subject: 'How To Send Email Using Node.js - Techsolutionstuff',
html: '<h1>Welcome, Techsolutionstuff</h1></br><p>This is test mail..!</p>'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
And if you want to send an email with text then change in mailOptions in HTML like the below code.
var mailOptions = {
from: 'from_email@gmail.com',
to: 'to_email@gmail.com',
subject: 'How To Send Email Using Node.js - Techsolutionstuff',
text: 'Welcome to Techsolutionstuff!', // Plain text body
};
Now, run index.js using the below code.
node index.js
You might also like: