Send email from node.js application
By: William Alexander in node.js Tutorials on 2018-08-04
In this tutorial, we will learn how to send emails from node.js applications. In any application, sending out alerts and notifications via email becomes an integral part. Node.js has a package for sending mails and it is called nodemailer.
What is npm?
npm is an acronym for node.js package manager. There are thousands of free packages available for download from npmjs.com. As part of node.js installation, npm is also installed automatically. Packages are nothing but JavaScript libraries and also called modules. While node.js default installation comes with many in-built packages, often times you may need to download and install packages that are used in some use cases. Sending emails from a node.js application is an example.
To install a new package, just run the command 'npm install <package-name>' in the command prompt.
C:\Users\username>npm install nodemailer
This will automatically download and install nodemailer package that can be used from your node.js applications by calling the 'require()' command.
Sending emails from node.js application
Create a new file named 'sendemail.js'
var nodemailer = require('nodemailer'); var transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: '[email protected]', pass: 'yourpassword' } }); var mailOptions = { from: '[email protected]', to: '[email protected]', subject: 'Sending Email from Node.js', html: '<h1>Hello friend</h1><p>This is an html formatted email content</p>'; }; transporter.sendMail(mailOptions, function(error, info){ if (error) { console.log(error); } else { console.log('Email sent: ' + info.response); } });
As usual, you can run this script by running the command 'node sendemail.js'
In this script, we just used the nodemailer package and created a transporter using a gmail account. So you have to replace the [email protected] and yourpassword with your actual gmail username and password. So the nodemailer will use your gmail account to send the email. You can send html formatted email as shown in the example. If you just want to send text format, just replace the 'html' with 'text'. example text: 'This is a normal text email content';
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in node.js ) |
Latest Articles (in node.js) |
Comments