ui / django-post_office

A Django app that allows you to send email asynchronously in Django. Supports HTML email, database backed templates and logging.
MIT License
1.01k stars 270 forks source link

Attachments do not work with HTML-only emails #116

Open jrief opened 9 years ago

jrief commented 9 years ago

Today I made an interesting discovery. If you create a HTML-only email, it can be sent. If, in addition to that HTML-only email you add one or more attachments, no text is shown in the email's body. If you then add some arbitrary plain text, the email is rendered correctly again. Could someone please verify this assertion. If so, then maybe some addition checks, or a HTML-to-text converter should be added.

yprez commented 9 years ago

I think I remember something like this happening with emails that had attachments, but no text version... if I remember correctly, emails without attachment worked fine as html-only. and even adding a single space char in the text version also used to fix it.

In any case, it's better to provide a text version for every email, if only to avoid spam filters.

@selwin, maybe it's a good idea to set blank=False on the text version of emails to prevent this from happening?

yprez commented 9 years ago

In my last project, I made a signal handler to automatically generate text versions of emails:

from django.dispatch import receiver
from django.db.models.signals import post_save

from bs4 import BeautifulSoup
from post_office.models import EmailTemplate

@receiver(post_save, sender=EmailTemplate, dispatch_uid='generate_text_version')
def generate_text_version(sender, instance, **kwargs):
    if not instance.content:
        instance.content = BeautifulSoup(instance.html_content).text
        instance.save()

It generates (not perfect, but good enough) text versions for email templates. Maybe someone will find this useful.

Maybe it's a good idea to add something like this to the post_office? (replacing BeautifulSoup with some other method in order not to create an extra dependency)

jrief commented 9 years ago

@yprez This is a great solution.

I prefer to use BeautifulSoup. This is a mature solution for converting HTML to text and since this task is not trivial at all, trying to add a home-grew solution certainly is worse than adding a dependency to a well maintained project.

selwin commented 9 years ago

Unless the email spec itself prohibits sending plain text emails with attachments, this is a bug. PR to fix this would be welcome :)

yprez commented 8 years ago

Judging by this: http://stackoverflow.com/questions/14580176/confusion-with-sending-email-in-django The bug appears to be with using EmailMultiAlternatives with no text version. But sending the html as text also doesn't work well (the html simply isn't rendered this way).

I think the solution would either be digging up further in Django and specs, or requiring a text version.

yprez commented 8 years ago

Actually, providing any text (even a single space char) fixes this. So a simple hack like

text = text or ' '

should also fix this.