Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#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)

mail.py (2.8 KB ) - added by loekje 16 years ago.
Example of EmailAlternativesMessage with support of including, next to alternative views, attachments
mail.py.patch (5.7 KB ) - added by loekje 16 years ago.

Download all attachments as: .zip

Change History (15)

by loekje, 16 years ago

Attachment: mail.py added

Example of EmailAlternativesMessage with support of including, next to alternative views, attachments

comment:1 by loekje, 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...)

comment:2 by Malcolm Tredinnick, 16 years ago

Summary: EmailMultiAlternatives does not properly attach (PDF) files.EmailMultiAlternatives does not properly handle attachments
Triage Stage: UnreviewedAccepted

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()?

in reply to:  2 comment:3 by anonymous, 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 loekje, 16 years ago

Attachment: mail.py.patch added

comment:4 by loekje, 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 loekje, 16 years ago

Owner: changed from nobody to Malcolm Tredinnick

comment:6 by Patryk Zawadzki, 16 years ago

Cc: patrys@… added

comment:7 by Malcolm Tredinnick, 16 years ago

Owner: Malcolm Tredinnick 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 Thejaswi Puthraya, 16 years ago

Component: Uncategorizeddjango.core.mail

comment:9 by Walter Doekes, 16 years ago

Cc: walter+django@… added

comment:10 by Luke Plant, 16 years ago

Owner: set to Luke Plant

comment:11 by Luke Plant, 16 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 Luke Plant, 16 years ago

Resolution: fixed
Status: newclosed

(In [10983]) Fixed #9367 - EmailMultiAlternatives does not properly handle attachments.

Thanks to Loek Engels for the bulk of the patch.

comment:13 by Luke Plant, 16 years ago

(In [10984]) [1.0.X] Fixed #9367 - EmailMultiAlternatives does not properly handle attachments.

Thanks to Loek Engels for the bulk of the patch.

Backport of r10983 from trunk

Note: See TracTickets for help on using tickets.
Back to Top