文章标题: {{ title }}
+文章内容: {{ summary }}
+发布时间: {{ published }}
+From 620dca6e28e4fe55a3cfb47374c3f149c2b1297a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E7=A5=9E?= <3162475700@qq.com> Date: Sat, 6 Jul 2024 20:02:50 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=B6=E2=80=8D=F0=9F=8C=AB=EF=B8=8F?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=82=AE=E4=BB=B6=E6=A8=A1=E6=9D=BF=E4=BB=A5?= =?UTF-8?q?=E7=BE=8E=E8=A7=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf.yaml | 1 + push_rss_update/send_email.py | 31 ++++--- requirements.txt | 3 +- rss_subscribe/email_template.html | 135 ++++++++++++++++++++++++++++++ rss_subscribe/last_articles.json | 8 -- run.py | 10 +++ 6 files changed, 169 insertions(+), 19 deletions(-) create mode 100644 rss_subscribe/email_template.html diff --git a/conf.yaml b/conf.yaml index 9bc0fb7..a3a5541 100644 --- a/conf.yaml +++ b/conf.yaml @@ -31,6 +31,7 @@ rss_subscribe: github_username: willow-god github_repo: Friend-Circle-Lite your_blog_url: https://blog.qyliu.top/ + email_template: "./rss_subscribe/email_template.html" # SMTP 配置 # 解释:使用其中的相关配置实现上面两种功能,若无推送要求可以不配置,请将以上两个配置置为false diff --git a/push_rss_update/send_email.py b/push_rss_update/send_email.py index 264ddaa..ed04ee3 100644 --- a/push_rss_update/send_email.py +++ b/push_rss_update/send_email.py @@ -1,6 +1,8 @@ import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText +from jinja2 import Environment, FileSystemLoader +import os def email_sender( target_email, @@ -10,6 +12,8 @@ def email_sender( password, subject, body, + template_path=None, + template_data=None, use_tls=True, ): """ @@ -20,11 +24,12 @@ def email_sender( sender_email (str): 发信邮箱地址。 smtp_server (str): SMTP 服务地址。 port (int): SMTP 服务端口。 - use_ssl (bool): 是否使用 SSL 加密。 - username (str): SMTP 服务用户名。 password (str): SMTP 服务密码。 subject (str): 邮件主题。 body (str): 邮件内容。 + template_path (str): HTML 模板文件路径。默认为 None。 + template_data (dict): 渲染模板的数据。默认为 None。 + use_tls (bool): 是否使用 TLS 加密。默认为 True。 """ # 创建 MIME 对象 msg = MIMEMultipart() @@ -32,8 +37,15 @@ def email_sender( msg['To'] = target_email msg['Subject'] = subject - # 添加邮件内容 - msg.attach(MIMEText(body, 'plain')) + if template_path and template_data: + # 使用 Jinja2 渲染 HTML 模板 + env = Environment(loader=FileSystemLoader(os.path.dirname(template_path))) + template = env.get_template(os.path.basename(template_path)) + html_content = template.render(template_data) + msg.attach(MIMEText(html_content, 'html')) + else: + # 添加纯文本邮件内容 + msg.attach(MIMEText(body, 'plain')) # 连接到 SMTP 服务器并发送邮件 try: @@ -46,7 +58,7 @@ def email_sender( except Exception as e: print(f'无法发送邮件到 {target_email}. 错误: {e}') -def send_emails(emails, sender_email, smtp_server, port, password, subject, body, use_tls=True): +def send_emails(emails, sender_email, smtp_server, port, password, subject, body, template_path=None, template_data=None, use_tls=True): """ 循环发送邮件给指定的多个邮箱。 @@ -58,12 +70,11 @@ def send_emails(emails, sender_email, smtp_server, port, password, subject, body password (str): SMTP 服务密码。 subject (str): 邮件主题。 body (str): 邮件内容。 - use_tls (bool): 是否使用 TLS 加密,默认为 True。 + template_path (str): HTML 模板文件路径。默认为 None。 + template_data (dict): 渲染模板的数据。默认为 None。 + use_tls (bool): 是否使用 TLS 加密。默认为 True。 """ for email in emails: print(f'正在发送邮件到 {email}') print(f'---------------------------\n邮件主题: {subject}\n邮件内容: {body}\n发件人: {sender_email}\n---------------------------') - email_sender(email, sender_email, smtp_server, port, password, subject, body, use_tls) - - - + email_sender(email, sender_email, smtp_server, port, password, subject, body, template_path, template_data, use_tls) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index b59f293..bef98e8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ datetime python-dateutil==2.9.0.post0 requests feedparser==6.0.11 -PyYAML==6.0.1 \ No newline at end of file +PyYAML==6.0.1 +jinja2==3.1.2 \ No newline at end of file diff --git a/rss_subscribe/email_template.html b/rss_subscribe/email_template.html new file mode 100644 index 0000000..673bfa1 --- /dev/null +++ b/rss_subscribe/email_template.html @@ -0,0 +1,135 @@ + + +
+ + +