node 定时发送邮件

发布时间:2019-07-02 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了node 定时发送邮件脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

定时发送,可做提醒使用

nodemailer

nodemailer 是一款简单易用的基于于SMTP协议(或 Amazon SES)的邮件发送组件

cron

cron可以指定每隔一段时间执行指定的程序、也可以指定每天的某个时刻执行某个程序、还可以按照星期、月份来指定。
具体描述
npm install nodemailer -S
npm install nodemailer-smtp-transport -S
npm install cron -S

代码中有详细的注释

let nodemailer = require('nodemailer'),
    smtpTransport = require('nodemailer-smtp-transport'),
    cronJob = require('cron').CronJob;

// SMTP 连接
let transport = nodemailer.createTransport(smtpTransport({
  // 主机
  host: 'smtp.163.com',
  // 是否使用 SSL
  secure: false,
  secureConnection: false,
  // 网易的SMTP端口
  port: 25, 
  auth: {
    // 账号
    user: '***@163.com', 
    // 授权码(自行百度邮箱SMTP的授权码设置),此处非密码
    pass: '***', 
  }
}));
// 设置邮件内容
let mailOptions = {
  // 发件人地址,例如 1234<1234@163.com>
  from: '***<***@163.com>', 
  // 收件人地址,可以使用逗号隔开添加多个
  // '***@qq.com, ***@163.com'
  to: '***@qq.com', 
  // 标题
  subject: 'Hello World', 
  // 邮件内容可以自定义样式
  html: '<strong style="color: red">测试"邮件轰炸机"</strong>'
}
// 定时发送邮件
// 每秒执行一次
// 具体的各项设置查看上方的链接
new cronJob('* * * * * *', () => {
  transport.sendMail(mailOptions, (error, response) => {
    if (error) {
      console.error(error)
    } else {
      console.log('Message Send Ok')
    }
    // 记得关闭连接
    transport.close();
  })
}, null, true, 'Asia/Shanghai');

脚本宝典总结

以上是脚本宝典为你收集整理的node 定时发送邮件全部内容,希望文章能够帮你解决node 定时发送邮件所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: