简单两步使用node发送qq邮件

node发送邮件很是简单,这里只作qq的演示,你能够触类旁通.

使用nodemailer包html

let transporter = nodemailer.createTransport({
  // 使用qq发送邮件
  // 更多请查看支持列表:https://nodemailer.com/smtp/well-known/
  service: 'qq',
  port: 465, // SMTP 端口
  secureConnection: true, // 使用了 SSL
  auth: {
    user: '751734566@qq.com',
    // 这里密码不是qq密码,是你设置的smtp受权码
    // 获取qq受权码请看:https://jingyan.baidu.com/article/6079ad0eb14aaa28fe86db5a.html
    pass: 'xxxxxxxx',
  }
});

接下来咱们设置咱们到发送内容node

let mailOpt= {
  from: '"test" <xxxxxx@qq.com>',  // 你到qq邮箱地址
  to: 'xxxx@qq.com', // 接受人,能够群发填写多个逗号分隔
  subject: 'Hello', // 主题名(邮件名)
  // 能够发送text或者html格式,2选1
  // text: 'Hello world?', // 纯文本
  html: '<b>Hello world?</b>' // html
};

若是咱们想发一个稍微漂亮到邮件怎么办?
咱们能够使用html模板来实现git

const template = require('art-template');

let html = template(__dirname + '/mail_temp.html', obj) // mail_temp.html为你想使用到页面模板,obj为你的参数

// 例如
obj = {
    name : 'test',
    phone : '183xxxxxxxx',
    time : new Date()
}
<section>
    新用户:{{name}}({{phone}})于{{time}}进行了注册.
</section>

全部准备完成,让咱们发送邮件吧!

// 执行发送
transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    return console.log(error);
  }
  console.log('邮件已发送成功,邮件id: %s', info.messageId);
});

文档参考
nodemailer : https://www.npmjs.com/package...
art-template : https://aui.github.io/art-tem...github

相关文章
相关标签/搜索