python - emailmultialternatives 在 django 中附加文件和 html 内容

标签 python django

我尝试使用 EmailMultiAlternativeshtml 格式发送电子邮件,并且 一段文字。我还想在此电子邮件中包含一个文件。

但后者似乎删除了我的 html 内容。

这是我的代码:

msg = EmailMultiAlternatives(subject, html2text(html_content), 
list(email_from), list(email_to), 
attachments=((request.session['customer']+".txt.blowfish", 
request.session["customer"].content),)) 

msg.attach_alternative(html_content, "text/html") 

msg.send() 

我使用最新的SVN版本 我也尝试使用 msg.attact() 而不是附件,结果相同!

发送替代文本内容,但不发送 html 内容。它只是 显示文件。

任何线索将不胜感激,

最佳答案

我遇到了同样的问题!

我用来将文件附加到 HTML 电子邮件的代码是:

email_dict = {}
email_dict['user'] = user                       

t = loader.get_template('emails/myemail.html')
html = t.render(Context(email_dict))

msg = EmailMultiAlternatives(subject='My Subject', 
                             body=html, 
                             from_email=settings.DEFAULT_FROM_EMAIL, 
                             to=['my_email@email.com'],) 

attachment = open('filepath', 'rb')
msg.attach('Name.txt', attachment.read(), 'text/csv')

msg.content_subtype = 'html'
msg.send()

所以也许这样的事情对你有用:

msg = EmailMultiAlternatives(subject=subject, 
                             body=html2text(html_content), 
                             from_email=list(email_from), 
                             to=list(email_to))


attachment = open(request.session['customer']+".txt.blowfish", 'rb')
msg.attach('Name.txt.blowfish', attachment.read(), 'text/plain')

msg.content_subtype = 'html'
msg.send()

希望对您有所帮助!

关于python - emailmultialternatives 在 django 中附加文件和 html 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33339052/

相关文章:

python - 几何在 GEODJANGO 中不存在

python - 以特定方式填充列表

python - 在Python中获取超出范围的索引默认值

python - 以通用方式为 Python 中的所有子类实现 __neg__

python-3.x - 我想解决 ubuntu 服务器中的 django webserver collectstatic 错误

python - Django 中缺少 'Median' 聚合函数?

python - IntegerField 只接受 11 位数字(不小于 11,不大于 11)

python - multiprocessing.Pool.imap_unordered 的内存使用量稳步增长

python - 类型错误 : Object of type 'float32' is not JSON serializable

python - 在具有现有值的现有数据库表中添加新的唯一字段 - Django1.7/MySql