From a2dc5de5cee1dd721aa477f04002fe690db9ac23 Mon Sep 17 00:00:00 2001 From: Wankko Ree Date: Sat, 14 Jan 2023 19:48:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9F=BA=E4=BA=8E=20SMTP=20?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=20=E7=9A=84=E6=B6=88=E6=81=AF=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E6=9C=8D=E5=8A=A1=20(#1763)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sample/config.sample.sh | 14 ++++++++++ sample/notify.js | 39 +++++++++++++++++++++++++++ sample/notify.py | 58 +++++++++++++++++++++++++++++++++-------- 3 files changed, 100 insertions(+), 11 deletions(-) diff --git a/sample/config.sample.sh b/sample/config.sample.sh index 9005c641..a1e3e8b4 100644 --- a/sample/config.sample.sh +++ b/sample/config.sample.sh @@ -160,4 +160,18 @@ export AIBOTK_TYPE="" ## aibotk_name (必填)填写群名或用户昵称,和上面的type类型要对应 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" +## smtp_email 填写 SMTP 收发件邮箱,通知将会由自己发给自己 +export SMTP_EMAIL="" +## smtp_password 填写 SMTP 登录密码,也可能为特殊口令,视具体邮件服务商说明而定 +export SMTP_PASSWORD="" +## smtp_name 填写 SMTP 收发件人姓名,可随意填写 +export SMTP_NAME="" + ## 其他需要的变量,脚本中需要的变量使用 export 变量名= 声明即可 diff --git a/sample/notify.js b/sample/notify.js index f8fe0a7b..f9eefa6c 100644 --- a/sample/notify.js +++ b/sample/notify.js @@ -125,6 +125,18 @@ let AIBOTK_NAME = ''; //FSKEY 飞书机器人的 FSKEY let FSKEY = ''; +// =======================================SMTP 邮件设置区域======================================= +// SMTP_SERVER: 填写 SMTP 发送邮件服务器,形如 smtp.exmail.qq.com:465 +// SMTP_SSL: 填写 SMTP 发送邮件服务器是否使用 SSL,内容应为 true 或 false +// SMTP_EMAIL: 填写 SMTP 收发件邮箱,通知将会由自己发给自己 +// SMTP_PASSWORD: 填写 SMTP 登录密码,也可能为特殊口令,视具体邮件服务商说明而定 +// SMTP_NAME: 填写 SMTP 收发件人姓名,可随意填写 +let SMTP_SERVER = ''; +let SMTP_SSL = 'false'; +let SMTP_EMAIL = ''; +let SMTP_PASSWORD = ''; +let SMTP_NAME = ''; + //==========================云端环境变量的判断与接收========================= if (process.env.GOTIFY_URL) { GOTIFY_URL = process.env.GOTIFY_URL; @@ -250,6 +262,22 @@ if (process.env.AIBOTK_NAME) { if (process.env.FSKEY) { FSKEY = process.env.FSKEY; } + +if (process.env.SMTP_SERVER) { + SMTP_SERVER = process.env.SMTP_SERVER; +} +if (process.env.SMTP_SSL) { + SMTP_SSL = process.env.SMTP_SSL; +} +if (process.env.SMTP_EMAIL) { + SMTP_EMAIL = process.env.SMTP_EMAIL; +} +if (process.env.SMTP_PASSWORD) { + SMTP_PASSWORD = process.env.SMTP_PASSWORD; +} +if (process.env.SMTP_NAME) { + SMTP_NAME = process.env.SMTP_NAME; +} //==========================云端环境变量的判断与接收========================= /** @@ -287,6 +315,7 @@ async function sendNotify( PushDeerNotify(text, desp), //PushDeer aibotkNotify(text, desp), //智能微秘书 fsBotNotify(text, desp), //飞书机器人 + smtpNotify(text, desp), //SMTP 邮件 ]); } @@ -1022,6 +1051,16 @@ 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(); + } + }); +} + module.exports = { sendNotify, BARK_PUSH, diff --git a/sample/notify.py b/sample/notify.py index 8392af54..79ac9fb2 100644 --- a/sample/notify.py +++ b/sample/notify.py @@ -9,6 +9,10 @@ import re import threading import time import urllib.parse +import smtplib +from email.mime.text import MIMEText +from email.header import Header +from email.utils import formataddr import requests @@ -62,10 +66,10 @@ push_config = { 'DEER_KEY': '', # PushDeer 的 PUSHDEER_KEY 'DEER_URL': '', # PushDeer 的 PUSHDEER_URL - + 'CHAT_URL': '', # synology chat url 'CHAT_TOKEN': '', # synology chat token - + 'PUSH_PLUS_TOKEN': '', # push+ 微信推送的用户令牌 'PUSH_PLUS_USER': '', # push+ 微信推送的群组编码 @@ -86,6 +90,12 @@ push_config = { 'AIBOTK_KEY': '', # 智能微秘书 个人中心的apikey 文档地址:http://wechat.aibotk.com/docs/about 'AIBOTK_TYPE': '', # 智能微秘书 发送目标 room 或 contact 'AIBOTK_NAME': '', # 智能微秘书 发送群名 或者好友昵称和type要对应好 + + 'SMTP_SERVER': '', # SMTP 发送邮件服务器,形如 smtp.exmail.qq.com:465 + 'SMTP_SSL': 'false', # SMTP 发送邮件服务器是否使用 SSL,填写 true 或 false + 'SMTP_EMAIL': '', # SMTP 收发件邮箱,通知将会由自己发给自己 + 'SMTP_PASSWORD': '', # SMTP 登录密码,也可能为特殊口令,视具体邮件服务商说明而定 + 'SMTP_NAME': '', # SMTP 收发件人姓名,可随意填写 } notify_function = [] # fmt: on @@ -282,16 +292,16 @@ def pushdeer(title: str, content: str) -> None: data = {"text": title, "desp": content, "type": "markdown", "pushkey": push_config.get("DEER_KEY")} url = 'https://api2.pushdeer.com/message/push' if push_config.get("DEER_URL"): - url = push_config.get("DEER_URL") - + url = push_config.get("DEER_URL") + response = requests.post(url, data=data).json() - + if len(response.get("content").get("result")) > 0: print("PushDeer 推送成功!") else: print("PushDeer 推送失败!错误信息:", response) - + def chat(title: str, content: str) -> None: """ 通过Chat 推送消息 @@ -303,14 +313,14 @@ def chat(title: str, content: str) -> None: data = 'payload=' + json.dumps({'text': title + '\n' + content}) url = push_config.get("CHAT_URL") + push_config.get("CHAT_TOKEN") response = requests.post(url, data=data) - + if response.status_code == 200: print("Chat 推送成功!") else: - print("Chat 推送失败!错误信息:", response) + print("Chat 推送失败!错误信息:", response) + + - - def pushplus_bot(title: str, content: str) -> None: """ 通过 push+ 推送消息。 @@ -562,6 +572,30 @@ def aibotk(title: str, content: str) -> None: print(f'智能微秘书 推送失败!{response["error"]}') +def smtp(title: str, content: str) -> None: + """ + 使用 SMTP 邮件 推送消息。 + """ + if not push_config.get("SMTP_SERVER") or not push_config.get("SMTP_SSL") or not push_config.get("SMTP_EMAIL") or not push_config.get("SMTP_PASSWORD") or not push_config.get("SMTP_NAME"): + print("SMTP 邮件 的 SMTP_SERVER 或者 SMTP_SSL 或者 SMTP_EMAIL 或者 SMTP_PASSWORD 或者 SMTP_NAME 未设置!!\n取消推送") + return + print("SMTP 邮件 服务启动") + + message = MIMEText(content, 'plain', 'utf-8') + message['From'] = formataddr((Header(push_config.get("SMTP_NAME"), 'utf-8').encode(), push_config.get("SMTP_EMAIL"))) + message['To'] = formataddr((Header(push_config.get("SMTP_NAME"), 'utf-8').encode(), push_config.get("SMTP_EMAIL"))) + message['Subject'] = Header(title, 'utf-8') + + try: + smtp_server = smtplib.SMTP_SSL(push_config.get("SMTP_SERVER")) if push_config.get("SMTP_SSL") == 'true' else smtplib.SMTP(push_config.get("SMTP_SERVER")) + smtp_server.login(push_config.get("SMTP_EMAIL"), push_config.get("SMTP_PASSWORD")) + smtp_server.sendmail(push_config.get("SMTP_EMAIL"), push_config.get("SMTP_EMAIL"), message.as_bytes()) + smtp_server.close() + print("SMTP 邮件 推送成功!") + except Exception as e: + print(f'SMTP 邮件 推送失败!{e}') + + def one() -> str: """ 获取一条一言。 @@ -603,7 +637,9 @@ if push_config.get("QYWX_KEY"): if push_config.get("TG_BOT_TOKEN") and push_config.get("TG_USER_ID"): notify_function.append(telegram_bot) if push_config.get("AIBOTK_KEY") and push_config.get("AIBOTK_TYPE") and push_config.get("AIBOTK_NAME"): - notify_function.append(aibotk) + notify_function.append(aibotk) +if push_config.get("SMTP_SERVER") and push_config.get("SMTP_SSL") and push_config.get("SMTP_EMAIL") and push_config.get("SMTP_PASSWORD") and push_config.get("SMTP_NAME"): + notify_function.append(smtp) def send(title: str, content: str) -> None: