From 7cfb125682521d7137bbb79263f9962ee5a46f02 Mon Sep 17 00:00:00 2001 From: OceanJiang Date: Sat, 14 Jan 2023 19:53:36 +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=20(#1776)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sample/config.sample.sh | 1 - sample/notify.js | 36 ++++++++++++++++++++++++++++++++++++ sample/package.json | 5 +++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/sample/config.sample.sh b/sample/config.sample.sh index a1e3e8b4..c9a2e776 100644 --- a/sample/config.sample.sh +++ b/sample/config.sample.sh @@ -162,7 +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 diff --git a/sample/notify.js b/sample/notify.js index f9eefa6c..6f3da33b 100644 --- a/sample/notify.js +++ b/sample/notify.js @@ -1051,6 +1051,42 @@ function fsBotNotify(text, desp) { }); } +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); + } +} + function smtpNotify(text, desp) { return new Promise((resolve) => { if (SMTP_SERVER && SMTP_SSL && SMTP_EMAIL && SMTP_PASSWORD && SMTP_NAME) { 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 +}