node自动发送邮件

504 阅读2分钟

利用nodemailer

//  使用nodejs 自动发送邮件,利用nodemailer  https://nodemailer.com/about/
// gitcoding@163.com
// npm install nodemailer
// 需要在 打开pop3 sMTP IMAP 服务

// "use strict";
const nodemailer = require("nodemailer");
// 定时任务
const schedule = require('node-schedule')
function random(max,min){
  return Math.round(Math.random()*(max-min)+min);
}
const math = random(1000, 9999)
const ToEmail = "2228429156@qq.com, 2228429150@qq.com"
// async..await is not allowed in global scope, must use a wrapper
async function main() {
  // Generate test SMTP service account from ethereal.email
  // Only needed if you don't have a real mail account for testing
  // let testAccount = await nodemailer.createTestAccount();

  // create reusable transporter object using the default SMTP transport
  let transporter = nodemailer.createTransport({
    host: "smtp.163.com",
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
     // user: testAccount.user, // generated ethereal user
     // pass: testAccount.pass, // generated ethereal password
     user: 'gitcoding@163.com',
     pass: '88888888',
    },
  });

  // send mail with defined transport object
  let info = await transporter.sendMail({
    from: '"GitCoding" <gitcoding@163.com>', // sender address
    // to 逗号分隔的列表或收件人的电子邮件地址的排列
    to: ToEmail, // list of receivers
    // // Subject line 消息主题
    subject: "欢迎注册GitCoding", 
    // cc 逗号分隔的列表或将显示在“抄送”字段中的收件人电子邮件地址数组
    cc: '2228429150@qq.com', 
    text: "Hello world?", // plain text body
    // 如果定义了html,则会忽略掉text
    html: `欢迎注册GitCoding, 您的邮箱验证码是:<b>${math}</b>`, // html body
    // attachments 附件内容
    attachments: [{
      // 当前目录下的文件
      "filename" : "fujie.js",
      "path": './index.js'
    },{
      // 创建文件
      filename: 'content.txt',
      content: '发送内容'
    }]
  });

  // console.log("Message sent: %s", info.messageId);
  // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>



  // Preview only available when sending through an Ethereal account
  // Preview URL: https://ethereal.email/message/EUMOVKRRGYBBXBIB...
}

main().catch(console.error);

// 发送定时邮件
schedule.scheduleJob('30 * * * * *', () => {
 //  main().catch(console.error);
})

发送定时任务

直接利用node-schedule github.com/node-schedu…

// 每分钟的第30秒触发: '30 * * * * *'
// 每小时的1分30秒触发 :'30 1 * * * *'
// 每天的凌晨1点1分30秒触发 :'30 1 1 * * *'
// 每月的1日1点1分30秒触发 :'30 1 1 1 * *'
// 2016年的1月1日1点1分30秒触发 :'30 1 1 1 2016 *'
// 每周1的1点1分30秒触发 :'30 1 1 * * 1'


schedule.scheduleJob('1-40 * * * * *', () => {
   console.log('aa', new Date())
})
//  每分钟的1-40秒触发 '1-40 * * * * *'
 schedule.scheduleJob('1-40 * * * * *', () => {
  console.log('aa', new Date())
 })
// dayOfWeek
// month
// dayOfMonth
// hour
// minute
// second
// 传入对象 11点02分10秒触发
 schedule.scheduleJob({second: 10, minute: 2, hour: 11}, () => {
   console.log('aa', new Date())
 })

取消定时器

 const cancelTime = schedule.scheduleJob({second: 10, minute: 2, hour: 11}, () => {
   console.log('aa', new Date())
 })
 cancelTime.cancel();   

备注

github.com/Vincedream/…