From f11784544ed7319ed94b0f0b29a0c4aee3a6933e Mon Sep 17 00:00:00 2001 From: a76yyyy Date: Sun, 3 Mar 2024 20:57:52 +0800 Subject: [PATCH] =?UTF-8?q?Change(libs):=20=F0=9F=93=9D=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E9=82=AE=E7=AE=B1=20SMTP=20=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed qd-today/qd#509,qd-today/qd#424 --- config.py | 1 + docker-compose.yml | 1 + libs/funcs.py | 3 ++ libs/utils.py | 50 ++++++++++++++++-------------- web/docs/guide/deployment.md | 2 ++ web/docs/toolbox/pusher.md | 2 +- web/docs/zh_CN/guide/deployment.md | 2 ++ web/docs/zh_CN/toolbox/pusher.md | 2 +- 8 files changed, 37 insertions(+), 26 deletions(-) diff --git a/config.py b/config.py index 02da4688f6a..55ee109ae31 100644 --- a/config.py +++ b/config.py @@ -185,6 +185,7 @@ class websocket: mail_smtp = os.getenv('MAIL_SMTP', "") # 邮箱 SMTP 服务器 mail_port = int(os.getenv('MAIL_PORT', '465')) # 邮箱 SMTP 服务器端口 mail_ssl = bool(strtobool(os.getenv('MAIL_SSL', 'True'))) # 是否使用 SSL 加密方式收发邮件 +mail_starttls = bool(strtobool(os.getenv('MAIL_STARTTLS', 'False'))) # 是否使用 STARTTLS 加密方式收发邮件, 默认不启用 mail_user = os.getenv('MAIL_USER', '') # 邮箱用户名 mail_password = os.getenv('MAIL_PASSWORD', '') # 邮箱密码 mail_from = os.getenv('MAIL_FROM', mail_user) # 发送时使用的邮箱,默认与 MAIL_USER 相同 diff --git a/docker-compose.yml b/docker-compose.yml index 2f8a8777cba..5ceebaf6077 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -83,6 +83,7 @@ services: # - MAIL_SMTP= # - MAIL_PORT=465 # - MAIL_SSL=True + # - MAIL_STARTTLS=False # - MAIL_USER= # - MAIL_PASSWORD= # - MAIL_FROM=${MAIL_USER} diff --git a/libs/funcs.py b/libs/funcs.py index 9ab1df6602a..efaf4eb5290 100644 --- a/libs/funcs.py +++ b/libs/funcs.py @@ -452,6 +452,9 @@ async def sendmail(self, email, title, content: str, sql_session=None): shark=True) except Exception as e: logger_funcs.error('Send mail error: %r', e, exc_info=config.traceback_print) + else: + r = '邮箱未验证或邮箱不存在!' + logger_funcs.error('Send mail error: %s, email: %s', r, email) class Cal: diff --git a/libs/utils.py b/libs/utils.py index b7e6f20015c..504fade414b 100644 --- a/libs/utils.py +++ b/libs/utils.py @@ -452,33 +452,35 @@ async def _send_mail(to, subject, text=None, subtype='html'): msg['To'] = to try: logger_util.info('send mail to %s', to) - if config.mail_port: - if config.mail_ssl or config.mail_port in [465, 587]: - s = None - if config.mail_port == 587: # use starttls - try: - s = smtplib.SMTP(config.mail_smtp, config.mail_port) - s.starttls() - except smtplib.SMTPException as e: - logger_util.error("smtp starttls failed: %s", e, exc_info=config.traceback_print) - if s is None: - s = smtplib.SMTP_SSL(config.mail_smtp, config.mail_port) - else: - s = smtplib.SMTP(config.mail_smtp, config.mail_port) - s.connect(config.mail_smtp, config.mail_port) - else: + tls_established = False + + # Create SMTP connection according to the configuration + if config.mail_starttls: # use starttls + s = smtplib.SMTP(config.mail_smtp, config.mail_port or 587) + try: + s.starttls() + tls_established = True + except smtplib.SMTPException as e: + logger_util.error("smtp starttls failed: %s", e, exc_info=config.traceback_print) + if not tls_established: if config.mail_ssl: - s = smtplib.SMTP_SSL(config.mail_smtp) + s = smtplib.SMTP_SSL(config.mail_smtp, config.mail_port or 465) else: - s = smtplib.SMTP(config.mail_smtp) - s.connect(config.mail_smtp) - # s = config.mail_ssl and smtplib.SMTP_SSL(config.mail_smtp) or smtplib.SMTP(config.mail_smtp) - if config.mail_user: - s.login(config.mail_user, config.mail_password) - s.sendmail(config.mail_from, to, msg.as_string()) - s.close() + s = smtplib.SMTP(config.mail_smtp, config.mail_port or 25) + + try: + # Only attempt login if user and password are set + if config.mail_user and config.mail_password: + s.login(config.mail_user, config.mail_password) + s.sendmail(config.mail_from, to, msg.as_string()) + except smtplib.SMTPException as e: + logger_util.error("smtp sendmail error: %s", e, exc_info=config.traceback_print) + finally: + # If sending fails, still close the connection + s.quit() + except Exception as e: - logger_util.error('send mail error: %s', e, exc_info=config.traceback_print) + logger_util.error('error occurred while sending mail: %s', e, exc_info=config.traceback_print) return diff --git a/web/docs/guide/deployment.md b/web/docs/guide/deployment.md index fce3e705813..87ab7954d12 100644 --- a/web/docs/guide/deployment.md +++ b/web/docs/guide/deployment.md @@ -158,6 +158,8 @@ python ./chrole.py your@email.address admin |PUSH_BATCH_SW|No|True|Whether to allow periodic push of QD task logs, the default is True| |MAIL_SMTP|No|""|Email SMTP server| |MAIL_PORT|No|465|Email SMTP server port| +|MAIL_SSL|No|True|Whether to use SSL when connecting to the email server, the default is True| +|MAIL_STARTTLS|No|False|Whether to use TLS when connecting to the email server, the default is False| |MAIL_USER|No|""|Email username| |MAIL_PASSWORD|No|""|Email password| |MAIL_FROM|No|MAIL_USER|The Email used when sending, the default is the same as MAIL_USER| diff --git a/web/docs/toolbox/pusher.md b/web/docs/toolbox/pusher.md index 078f56ce26f..634c48d4d3b 100644 --- a/web/docs/toolbox/pusher.md +++ b/web/docs/toolbox/pusher.md @@ -61,7 +61,7 @@ Tencent Enterprise Mail|[How to enable Tencent Enterprise Mail POP/SMTP/IMAP ser QQ Mail|[How to enable QQ Mail POP3/SMTP/IMAP service?](https://service.mail.qq.com/detail/0/428)|[How to turn on POP3/SMTP/IMAP?](https://service.mail.qq.com/detail/0/310)|[Why do I need to set a separate password to enable POP3/SMTP/IMAP?](https://service.mail.qq.com/detail/0/86) Netease Enterprise Mail|-|[Enterprise Mail POP, SMTP, IMAP server address settings.](https://qiye.163.com/help/client-profile.html)|[What is a client authorization code and how do I use it?](https://qiye.163.com/help/af988e.html) Netease Mail|[What is POP3, SMTP and IMAP?](https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac21b87735d7227c217)|[How to enable client protocol?](https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2a5feb28b66796d3b)|- -Gmail|-|[How to use POP3/SMTP/IMAP service?](https://support.google.com/mail/answer/7126229?hl=en)|[Sign in with app passwords](https://support.google.com/accounts/answer/185833?hl=en) +Gmail|-|[How to use POP3/SMTP/IMAP service?](https://support.google.com/mail/answer/7104828?hl=en)|[Sign in with app passwords](https://support.google.com/accounts/answer/185833?hl=en) Outlook|-|[POP, IMAP and SMTP settings](https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-8361e398-8af4-4e97-b147-6c6c4ac95353)|[Use app passwords with apps that don't support two-step verification](https://support.microsoft.com/en-us/account-billing/using-app-passwords-with-apps-that-don-t-support-two-step-verification-5896ed9b-4263-e681-128a-a6f2979a7944) ::: tip MailGun diff --git a/web/docs/zh_CN/guide/deployment.md b/web/docs/zh_CN/guide/deployment.md index b90cec45c8d..dfe38c542b8 100644 --- a/web/docs/zh_CN/guide/deployment.md +++ b/web/docs/zh_CN/guide/deployment.md @@ -158,6 +158,8 @@ PUSH_PIC_URL|否|[push_pic.png](https://fastly.jsdelivr.net/gh/qd-today/qd@maste PUSH_BATCH_SW|否|True|是否允许开启定期推送 QD 任务日志, 默认为True MAIL_SMTP|否|""|邮箱SMTP服务器 MAIL_PORT|否|465|邮箱SMTP服务器端口 +MAIL_SSL|否|True|是否启用邮箱SSL, 默认为True +MAIL_STARTTLS|否|False|是否启用邮箱STARTTLS, 默认为False MAIL_USER|否|""|邮箱用户名 MAIL_PASSWORD|否|""|邮箱密码 MAIL_FROM|否|MAIL_USER|发送时使用的邮箱,默认与MAIL_USER相同 diff --git a/web/docs/zh_CN/toolbox/pusher.md b/web/docs/zh_CN/toolbox/pusher.md index 54d10cfd561..1a6a94035f4 100644 --- a/web/docs/zh_CN/toolbox/pusher.md +++ b/web/docs/zh_CN/toolbox/pusher.md @@ -61,7 +61,7 @@ MAIL_DOMAIN_HTTPS|否|False|发送的邮件链接启用HTTPS,
非框架前 QQ邮箱|[如何开启QQ邮箱的POP3/SMTP/IMAP服务?](https://service.mail.qq.com/detail/0/428)|[如何打开POP3/SMTP/IMAP功能?](https://service.mail.qq.com/detail/0/310)|[开启POP3/SMTP/IMAP功能为什么需要先设置独立密码?](https://service.mail.qq.com/detail/0/86) 网易企业邮箱|-|[企业邮箱的POP、SMTP、IMAP服务器地址设置。](https://qiye.163.com/help/client-profile.html)|[什么是客户端授权码,如何使用?](https://qiye.163.com/help/af988e.html) 网易邮箱|[什么是POP3、SMTP及IMAP?](https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac21b87735d7227c217)|[如何开启客户端协议?](https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2a5feb28b66796d3b)|- -Gmail|-|[如何使用POP3/SMTP/IMAP服务?](https://support.google.com/mail/answer/7126229?hl=zh-Hans)|[如何使用客户端授权密码?](https://support.google.com/accounts/answer/185833?hl=zh-Hans) +Gmail|-|[如何使用POP3/SMTP/IMAP服务?](https://support.google.com/mail/answer/7104828?hl=zh-Hans)|[如何使用客户端授权密码?](https://support.google.com/accounts/answer/185833?hl=zh-Hans) Outlook|-|[POP、IMAP 和 SMTP 设置](https://support.microsoft.com/zh-cn/office/pop-imap-%E5%92%8C-smtp-%E8%AE%BE%E7%BD%AE-8361e398-8af4-4e97-b147-6c6c4ac95353)|[对不支持双重验证的应用使用应用密码](https://support.microsoft.com/zh-cn/account-billing/%E5%AF%B9%E4%B8%8D%E6%94%AF%E6%8C%81%E5%8F%8C%E9%87%8D%E9%AA%8C%E8%AF%81%E7%9A%84%E5%BA%94%E7%94%A8%E4%BD%BF%E7%94%A8%E5%BA%94%E7%94%A8%E5%AF%86%E7%A0%81-5896ed9b-4263-e681-128a-a6f2979a7944) ::: tip MailGun