发送列表到HTML电子邮件 [英] Send a list to HTML Email

查看:138
本文介绍了发送列表到HTML电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理HTML电子邮件文档。现在,我想提供一个包含数据库信息的列表。如何在HTML电子邮件中显示列表?我尝试过以下操作:

I am currently working with a HTML email document. Now I want to present a list with information from my database. How do I present the list in a HTML email? I've tried following:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

articles = ['hello', 2, 5, 'bye']

me = "email@gmail.com"
you = "email@gmail.com"
subject = 'something'

msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = me
msg['To'] = you

html = """\

    {% for i in {articles} %}
        <p> {{ i }} </p>
    {% endfor %}

""".format(articles)

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login("email@gmail.com", "password")

server.sendmail(me, you, msg.as_string())
server.quit()

感谢所有帮助。

推荐答案

在我看来,您似乎正在尝试使用 jinja2 Synthax不知道。
您可以按照jinja2简介以便将其包含到您的代码中,也可以通过简单的循环将 articles 附加到html字符串中,如下所示: / p>

It seems to me like you're trying to use the jinja2 synthax without knowing it. You can either follow jinja2 Introduction in order to include it into your code, or just append articles to your html string with a simple loop, something like that:

articles = ['hello', 2, 5, 'bye']

html = """\
<html>
  <body>
    <table>
      <tbody>
        {}
      </tbody>
    </table>
  </body>
</html>
"""

rows = ""
for article in articles:
    rows = rows + "<tr><td>"+str(article)+"<td></tr>"
html = html.format(rows)

这篇关于发送列表到HTML电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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