Home  >  Article  >  Web Front-end  >  node.js uses nodemailer to send email examples

node.js uses nodemailer to send email examples

高洛峰
高洛峰Original
2016-12-17 12:55:541730browse

1. Install nodemailer

npm install nodemailer --save

2. Call

var nodemailer = require("nodemailer");
// 开启一个 SMTP 连接池
var smtpTransport = nodemailer.createTransport("SMTP",{
  host: "smtp.qq.com", // 主机
  secureConnection: true, // 使用 SSL
  port: 465, // SMTP 端口
  auth: {
    user: "xxxxxxxx@qq.com", // 账号
    pass: "xxxxxxxx" // 密码
  }
});
// 设置邮件内容
var mailOptions = {
  from: "Fred Foo <xxxxxxxx@qq.com>", // 发件地址
  to: "2838890xx@qq.com, minimixx@126.com", // 收件列表
  subject: "Hello world", // 标题
  html: "<b>thanks a for visiting!</b> 世界,你好!" // html 内容
}
// 发送邮件
smtpTransport.sendMail(mailOptions, function(error, response){
  if(error){
    console.log(error);
  }else{
    console.log("Message sent: " + response.message);
  }
  smtpTransport.close(); // 如果没用,关闭连接池
});

3. Common errors

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

Error reason: The account is not set up for this service
Solution: QQ mailbox-> Settings-> Account -> Enable service: POP3/SMTP service

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

Error reason: The sending account and the authentication account are different



For more node.js using nodemailer to send email examples and related articles, please pay attention to 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