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:

  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!

After clicking the "Send" button, your message will be delivered to me on the mail.

Author of Article

Artem Maltsev

Web-developer, having the knowlenge of programming language - Python, framework - Django, content management system - Django CMS, platform of e-commerce site - Django Shop and many other applications, using this technologies.

The right to use content on this page https://vivazzi.pro/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: Artem Maltsev
Link to article: <a href="https://vivazzi.pro/it/html_content-attachment-django-post-office/">https://vivazzi.pro/it/html_content-attachment-django-post-office/</a>

More: Terms of site usage

Comments: 2

Denis
Denis

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

Артём Мальцев
Артём Мальцев author

28.01.2019 16:44 #

Thanks for your solution!

Reply

You can leave a comment as an unregistered user.

But if you sing up, you can:

  • receive notifications
  • view your comments
  • be able to use all the functions of the developed services

To comment in one's own name you should log in or sign up on Vuspace website

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

Select currency for displaying monetary values