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 @@ + + + + + + 最新文章通知 + + + +
+
+

清羽飞扬の最新文章

+
+
+

文章标题: {{ title }}

+

文章内容: {{ summary }}

+

发布时间: {{ published }}

+
+ 阅读更多 + +
+

取消订阅

+
+
+ + diff --git a/rss_subscribe/last_articles.json b/rss_subscribe/last_articles.json index 4be54d6..f4979b8 100644 --- a/rss_subscribe/last_articles.json +++ b/rss_subscribe/last_articles.json @@ -1,13 +1,5 @@ { "articles": [ - { - "title": "Spikformer脉冲神经网络学习", - "author": "", - "link": "https://blog.qyliu.top/posts/67189760/", - "published": "2024-07-06 05:33", - "summary": "近期我们进行了人工智能实训,我们小组选择的是脉冲神经网络,不同于原先的神经网络,这个网络采用的是脉冲信号,目前脉冲神经网络的效果并不是很好,但是因为是一个全新的神经网络架构,并且基于生物启发的计算方式,使得它们在处理稀疏和非结构化数据时具有独特的优势。", - "content": "近期我们进行了人工智能实训,我们小组选择的是脉冲神经网络,不同于原先的神经网络,这个网络采用的是脉冲信号,目前脉冲神经网络的效果并不是很好,但是因为是一个全新的神经网络架构,并且基于生物启发的计算方式,使得它们在处理稀疏和非结构化数据时具有独特的优势。" - }, { "title": "东软软件园实习日记", "author": "", diff --git a/run.py b/run.py index 54ceae1..ad9fc80 100644 --- a/run.py +++ b/run.py @@ -38,6 +38,7 @@ if config["rss_subscribe"]["enable"]: github_username = config["rss_subscribe"]["github_username"] github_repo = config["rss_subscribe"]["github_repo"] your_blog_url = config["rss_subscribe"]["your_blog_url"] + email_template = config["rss_subscribe"]["email_template"] # 获取最近更新的文章 latest_articles = get_latest_articles_from_link( url=your_blog_url, @@ -58,6 +59,13 @@ if config["rss_subscribe"]["enable"]: print("获取到的邮箱列表为:", email_list) # 循环latest_articles,发送邮件 for article in latest_articles: + template_data = { + "title": article["title"], + "summary": article["summary"], + "published": article["published"], + "link": article["link"] + } + send_emails( emails=email_list["emails"], sender_email=email, @@ -66,5 +74,7 @@ if config["rss_subscribe"]["enable"]: password=password, subject="清羽飞扬の最新文章:" + article["title"], body="文章链接:" + article["link"] + "\n" + "文章内容:" + article["summary"] + "\n" + "发布时间:" + article["published"], + template_path=email_template, + template_data=template_data, use_tls=use_tls )