使用NodeJs定时给亲爱的人发送彩虹屁

2022-07-26,,,

1.工具包安装
  • NodeJs

    所有的工具包都是基于安装NodeJs的基础之上

  • nodemailer

    NodeJs 发送邮件的插件包

  • axios

    发送Ajax请求的请求包

  • node-schedule

    定时执行任务包

2.代码
const nodemailer = require("nodemailer"); //npm发送mail包
const { default: Axios } = require("axios"); //在线请求制作彩虹屁;
const schedule = require("node-schedule"); //npm 定时发送包

//在线生成彩虹屁 请求函数
function getHoneyedWords() {
  let url = "https://chp.shadiao.app/api.php";
  return Axios.get(url);
}

// 发送邮件函数
async function sendMail(text) {
  let user = "xxxxxx"; //自己的邮箱
  let pass = "xxxxxx"; //自己邮箱授权码
   //获取方式 登录QQ邮箱 ---设置----账户----获取授权码  即可
  let to = "xxxxxx"; //对方的邮箱

  let transporter = nodemailer.createTransport({
    host: "smtp.qq.com",
    port: 587,
    secure: false,
    auth: {
      user: user, // 用户账号
      pass: pass, //授权码,通过QQ获取
    },
  });

  let info = await transporter.sendMail({
    from: `xxxx<${user}>`,//发送者
    to: `xxxx<${to}>`,//接受者
    subject: "自定义主题", //mail主题
    text: text, //mail 内容
  });
}

//自定义时间进行定时发送   例如  每天的 17:21 定时进行发送
schedule.scheduleJob({ hour: "17", minute: "21" }, function () {
  getHoneyedWords().then((res) => {
    sendMail(res.data);
  });
});

3.使用方法
  • 1.找到一个文件夹或者新建一个空白的文件夹;
  • 2.打开文件夹,Windows+R,执行npm init -y;
  • 3.下载上述工具包;
  • 4.新建index.js脚本文件,复制代码进去,然后保存;
  • 5.然后在CMD中执行node index.js即可;
  • 6.如果不想自己动手执行的话,可以把这个脚本文件放在服务器上面,运行即可;
  • 7.如果有多个人需要发送的话,可以考虑使用for循环;

本文地址:https://blog.csdn.net/weixin_45356397/article/details/111094321

《使用NodeJs定时给亲爱的人发送彩虹屁.doc》

下载本文的Word格式文档,以方便收藏与打印。