html_content with attachment in django-post_office
July 30, 2017, 10:25 p.m.
I get strange bug with attachment in django-post-office (at least, it's encountered in version 2.0.8 and lower): when the mail is sent, which consist of html-content with attachment (of any format), the mail is come, but without attachment.
I used next package version:
django==1.10.7 (and lower) django-post-office==2.0.8 (and lower)
In more detail, the behavior of django-post-office of the specified version and the working of the mail.send()
method can be described as follows:
- When the mail is sent with simple content, using
message
parameter, and with attachment - it works correctly, i. e. I get mail with attachment. - When the mail is sent with content, which contains html-tags or even plain text without tags, using
html_message
parameter, and with attachment - it does not work correctly, i. e. I get the empty mail, but file is attached. - When the mail is sent with content, using
html_message
parameter, but without attachment - it work correctly, i. e. I get the mail formated by html-tags and with attachment.
And I find the workaround of this strange gug: use both arguments message
and html_message
. For example:
msg = render_to_string('path/to/template/', {'some_data': 'some_data'}) pdf = render_to_pdf(form.pdf_template, email_data) attachments = {'Message.pdf': ContentFile(pdf)} mail.send(user_email, me, subject=subject, message=msg, html_message=msg, attachments=attachments)
Related Posts:
Comments: 2
26.01.2019 9:17 #
A short hack is to pass ' ' (a single whitespace) as 'msg' parameter.
Found it here: https://github.com/ui/django-post_office/issues/116
Reply
28.01.2019 16:44 #
Thanks for your solution!
Reply