本文是以一个给女友发邮件的例子讲解 nodemailer 模块,实际工作中发送异常日志等到自己的邮箱还是挺常用的,有兴趣的小伙伴学习下这个模块。
一、引言
工作的时候不能陪女朋友,就想着写个程序,每天在固定时间给她发一些情话,好让她时刻能感受到我的爱心,帮她缓解一下对我的思念之情。
考虑的到qq和微信有诸多的限制(一不小心就被封),先来个邮箱版本的吧!
Stack Overflow 联合创始人杰夫·阿特伍德曾经说过,任何一个能用 JavaScript 编写的应用系统,最终都必将使用 JavaScript 实现。
Atwood’s Law是Jeff Atwood在2007年提出的:”any application that can be written in JavaScript, will eventually be written in JavaScript。
大佬的定律都出来了,只能说js牛x!

我们的技术选型 就是nodejs,首先你得安装一个nodejs,然后我们主要使用一个模块Nodemailer。
二、nodemailer介绍
nodemailer是一个发送邮件npm包,我们可以使用它方便快捷的给任何人发送邮件。
三、nodemailer的安装
打开终端,然后输入如下命令:
1 2 3 4
   | mkdir mailBot  #cmd创建文件夹mailBot(touch mailBot  #如果是Linux可以用touch命令) cd mailBot  #进入文件夹 npm init -y #初始化npm npm install nodemailer  #安装邮件发送模块
   | 
 
我们创建一个mailBot文件夹,在终端中进入文件夹,初始化npm,然后安装nodemailer
四、nodemailer的使用 - 发送邮件
接下来在项目中新建 index.js 文件,编写如下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
   | 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("发送成功"); }
  //测试一下 send('你好亲爱的')
  | 
 
在终端中输入 node index.js 就可以执行js文件的代码!
通过以上代码,我们就可以发送 任意一句话给你想发送的人!
注意:qq邮箱的pass(授权码)需要进入 qq邮箱 的【设置】-【账户】,然后如下图1的地方,开启smtp,下图2的地方查看你的授权码

五、自动生成情话
当然现在还有个缺点,既然要献爱心,那就要发句比较好听的话,我们也叫彩虹屁。。。
有个彩虹屁生成网站,网站名字很有意思,自己体会。
我们使用这个网站的接口来生成我们要说的内容。
使用axios模块来下载生成的情话。
安装axios,在终端中输入如下命令
使用axios获取情话,在index.js中 增加如下代码:
1 2 3 4 5 6
   | const { default: Axios } = require("axios"); function getHoneyedWords() {   var url = "https://chp.shadiao.app/api.php";   //获取这个接口的信息   return Axios.get(url); }
  | 
 
六、使用邮件发送情话
index.js中增加测试邮件发送代码如下
1 2 3 4 5 6
   | //获取情话 getHoneyedWords().then(res=>{     console.log(res.data)   //发送邮件     sendMail(res.data); })
   | 
 
终端中输入node index.js
结果显示 :春水初生,春林初盛,春风十里,不如你!
七、每天定时发送
考虑到每天定时发送会显示的更有诚意,接下来搞个定时发送。我们需要启动个定时任务,使用node-schedule模块。
安装,终端中输入如下
1
   | npm install node-schedule
   | 
 
使用,index.js增加如下代码
1 2 3 4 5 6 7 8 9
   | 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);   }); });
  | 
 
终端中输入node index.js
这样每天下午5点21分就会自动发送一句情话!
就连家庭地位都提高了呢!
八、完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
   | const nodemailer = require("nodemailer"); const { default: Axios } = require("axios"); const schedule = require("node-schedule"); // 发送邮件函数 async function sendMail(text) {     const user = "[email protected]"; // 邮件发送方账号     const pass = "yqfzddbqbkzxfjgb"; // 发送方邮箱授权码     const to = "[email protected]"; // 邮件接收方账号     // 创建一个 SMTP 客户端配置对象     const transporter = nodemailer.createTransport({         host: "smtp.qq.com",         port: 587,         secure: false,         auth: {             user: user, // 用户账号             pass: pass // 授权码         },     });     // 创建一个收件人对象     const mail = {         from: `Your Dear<${user}>`, // 发件人: 一定不能少< >         to: `My Dear<${to}>`, // 收件人         subject: 'My Favourite', // 主题         text: text, // 内容         html: '' // 这里可以添加html标签     }     // 发送邮件     await transporter.sendMail(mail, (err, info) => {         if (err) {             return console.log(err);         }         transporter.close();         console.log('mail sent:', info.response)     }); } // 从彩虹屁生成网站获取一条彩虹屁 function getHoneyedWords() {     var url = "https://chp.shadiao.app/api.php";     return Axios.get(url) } // 定时:每天下午5点21分 schedule.scheduleJob({ hour: 17, minute: 21 }, function() {     console.log("启动任务" + new Date());     // 使用邮件发送情话     getHoneyedWords().then(res=>{         console.log(res.data)         sendMail(res.data)     }) })
  |