Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachments do not work with HTML-only emails #116

Open
jrief opened this issue Aug 12, 2015 · 6 comments
Open

Attachments do not work with HTML-only emails #116

jrief opened this issue Aug 12, 2015 · 6 comments

Comments

@jrief
Copy link
Collaborator

jrief commented Aug 12, 2015

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
Copy link
Contributor

yprez commented Aug 12, 2015

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
Copy link
Contributor

yprez commented Aug 12, 2015

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
Copy link
Collaborator Author

jrief commented Aug 12, 2015

@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
Copy link
Collaborator

selwin commented Sep 1, 2015

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

@yprez
Copy link
Contributor

yprez commented Feb 4, 2016

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
Copy link
Contributor

yprez commented Feb 4, 2016

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

text = text or ' '

should also fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants