html电子邮件python中的if语句 [英] if statement inside html email python

查看:103
本文介绍了html电子邮件python中的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MIME Multipart 以 HTML 格式撰写电子邮件并使用 smtplib 发送,并且仅在条件评估为真时才包含一个句子.基本上像

I am using MIME Multipart to compose an email in HTML and sending using smtplib and would like to include a sentence only if a condition evaluates to true. Basically something like

html = """\
    <html>
      <body>
        Hi {name}<br>
        Hope you are doing well.
           **<% if {empanelled}: %>**
                <br> We are already empanelled with your company<br>

      </body>
    </html>
    """.format(name=firstname, empanelled = empanelled)

如果我们被嵌入 (value = 1) 后面的句子应该写成 else if value = 0, 不应该出现.任何有关如何使用 python 执行此操作的指示将不胜感激.

If we are empanelled (value = 1) the sentence following this should be written else if value = 0, should not appear . Any pointers to how I can do this with python will be greatly appreciated.

推荐答案

如前所述,你应该使用类似 Jinja2.但是,除非我误解了您的问题,否则您在 python 中有 empanelled 变量.为什么不像这样在 python 中评估条件:

As mentioned, you should using something like Jinja2. However, unless I am misunderstanding your question, you have the empanelled variable in python. why not jus evaluate the conditional in python like this:

html = """\
    <html>
      <body>
        Hi {name}<br>
        Hope you are doing well.
             {additional_message}
      </body>
    </html>
    """

if empanelled:
    return html.format(name=firstname, additional_message="<br> We are already empanelled with your company<br>")
else:
    return html.format(name=firstname,additional_message="")

虽然,一些模板语言方法仍然会更好.

Although, some templating language approach would still be better.

这篇关于html电子邮件python中的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆