Merge pull request #241 from hinak0/master

feat: email push
This commit is contained in:
Womsxd 2022-09-02 22:02:51 +08:00 committed by GitHub
commit b3a3faacbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 10 deletions

27
assets/email_example.html Normal file
View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AutoMihoyoBBS</title>
</head>
<body>
<div class="cover">
<!-- 邮箱只解析行内样式 -->
<img src="https://iw233.cn/api/Random.php" alt="background" style="width: 100%;
filter: brightness(80%)">
<section style="position: fixed; width: 100%;
top: 10%;
left: 0;
text-align: center;
font-weight: 800;">
<h2>{title}</h2>
<p>{message}</p>
</section>
</div>
</body>
</html>

View File

@ -1,7 +1,7 @@
[setting]
enable=false
#共有 cqhttp ftqq(sever酱) pushplus telegram wecom dingrobot bark pushdeer gotify
push_server=cqhttp
enable=true
#共有 cqhttp ftqq(sever酱) pushplus telegram wecom dingrobot bark pushdeer gotify email
push_server=smtp
#server酱 pushplus dingrobot 的推送token
push_token=123456
@ -39,4 +39,16 @@ token=ssXXX
[gotify]
api_url=http://xxx.xxx.cn
token=AMxxxx
priority=7
priority=7
# 邮件推送
[smtp]
mailhost=smtp.qq.com
port=465
fromaddr=xxx@example.com
toaddr=xxx@example.com
# 邮件标题
subject=AutoMihoyoBBS
username=xxx@example.com
# 授权码
password=passward

29
push.py
View File

@ -1,14 +1,15 @@
import base64
import hashlib
import hmac
import os
import time
import hmac
import base64
import config
import urllib
import hashlib
from request import http
from loghelper import log
from configparser import ConfigParser
import config
from loghelper import log
from request import http
cfg = ConfigParser()
@ -76,6 +77,22 @@ def cqhttp(send_title, push_message):
}
)
# ssl smtp mail
def smtp(send_title, push_message):
import smtplib
from email.mime.text import MIMEText
with open("assets/email_example.html") as f:
EMAIL_TEMPLATE = f.read()
message = EMAIL_TEMPLATE.format(title=send_title, message=push_message.replace("\n", "<br/>"))
message = MIMEText(message, "html", "utf-8")
message['Subject'] = cfg["smtp"]["subject"]
message['To'] = cfg["smtp"]["toaddr"]
message['From'] = f"{cfg['smtp']['subject']}<{cfg['smtp']['fromaddr']}>"
with smtplib.SMTP_SSL(cfg["smtp"]["mailhost"], cfg.getint("smtp", "port")) as server:
server.login(cfg["smtp"]["username"], cfg["smtp"]["password"])
server.sendmail(cfg["smtp"]["fromaddr"], cfg["smtp"]["toaddr"], message.as_string())
log.info("邮件发送成功啦")
# 企业微信 感谢linjie5492@github
def wecom(send_title, push_message):