#9367 closed (fixed)
EmailMultiAlternatives does not properly handle attachments
Reported by: | loekje | Owned by: | Luke Plant |
---|---|---|---|
Component: | Core (Mail) | Version: | 1.0 |
Severity: | Keywords: | ||
Cc: | patrys@…, walter+django@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
EmailMultiAlternatives does not properly attach (PDF) files. The generated email only contains a multipart/alternative part, whereas in order to additionally support attachments the multipart/alternative part should be wrapped in a multipart/mixed part. This allows for the inclusion of attachments in addition to different alternative views.
I would like to suggest to add this functionality to either the EmailMessage class or to update the EmailMultiAlternatives class to have proper support of both alternative views AND attachments. I have included an example on how to support this behavior. It has been tested to work with Outlook and Thunderbird mail clients.
Attachments (2)
Change History (15)
by , 16 years ago
comment:1 by , 16 years ago
Just to be clear on what the result is of the following code for the EmailMultiAlternatives implementation in Django 1.0 when invoking the following code:
subject, from_email, to = 'hello', 'from@example.com', 'to@example.com' text_content = 'This is an important message.' html_content = '<p>This is an <strong>important</strong> message.</p>' msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.attach_file('/tmp/file.pdf') msg.send()
A message will be sent, with the two views (typically the HTML view will show up in the email client) and the file.pdf attachment is included in the email. However, it cannot be opened in email client because it does not show any attachments (and why should the email client, it is supposed to be an alternative view...)
follow-up: 3 comment:2 by , 16 years ago
Summary: | EmailMultiAlternatives does not properly attach (PDF) files. → EmailMultiAlternatives does not properly handle attachments |
---|---|
Triage Stage: | Unreviewed → Accepted |
We should make sure that attach_file()
works correctly and actually attaches the file even in the subclass. That should be all that's needed here.
I can't work out what your attachment here is trying to do, since it's a whole file. Can you attach a proper patch if you're trying to fix attach_file()
?
comment:3 by , 16 years ago
Replying to mtredinnick:
We should make sure that
attach_file()
works correctly and actually attaches the file even in the subclass. That should be all that's needed here.
I can't work out what your attachment here is trying to do, since it's a whole file. Can you attach a proper patch if you're trying to fix
attach_file()
?
Ok. I will submit a patch in the coming days.
by , 16 years ago
Attachment: | mail.py.patch added |
---|
comment:4 by , 16 years ago
Has patch: | set |
---|
Patch has been attached. In the patch the EmailMultiAlternatives wraps the multipart/alternative views in a multipart/mixed message that also includes the attachments. The changes made to EmailMessage are merely for reusing code in the derived EmailMutliAlternatives class. In the original EmailMultiAlternatives all attachments (including the alternative views) would be attached as multipart/alternative. As a result of this the alternative views are correctly presented in the browser, however the attachments are also considered to be an alternative view (instead of the real attachment they are).
comment:5 by , 16 years ago
Owner: | changed from | to
---|
comment:6 by , 16 years ago
Cc: | added |
---|
comment:7 by , 16 years ago
Owner: | removed |
---|
Not sure why this is assigned to me. I'm not going to have time to look at it just yet.
comment:8 by , 16 years ago
Component: | Uncategorized → django.core.mail |
---|
comment:9 by , 15 years ago
Cc: | added |
---|
comment:10 by , 15 years ago
Owner: | set to |
---|
comment:11 by , 15 years ago
It's still quite difficult to see what the patch does, due to the way diff interprets things, but I've had a proper look. attach_file() doesn't work correctly in the subclass because the subclass works by overriding the value of multipart_subtype, which is used by the base class to build the message. So the fundamental way EmailMultiAlternatives works is broken for normal attachments, hence the need for quite a big patch. But it looks like a good patch.
I've added a test, I'll commit shortly. I'm taking it on trust that this is necessary to fix behaviour in Outlook (the previous format of email worked OK in my mail client, but Outlook's behaviour seems sensible as loekje argues).
comment:12 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Example of EmailAlternativesMessage with support of including, next to alternative views, attachments