Home  >  Article  >  Web Front-end  >  node.js uses nodemailer to send emails example_javascript skills

node.js uses nodemailer to send emails example_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:56:181255browse

1. Install nodemailer

Copy the code The code is as follows:
npm install nodemailer --save

2. Call
Copy code The code is as follows:
var nodemailer = require("nodemailer");

//Open an SMTP connection pool
var smtpTransport = nodemailer.createTransport("SMTP",{
host: "smtp.qq.com", // Host
secureConnection: true, // Use SSL
port: 465, // SMTP port
auth: {
user: "xxxxxxxx@qq.com", // account number
pass: "xxxxxxxx" // password
}
});

//Set email content
var mailOptions = {
from: "Fred Foo ", // Sending address
to: "2838890xx@qq.com, minimixx@126.com", // Recipient list
subject: "Hello world", // Title
html: "thanks a for visiting! Hello world! " // html content
}

//Send mail
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " response.message);
}
smtpTransport.close(); // If useless, close the connection pool
});


3. Common Errors
Copy code The code is as follows:

{ [AuthError: Invalid login - 454 Authentication failed, please open smtp flag first!]
name: 'AuthError',
data: '454 Authentication failed, please open smtp flag first!',
stage: 'auth' }

Cause of error: The service is not set up in the account
Solution: QQ Mail-> Settings-> Account-> Enable service: POP3/SMTP service

Copy code The code is as follows:

{ [SenderError: Mail from command failed - 501 mail from address must be same as authorized user]
name: 'SenderError',
data: '501 mail from address must be same as authorization user',
stage: 'mail' }

Error reason: The sending account and the authentication account are different
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