nodejs邮件发送

nodejs邮件发送

需要的包

nodemailernodemailer-smtp-transport

安装:

npm install nodemailer --save
npm install nodemailer-smtp-transport --save

实现代码:

var nodemailer = require("nodemailer");
var smtpTransport = require('nodemailer-smtp-transport');

var email = function (username,email) {
    // 开启一个 SMTP 连接池
    var transport = nodemailer.createTransport(smtpTransport({
      host: "smtp.163.com", 
      secure: true, 
      port: 465, // SMTP 端口
      auth: {
        user: "[email protected]", // 账号
        pass: "xxx" // 授权码或者密码
      }
    }));

    // 设置邮件内容
    var mailOptions = {
      from: "***<[email protected]>", // 发件地址, 收件人显示的发件人即为***<[email protected]>,***可以改为需要的信息
      to: email, // 收件列表
      subject: "xxxx", // 标题
      html: "hello" // html 内容
    }

    // 发送邮件
    transport.sendMail(mailOptions, function(error, response) {
      if (error) {
        console.error(error);
      } else {
        console.log(response);
      }
      transport.close(); // 如果没用,关闭连接池
    });
}

exports = module.exports = email;

猜你喜欢

转载自blog.csdn.net/lllllyt/article/details/80592731