Skip to content

Commit

Permalink
feat: smtp(邮箱)推送可以关闭背景图
Browse files Browse the repository at this point in the history
Signed-off-by: Womsxd <[email protected]>
  • Loading branch information
Womsxd committed Oct 4, 2024
1 parent d772880 commit cc216df
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
6 changes: 2 additions & 4 deletions assets/email_example.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
<body>
<div class="cover" style="position: relative;">
<!-- 邮箱只解析行内样式 -->
<img src="{image_url}" alt="background" style="width: 100%;
filter: brightness(50%)">
{background_image}
<section style="position: absolute;
top: 0px;
width: 100%;
Expand All @@ -22,8 +21,7 @@
<h2 style="color: #fff;text-shadow:0px 0px 10px #000;">{title}</h2>
<p style="color: #fff;text-shadow:0px 0px 10px #000;">{message}</p>
<br />
<p style="color: #fff;text-shadow:0px 0px 10px #000;">背景图片链接</p>
<a href="{image_url}" style="color: #fff;text-shadow:0px 0px 10px #000;">{image_url}</a>
{background_info}
</section>
</div>
</body>
Expand Down
2 changes: 2 additions & 0 deletions config/push.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ subject=AutoMihoyoBBS
[email protected]
# 授权码
password=passward
# 背景 开关
background = true

#webhook自定义推送
[webhook]
Expand Down
31 changes: 24 additions & 7 deletions push.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,34 @@ def smtp(status_id, push_message):
import smtplib
from email.mime.text import MIMEText

IMAGE_API = "https://api.iw233.cn/api.php?sort=random&type=json"
def get_background_url():
try:
_image_url = http.get("https://api.iw233.cn/api.php?sort=random&type=json").json()["pic"][0]
except:
_image_url = "unable to get the image"
log.warning("获取随机背景图失败,请检查图片api")
return _image_url

def get_background_img_html(background_url):
if background_url:
return f'<img src="{background_url}" alt="background" style="width: 100%; filter: brightness(50%)">'
return ""

def get_background_img_info(background_url):
if background_url:
return f'p style="color: #fff;text-shadow:0px 0px 10px #000;">背景图片链接</p>\n' \
f'<a href="{background_url}" style="color: #fff;text-shadow:0px 0px 10px #000;">{background_url}</a>'
return ""

image_url = None
if cfg.getboolean('smtp', 'background', fallback=True):
image_url = get_background_url()

try:
image_url = http.get(IMAGE_API).json()["pic"][0]
except:
image_url = "unable to get the image"
log.warning("获取随机背景图失败,请检查图片api")
with open("assets/email_example.html", encoding="utf-8") as f:
EMAIL_TEMPLATE = f.read()
message = EMAIL_TEMPLATE.format(title=get_push_title(status_id), message=push_message.replace("\n", "<br/>"),
image_url=image_url)
background_image=get_background_img_html(image_url),
background_info=get_background_img_info(image_url))
message = MIMEText(message, "html", "utf-8")
message['Subject'] = cfg["smtp"]["subject"]
message['To'] = cfg["smtp"]["toaddr"]
Expand Down

0 comments on commit cc216df

Please sign in to comment.