天天看點

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);
*/

           

繼續閱讀