Home  >  Article  >  Web Front-end  >  How to send email from Node.js

How to send email from Node.js

不言
不言Original
2019-03-29 14:14:202597browse

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.

How to send email from Node.js

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: &#39;<b>Test email with node.js</b>&#39;
};
mailTransport.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log(&#39;Message sent: &#39; + 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn