😶‍🌫️添加邮件模板以美观

This commit is contained in:
2024-07-06 20:02:50 +08:00
parent f860e823cd
commit 620dca6e28
6 changed files with 169 additions and 19 deletions

View File

@ -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)