Skip to content

Latest commit

 

History

History
90 lines (69 loc) · 2.35 KB

2.3.8 nodemailer 发送邮件 使用.md

File metadata and controls

90 lines (69 loc) · 2.35 KB

2.3.8 nodemailer 使用

install

npm install nodemailer

Example

const nodemailer = require("nodemailer");
// 发送邮件函数
async function sendMail(text) {  
  var user = "[email protected]";//自己的邮箱  
  var pass = "xxx"; //qq邮箱授权码,如何获取授权码下面有讲  
  var to = "[email protected]";//对方的邮箱  
  let transporter = nodemailer.createTransport({    
    host: "smtp.qq.com",    
    port: 587,    
    secure: false,    
    auth: {     
      user: user, // 用户账号      
      pass: pass, //授权码,通过QQ获取    
    },  
  });  
  let info = await transporter.sendMail({    
    from: `亲爱的<${user}>`, // sender address    
    to: `亲爱的<${to}>`, // list of receivers    
    subject: "亲爱的", // Subject line   
    text: text, // plain text body 
  });  
  console.log("发送成功");
}
  
  //测试一下
  sendMail('你好')

注意:qq邮箱的pass(授权码)需要进入 qq邮箱 的【设置】-【账户】,然后开启smtp,查看你的授权码

定时任务

npm install node-schedule

node-schedule

Cron-style Scheduling
The cron format consists of:

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
Examples with the cron format:

var schedule = require('node-schedule');

var j = schedule.scheduleJob('42 * * * *', function(){
  console.log('The answer to life, the universe, and everything!');
});




const schedule = require("node-schedule");
//每天下午5点21分发送
schedule.scheduleJob({ hour: 17, minute: 21 }, function () {  
  console.log("启动任务:" + new Date());  
  getHoneyedWords().then((res) => {    
    console.log(res.data);    
    sendMail(res.data);  
  });
});

参考