How To send emails using Node.js with Nodemailer npm
In this post we will give you information about How To send emails using Node.js with Nodemailer npm. Hear we will give you detail about How To send emails using Node.js with Nodemailer npmAnd how to use it also give you demo for it if it is necessary.
How To send emails using Node.js with Nodemailer npm
In this Node.js tutorial, I will tell you how to send email in Node.js using Nodemailer module.
This makes it very easy to send emails from node application.
You can easily installed the Nodemailer module using npm command with Node.js
Nodemailer is licensed under MIT license
.
The best thing is it support unicode, that means you can use any characters including emoji.
You can send raw text as well HTML
content.
You can embed images in HTML, and also can add attachments to messages.
Nodemailer support different transport methods – SMTP, Sendmail and Amazon SES.
In this example, I am sharing simple code to send email in node application using Nodemailer module.
Installation
Run following command to install Nodemailer :
npm install nodemailer --save
Require the Nodemailer module in your js file :
var nodemailer = require('nodemailer');
- functionsendMail(to,subject,message)
- {
- var smtpConfig ={
- service:'Gmail',
- auth:{
- user:'username@gmail.com',
- pass:'xxxxxx'
- }
- };
- var transporter = nodemailer.createTransport(smtpConfig);
- var mailOptions ={
- from:'"Sender Name" <sender@gmail.com>',// sender address
- to: to,// list of receivers
- subject: subject,// Subject line
- text:'Hello world ?',// plaintext body
- html: message // html body
- };
- transporter.sendMail(mailOptions,function(error, info){
- if(error)
- {
- return console.log(error);
- }
- else
- {
- return console.log(info.response);
- }
- });
- }
- var message ='<p>This is HTML content</p>';
- sendMail('ajay.agrahari09@gmail.com','Welcome to onlinecode',message);
function sendMail(to,subject,message) { var smtpConfig = { service: 'Gmail', auth: { user: 'username@gmail.com', pass: 'xxxxxx' } }; var transporter = nodemailer.createTransport(smtpConfig); var mailOptions = { from: '"Sender Name" <sender@gmail.com>', // sender address to: to, // list of receivers subject: subject, // Subject line text: 'Hello world ?', // plaintext body html: message // html body }; transporter.sendMail(mailOptions, function(error, info){ if(error) { return console.log(error); } else { return console.log(info.response); } }); } var message = '<p>This is HTML content</p>'; sendMail('ajay.agrahari09@gmail.com','Welcome to onlinecode',message);
Hope this code and post will helped you for implement How To send emails using Node.js with Nodemailer npm. 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