gpt4 book ai didi

html - 在邮件上发送动态 html 文件

转载 作者:太空宇宙 更新时间:2023-11-04 16:12:25 24 4
gpt4 key购买 nike

我尝试使用 django-mailgun 在邮件上发送 html,但它发送的是整个 HTML 包含,而不仅仅是呈现的 HTML 表格。我想要的是通过邮件发送呈现的表格。

这是我的代码:

def send_email():
dict = [....] #it contains list of data that I want to render
html_message = render_to_string('email.html', {'data': dict})
msg = EmailMessage('Hello', html_message,'mailgun@sandboxccd9ae5xxx6486546476d879gd.mailgun.org', ['abc@gmail.com'])
msg.content_subtype = "html"
msg.send()

这是我的 HTML 表格:

  <table style=" border:1px solid black; border-top:1px solid transparent; border-left:1px solid transparent; border-right:1px solid transparent; width:80%;  border-collapse: collapse; font-size:95%; margin-left:10%; margin-right:10%; margin-top:5%; ">
<thead>
<tr><th colspan=5 style=" border-bottom:1px solid black; border-right:none; border-left:none; text-align:center; padding:4x"><h4>Activity List</h4></th></tr>
<tr style="border:1px solid black">
<th style="border:1px solid black">col 1</th>
<th style="border:1px solid black">col 2</th>
<th style="border:1px solid black">col 3</th>
<th style="border:1px solid black">col 4</th>
<th style="border:1px solid black">col 5</th>
</tr>
</thead>
<body>
{% for item in data %}
<tr>
<td style="border:1px solid; text-align:right; padding: 4px">{{ item.data1 }}</td>
<td style="border:1px solid; text-align:right; padding: 4px">{{ item.data2 }}</td>
<td style="border:1px solid; text-align:right; padding: 4px">{{ item.data3 }}</td>
<td style="border:1px solid; text-align:right; padding: 4px">{{ item.data4 }}</td>
<td style="border:1px solid; text-align:right; padding: 4px">{{ item.data5 }}</td>
</tr>
{% endfor %}
</body>

我在邮件中收到这个:

<table style=" border:1px solid black; border-top:1px solid transparent; border-left:1px solid transparent; border-right:1px solid transparent; width:80%;  border-collapse: collapse; font-size:95%; margin-left:10%; margin-right:10%; margin-top:5%; ">
<thead>
<tr><th colspan=5 style=" border-bottom:1px solid black; border-right:none; border-left:none; text-align:center; padding:4x"><h4>Activity List</h4></th></tr>
<tr style="border:1px solid black">
<th style="border:1px solid black">col 1</th>
<th style="border:1px solid black">col 2</th>
<th style="border:1px solid black">col 3</th>
<th style="border:1px solid black">col 4</th>
<th style="border:1px solid black">col 5</th>
</tr>
</thead>
<tbody>

<tr>
<td style="border:1px solid; text-align:right; padding: 4px">data 11</td>
<td style="border:1px solid; text-align:right; padding: 4px">data 12</td>
<td style="border:1px solid; text-align:right; padding: 4px">data 13</td>
<td style="border:1px solid; text-align:right; padding: 4px">data 14</td>
<td style="border:1px solid; text-align:right; padding: 4px">data 15</td>
</tr>

<tr>
<td style="border:1px solid; text-align:right; padding: 4px">data 21</td>
<td style="border:1px solid; text-align:right; padding: 4px">data 22</td>
<td style="border:1px solid; text-align:right; padding: 4px">data 23</td>
<td style="border:1px solid; text-align:right; padding: 4px">data 24</td>
<td style="border:1px solid; text-align:right; padding: 4px">data 25</td>
</tr>

</tbody>

</table>

最佳答案

使用 Django 的电子邮件库,您可以使用 EmailMultiAlternatives 类来完成此操作。

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

它对我有用。尝试这个。引用: https://docs.djangoproject.com/en/1.8/topics/email/#sending-alternative-content-types

关于html - 在邮件上发送动态 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34286609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com