html_content with attachment in django-post_office

July 30, 2017, 6: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:

  1. When the mail is sent with simple content, using message parameter, and with attachment - it works correctly, i. e. I get mail with attachment.
  2. 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.
  3. 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)

Rate this article

0 from 5 (total 0 ratings)

You can send feedback, suggestions or comments on this article using this form:

Fields marked by star ( * ) is required.

Thank you for yor feedback!

Author of Article

The right to use content on this page http://vivazzi.ru/en/it/html_content-attachment-django-post-office/:

Permission is granted to copy an content with its author and reference to the original without using the parameter rel="nofollow" in tag <a>. Usage:

Author of Article: Maltsev Artem
Link to article: <a href="http://vivazzi.ru/en/it/html_content-attachment-django-post-office/">http://vivazzi.ru/en/it/html_content-attachment-django-post-office/</a>

More: Terms of site usage

Comments: 0

You can leave a comment as an unregistered user. But by registering, you will be able to receive notifications of replies, and also have access to your personal account to view your comments.

To comment in one's own name you should log in or sign up in the usual way or through social networks:

Send

There is no search on this site, so I offer to use usual search engine, for example, Google, adding "vivazzi" after your request.

Try it