本来想随便玩玩,结果发现博主写的有点儿问题。就自己写了并更正了
直接上代码:
const nodemailer = require('nodemailer');
const {default: Axios} = require('axios');
const schedule = require('node-schedule');
// const app = new Koa();
// response
// app.use(async ctx => {
// ctx.body = 'hello world';
//
// });
function getHoneyWords () {
var url = 'http://chp.shadiao.app/api.php';
return Axios.get(url);
}
async function sendMail(text) {
var user = 'xxx@qq.com'; // 发件人
var pass = 'xxx'; // 邮箱授权码
var to = 'xxx@qq.com'; // 收件人
let transporter = nodemailer.createTransport({
host: 'smtp.qq.com',
port: 587,
secure: false,
auth: {
user: user,
pass: pass,
}
});
let info = await transporter.sendMail({
from: user,
to: to,
subject: '您好,老于',
text: text
});
console.log('发送成功');
}
getHoneyWords().then(res => {
console.log(res.data);
sendMail(res.data);
});
// 使用schedule实现定时发送
// schedule.scheduleJob({
// hour: 15,
// minute: 55
// }, function () {
// console.log('启动任务: ', + new Date());
// getHoneyWords().then(res => {
// console.log(res.data);
// sendMail(res.data);
// });
// });
代码里面的如何获取授权码、如何调用情(sao)话接口都有写,引用的博客已经描写的很清楚了,就不再赘述了,嘿嘿。
效果如下: