Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/setting_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,7 @@ def __initCard(self):
"ssl": {"title": tr("启用 SSL"), "description": tr("可选参数,是否启用 SSL"), "type": "bool"},
"starttls": {"title": tr("启用 STARTTLS"), "description": tr("可选参数,是否启用 STARTTLS"), "type": "bool"},
"ssl_unverified": {"title": tr("跳过证书验证"), "description": tr("可选参数,是否跳过 SSL 证书验证"), "type": "bool"},
"plain_text": {"title": tr("纯文本模式"), "description": tr("可选参数,是否以纯文本格式发送邮件(适用于 Outlook 等邮箱)"), "type": "bool"},
},
"tutorial": """
<h4>一、什么是 SMTP?</h4>
Expand Down
1 change: 1 addition & 0 deletions assets/config/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ notify_smtp_port: "" # SMTP 服务器端口,默认为 465。
notify_smtp_ssl: true # 是否使用 SSL 连接 SMTP 服务器。默认为 True,可设置为 False 关闭。
notify_smtp_starttls: false # 是否使用 SSL 连接 SMTP 服务器。默认为 False,可设置为 True 关闭。
notify_smtp_ssl_unverified: false # 是否使用不经验证的 SSL 连接。默认为 False,可设置为 True 关闭。除非您使用自签名邮箱,否则不推荐启用。
notify_smtp_plain_text: false # 是否以纯文本格式发送邮件。默认为 False(发送 HTML 格式)。true 开启后将以纯文本发送,适用于 Outlook 等将 HTML 邮件作为附件处理的邮箱。

# OneBot 通知配置(QQ 机器人)
# 已测试 NapCatQQ(https://github.com/NapNeko/NapCatQQ) 和 OpenShamrock(https://github.com/whitechi73/OpenShamrock) 可用
Expand Down
36 changes: 22 additions & 14 deletions module/notification/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,29 @@ def send(self, title: str, content: str, image_io=None):
ssl = self.params.get("ssl", True)
starttls = self.params.get("starttls", False)
ssl_unverified = self.params.get("ssl_unverified", False)
plain_text = self.params.get("plain_text", False)


msg = MIMEMultipart('related')
body = f'<p>{content}<br>'
if image_io:
body += '<img src="cid:image1">'
body += '</br></p>'
msg['Subject'] = Header(title, 'utf-8')
msg['From'] = From
msg['To'] = To
if image_io:
img = MIMEImage(image_io.getvalue())
img.add_header('Content-ID', '<image1>')
msg.attach(img)
msg.attach(MIMEText(body, "html", "utf-8"))
if plain_text:
if image_io:
self.logger.warning("SMTP 纯文本模式下不支持发送图片,图片将被忽略")
msg = MIMEText(content, "plain", "utf-8")
msg['Subject'] = Header(title, 'utf-8')
msg['From'] = From
msg['To'] = To
else:
msg = MIMEMultipart('related')
body = f'<p>{content}<br>'
if image_io:
body += '<img src="cid:image1">'
body += '</br></p>'
msg['Subject'] = Header(title, 'utf-8')
msg['From'] = From
msg['To'] = To
if image_io:
img = MIMEImage(image_io.getvalue())
img.add_header('Content-ID', '<image1>')
msg.attach(img)
msg.attach(MIMEText(body, "html", "utf-8"))

if starttls:
smtp = smtplib.SMTP(host, port)
Expand Down