天天看点

Node.js环境 nodemailer & schedule 实现动态HTML邮件的定时发送IntroductionCode

文章目录

  • Introduction
  • Code

Introduction

最近突发奇想, 想定时发送邮件, 最终选择了js这套方案, 真是有趣!

但是前端这一行, 自身这审美, 这一点一点肝, 真是干不了😪

项目总体分为

Node.js环境 nodemailer & schedule 实现动态HTML邮件的定时发送IntroductionCode

这两部分.

思路是 请求 天气API, 彩虹屁API, 图床 的数据放入 模板html( template.js) 中, 返回一个HTML字符串, 再将这个字符串用 nodemailer 发送给收件人.

使用 schedule 在每天19.19定时发送

TIPS:

  • QQ邮箱需要收件人添加信任才能看到图片!
  • 代码块中的pass是邮箱授权码, 而非QQ密码!
Node.js环境 nodemailer & schedule 实现动态HTML邮件的定时发送IntroductionCode
Node.js环境 nodemailer & schedule 实现动态HTML邮件的定时发送IntroductionCode
Node.js环境 nodemailer & schedule 实现动态HTML邮件的定时发送IntroductionCode
Node.js环境 nodemailer & schedule 实现动态HTML邮件的定时发送IntroductionCode

Code

title, emoji 等这些都是自定义库, 分模块来写, 日后若要再增添功能也方便!

/*
 * @Author      : TouSan
 * @Date        : 2021-03-10 11:42:51
 * @LastEditTime: 2021-03-11 15:18:43
 * @FilePath    : \boom\index.js
 * @Website     : http://csdn.acmaker.vip
 * @Email       : [email protected]
 * @Description :
 */

const nodemailer = require('nodemailer');
const schedule = require('node-schedule');
const Axios = require('axios');

const title = require('./module/title');
const emoji = require('./module/emoji');
const template = require('./module/template');
const image = require('./module/image');
const weather = require('./module/weather');
const honeyWords = require('./module/honeyWords');

/**
 * The function of sending mail
 */
async function sendMail(HTML) {
  let user = '[email protected]';
  let pass = 'xxxxxxxxxxx'; // authentication code! Notice! NOT QQ PASSOWRD!!!
  // let target = '[email protected]'; // target mail address // dear
  let target = '[email protected]'; // target mail address // myself
  // let target = '[email protected]'; // target mail address // test_ghr
  let transporter = nodemailer.createTransport({
    host: 'smtp.qq.com',
    port: 587,
    secure: false,
    auth: {
      user: user,
      pass: pass,
    },
  });
  let info = await transporter.sendMail({
    from: `<${user}>`, // sender
    to: `<${target}>`, // receiver
    subject: `致${title.getTitle()}${emoji.getEmoji()}`, // mail theme
    html: HTML,
    bcc: '[email protected]', // blind carbon copy
    // text: text, //mail content
  });
}

/**
 * Generate Html Mail Template
 */
function GenerateHtmlMail() {
  Axios.all([weather.getWeather(), honeyWords.getHoneyWords()]).then(
    Axios.spread((weatherResp, honeyWordsResp) => {
      // console.log(weatherResp, honeyWordsResp);
      sendMail(
        template.getTemplate(
          image.getImage(),
          {
            city: weatherResp.data.data.city,
            forecast: weatherResp.data.data.forecast,
            ganmao: weatherResp.data.data.ganmao,
          },
          title.getTitle(),
          honeyWordsResp.data,
        ),
      );
    }),
  );
}

/**
 * Schedule Task: the date of sending, eg 17:21 every day
 */
schedule.scheduleJob({ hour: '19', minute: '19' }, function () {
  GenerateHtmlMail();
});

console.log('\t\t\t\tSTART AT: ' + new Date().toLocaleString());

// GenerateHtmlMail();

/**
setInterval(function(){
	console.log(new Date().toLocaleString());
},1000);
*/

           

继续阅读