带附件问题的Django EmailMessage

时间:2013-08-07 09:26:13

标签: django django-email

我正在使用EmailMessage发送带附件的邮件。该附件是.html文件。我成功地将该文件附加到电子邮件中。但是那个html显示不正确。 Html文件在附件内容中显示一些标记。

代码返回如下,

attach_file_name是我想要显示的文件路径,即" /path/of/html/file.html"

msg = EmailMessage(subject, content, from_email, to_list, cc=cc_list, bcc=bcc_list)
if attach_file_name:
    msg.attach_file(attach_file_name)
msg.content_subtype = "html"
msg.send()

请帮帮我

2 个答案:

答案 0 :(得分:1)

您需要使用EmailMultiAlternatives

from django.core.mail import EmailMultiAlternatives

msg = EmailMultiAlternatives(subject, content, from_email, to_list, cc=cc_list, bcc=bcc_list)
if attach_file_name:
    msg.attach_file(attach_file_name, mimetype="text/html")
msg.send()

答案 1 :(得分:0)

我在BeautifulSoup的帮助下阅读了该文件,然后我将该文件附加到电子邮件中,然后html以正确的格式出现