【发布时间】:2022-10-12 22:53:54
【问题描述】:
我正在尝试使用 jinja 发送一封带有 python 的 html 电子邮件,以将字符串插入到 html 模板中。使用以下代码,我可以在本地主机上运行我的烧瓶应用程序并发送电子邮件,但电子邮件没有发送我想要的模板......事实上,它在电子邮件中没有任何内容全部。
这是我正在使用的代码的缩写版本:
应用程序.py
from flask import Flask
from util import send_email
app = Flask(__name__)
@app.route("/")
def index():
send_email("SUBJECT", "recipient@gmail.com", "CONTENT")
实用程序.py
import smtplib
from email.message import EmailMessage
from flask import render_template
def send_email(subject, recipient, content):
msg = EmailMessage()
msg["Subject"] = "SUBJECT"
msg["From"] = "sender@gmail.com"
msg["To"] = "recipient@gmail.com"
msg.set_content(
render_template("email_template.html", content=content),
subtype="html"
)
with smtplib.SMTP_SSL("smtp.gmail.com", port=465) as smtp:
smtp.login("sender@gmail.com", "password")
smtp.send_message(msg)
email_template.html
<!DOCTYPE html>
<head>
<title>email_template</title>
</head>
<body>
<p> {{content}} </p>
</body>
我怎样才能让它在电子邮件中发送带有我想在模板中插入的内容的 html 模板?
【问题讨论】:
标签: python flask jinja2 smtplib email-templates