Django EmailMessage send()在整个电子邮件中插入感叹号(!)

时间:2013-07-26 18:29:06

标签: django html-email

我一直在讨论由EmailMessage课程引起的一个神秘的错误,我发送给自己的最终电子邮件最终会在整个正文中插入“!”。目前,我正在使用以下代码发送电子邮件:

   html_content = render_to_string('layouts/option1.html', request.POST)
   subject = "just a test email"
   from_email = request.user.email
   recipient = [from_email, ]
   msg = EmailMessage(subject, html_content, from_email, to=recipient, headers={
       'Content-Type': 'text/html; charset=ISO-8859-1',
       'MIME-Version': '1.0'
   })
   msg.content_subtype = "html"
   try:
       msg.send()
       response['success'] = True
       response['html_content'] = html_content
   except:
       pass

我发现了一个类似的thread(但对于php),它讨论了非常类似的东西。显然这与消息长度有关。我实际上发送了一个相当长的HTML电子邮件,但我无法实现模拟他们在我的链接中建议的解决方案的pythonic版本。

如何防止“!”出现的任何帮助或建议都将非常感谢!!!

谢谢你, Fy

2 个答案:

答案 0 :(得分:0)

这里应该是该PHP解决方案的python版本:

import textwrap

html_content = render_to_string('layouts/option1.html', request.POST)
encoded_content = '\r\n'.join(textwrap.wrap(html_content.encode('base64'), 76))
subject = "just a test email"
from_email = request.user.email
recipient = [from_email, ]
msg = EmailMessage(subject, encoded_content, from_email, to=recipient, headers={
    'Content-Type': 'text/html; charset=ISO-8859-1',  # why not UTF-8 here?
    'Content-Transfer-Encoding': 'base64',
    'MIME-Version': '1.0'
})
msg.content_subtype = "html"
try:
    msg.send()
    response['success'] = True
    response['html_content'] = html_content
except:
    pass

我没有时间测试这整个功能,但希望这有帮助!

答案 1 :(得分:0)

这在this pull request中的Django 1.10中得到修复。

相关问题