From 4ab97a79f0f3d171a5318d8c0bd8afc0ab481e4c Mon Sep 17 00:00:00 2001 From: catlair Date: Wed, 11 Jan 2023 13:35:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20Node.js=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=20SMTP=20=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sample/config.sample.sh | 2 -- sample/notify.js | 40 +++++++++++++++++++++++++++++++++------- sample/package.json | 5 +++-- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/sample/config.sample.sh b/sample/config.sample.sh index a1e3e8b4..00f97bb8 100644 --- a/sample/config.sample.sh +++ b/sample/config.sample.sh @@ -162,8 +162,6 @@ export AIBOTK_NAME="" ## 14. SMTP ## 暂时只支持在 Python 中调用 notify.py 以发送 SMTP 邮件通知 -## Node.js 因为无内置 SMTP 功能,暂时不支持在 Node.js 中调用 notify.js 以发送 SMTP 邮件通知,详见 notify.js 的 todo -## smtp_server 填写 SMTP 发送邮件服务器,形如 smtp.exmail.qq.com:465 export SMTP_SERVER="" ## smtp_ssl 填写 SMTP 发送邮件服务器是否使用 SSL,内容应为 true 或 false export SMTP_SSL="false" diff --git a/sample/notify.js b/sample/notify.js index f9eefa6c..fc23c9e9 100644 --- a/sample/notify.js +++ b/sample/notify.js @@ -1051,14 +1051,40 @@ function fsBotNotify(text, desp) { }); } -function smtpNotify(text, desp) { - return new Promise((resolve) => { - if (SMTP_SERVER && SMTP_SSL && SMTP_EMAIL && SMTP_PASSWORD && SMTP_NAME) { - // todo: Node.js并没有内置的 smtp 实现,需要调用外部库,因为不清楚这个文件的模块依赖情况,所以留给有缘人实现 - } else { - resolve(); +async function smtpNotify(text, desp) { + if (![SMTP_SERVER, SMTP_EMAIL, SMTP_PASSWORD].every(Boolean)) { + return; + } + + try { + const nodemailer = require('nodemailer'); + const transporter = nodemailer.createTransport( + `${SMTP_SSL === 'true' ? 'smtps:' : 'smtp:'}//${SMTP_SERVER}`, + { + auth: { + user: SMTP_EMAIL, + pass: SMTP_PASSWORD, + }, + }, + ); + + const addr = SMTP_NAME ? `"${SMTP_NAME}" <${SMTP_EMAIL}>` : SMTP_EMAIL; + const info = await transporter.sendMail({ + from: addr, + to: addr, + subject: text, + text: desp, + }); + + if (!!info.messageId) { + console.log('SMTP发送通知消息成功🎉\n'); + return true; } - }); + console.log('SMTP发送通知消息失败!!\n'); + } catch (e) { + console.log('SMTP发送通知消息出现错误!!\n'); + console.log(e); + } } module.exports = { diff --git a/sample/package.json b/sample/package.json index 8eeed397..82ac9225 100644 --- a/sample/package.json +++ b/sample/package.json @@ -5,12 +5,13 @@ "dependencies": { "crypto-js": "^4.0.0", "download": "^8.0.0", - "http-server": "^0.12.3", "got": "^11.5.1", + "http-server": "^0.12.3", + "nodemailer": "^6.8.0", "qrcode-terminal": "^0.12.0", "request": "^2.88.2", "tough-cookie": "^4.0.0", "tunnel": "0.0.6", "ws": "^7.4.3" } -} \ No newline at end of file +}