From 8c30787353ee9385b7565d4100819ca084678543 Mon Sep 17 00:00:00 2001 From: ihmily <114978440+ihmily@users.noreply.github.com> Date: Sat, 25 Jan 2025 15:03:23 +0800 Subject: [PATCH] feat: add smtp port and smtp ssl config --- config/config.ini | 2 ++ main.py | 4 +++- msg_push.py | 13 +++++++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/config/config.ini b/config/config.ini index 70d6bd8..38c5d3f 100644 --- a/config/config.ini +++ b/config/config.ini @@ -41,6 +41,8 @@ bark推送铃声 = tgapi令牌 = tg聊天id(个人或者群组id) = smtp邮件服务器 = +是否使用SMTP服务SSL加密(是/否) = +SMTP邮件服务器端口 = 邮箱登录账号 = 发件人密码(授权码) = 发件人邮箱 = diff --git a/main.py b/main.py index 0a3821e..8ceeaef 100644 --- a/main.py +++ b/main.py @@ -312,7 +312,7 @@ def push_message(record_name: str, live_url: str, content: str) -> None: '钉钉': lambda: dingtalk(dingtalk_api_url, content, dingtalk_phone_num, dingtalk_is_atall), '邮箱': lambda: send_email( email_host, login_email, email_password, sender_email, sender_name, - to_email, msg_title, content + to_email, msg_title, content, smtp_port, open_smtp_ssl ), 'TG': lambda: tg_bot(tg_chat_id, tg_token, content), 'BARK': lambda: bark( @@ -1650,6 +1650,8 @@ while True: tg_token = read_config_value(config, '推送配置', 'tgapi令牌', "") tg_chat_id = read_config_value(config, '推送配置', 'tg聊天id(个人或者群组id)', "") email_host = read_config_value(config, '推送配置', 'SMTP邮件服务器', "") + open_smtp_ssl = options.get(read_config_value(config, '推送配置', '是否使用SMTP服务SSL加密(是/否)', "是"), True) + smtp_port = read_config_value(config, '推送配置', 'SMTP邮件服务器端口', "") login_email = read_config_value(config, '推送配置', '邮箱登录账号', "") email_password = read_config_value(config, '推送配置', '发件人密码(授权码)', "") sender_email = read_config_value(config, '推送配置', '发件人邮箱', "") diff --git a/msg_push.py b/msg_push.py index 677de97..96f4681 100644 --- a/msg_push.py +++ b/msg_push.py @@ -83,7 +83,7 @@ def xizhi(url: str, title: str, content: str) -> Dict[str, Any]: def send_email(email_host: str, login_email: str, email_pass: str, sender_email: str, sender_name: str, - to_email: str, title: str, content: str) -> Dict[str, Any]: + to_email: str, title: str, content: str, smtp_port: str = None, open_ssl: bool = True) -> Dict[str, Any]: receivers = to_email.replace(',', ',').split(',') if to_email.strip() else [] try: @@ -97,7 +97,12 @@ def send_email(email_host: str, login_email: str, email_pass: str, sender_email: t_apart = MIMEText(content, 'plain', 'utf-8') message.attach(t_apart) - smtp_obj = smtplib.SMTP_SSL(email_host, 465) + if open_ssl: + smtp_port = int(smtp_port) or 465 + smtp_obj = smtplib.SMTP_SSL(email_host, smtp_port) + else: + smtp_port = int(smtp_port) or 25 + smtp_obj = smtplib.SMTP(email_host, smtp_port) smtp_obj.login(login_email, email_pass) smtp_obj.sendmail(sender_email, receivers, message.as_string()) return {"success": receivers, "error": []} @@ -237,7 +242,7 @@ if __name__ == '__main__': # sender_name="", # to_email="", # title="", - # content="" + # content="", # ) bark_url = 'https://xxx.xxx.com/key/' @@ -247,4 +252,4 @@ if __name__ == '__main__': api="https://ntfy.sh/xxxxx", title="直播推送", content="xxx已开播", - ) + ) \ No newline at end of file