【问题标题】:Flask render_template() return raw HTML, not processedFlask render_template() 返回原始 HTML,未处理
【发布时间】:2017-12-03 00:09:15
【问题描述】:

我正在使用烧瓶邮件,我想在我的邮件中插入呈现的 HTML。

代码如下:

控制器:

        msg = Message("test", sender='xxx@gmail.com', recipients=[user.get("email")])
        msg.body = render_template('/assets/views/emailing/notification.html', name=user.get("name"))
        mail.send(msg)

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{% if name %}
  <h1>Hello {{ name }}!</h1>
{% else %}
  <h1>Hello, World!</h1>
{% endif %}

</body>
</html>

所以我希望得到一个呈现的电子邮件,或者至少是作为文本的 html,但键被替换为值。

这是我得到的:

https://i.imgur.com/efDMyDM.png

有什么线索吗?

【问题讨论】:

    标签: python html email flask render


    【解决方案1】:

    当您发送 HTML 电子邮件时,您需要设置 html 属性而不是 body,如下所示:

    html = render_template('/assets/views/emailing/notification.html', name=user.get("name"))
    msg = Message("test", sender='xxx@gmail.com', recipients=[user.get("email")], html=html)
    mail.send(msg)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-15
      • 2021-09-26
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      相关资源
      最近更新 更多