Home > Article > Web Front-end > How to send email from Node.js
This article will introduce to you how to send emails from the node.js application through the gmail smtp server. Let's take a look at the specific content.
First, we need to install the nodemailer package in our application. Use the following command to install this package.
$ npm install nodemailer
Now add the following code in the application to send the email. Be sure to update all required values in the code below so that the email can be sent successfully.
var nodemailer = require('nodemailer'); var mailTransport = nodemailer.createTransport('smtps://user%40gmail.com:email_password@smtp.gmail.com'); var mailOptions = { from: "Sender Name <sender@example.com>", to: "Recipient Name <recipient@example.com>", subject: "Hello World", text: "Test email with node.js" html: '<b>Test email with node.js</b>' }; mailTransport.sendMail(mailOptions, function(error, info){ if(error){ return console.log(error); } console.log('Message sent: ' + info.response); });
If you are still facing other issues sending emails through gmail stmp server, make sure you are using the correct login details. Generate the application-specific password and set the required authentication-enabled account here. Additionally, it is possible to use less secure apps within your Gmail account.
This article has ended here. For more other exciting content, you can pay attention to the node.js video tutorial column on the PHP Chinese website! ! !
The above is the detailed content of How to send email from Node.js. For more information, please follow other related articles on the PHP Chinese website!