Skip to content

Commit

Permalink
Change(libs): 📝 优化邮箱 SMTP 连接策略
Browse files Browse the repository at this point in the history
Fixed #509,#424
  • Loading branch information
a76yyyy committed Mar 3, 2024
1 parent 742b5d1 commit f117845
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 26 deletions.
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 相同
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ services:
# - MAIL_SMTP=
# - MAIL_PORT=465
# - MAIL_SSL=True
# - MAIL_STARTTLS=False
# - MAIL_USER=
# - MAIL_PASSWORD=
# - MAIL_FROM=${MAIL_USER}
Expand Down
3 changes: 3 additions & 0 deletions libs/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
50 changes: 26 additions & 24 deletions libs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 2 additions & 0 deletions web/docs/guide/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ python ./chrole.py [email protected] 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|
Expand Down
2 changes: 1 addition & 1 deletion web/docs/toolbox/pusher.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions web/docs/zh_CN/guide/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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相同
Expand Down
2 changes: 1 addition & 1 deletion web/docs/zh_CN/toolbox/pusher.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ MAIL_DOMAIN_HTTPS|否|False|发送的邮件链接启用HTTPS, <br>非框架前
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
Expand Down

0 comments on commit f117845

Please sign in to comment.