Opened 9 years ago

Closed 7 years ago

#26344 closed Bug (fixed)

EmailMultiAlternatives ignores alternatives when it has attachments and body is empty

Reported by: Alvin Lindstam Owned by: nobody
Component: Core (Mail) Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It seems like django.core.mail.EmailMultiAlternatives does not include the body or the alternative if the body is falsey and if it has attachments.

    >>> from django.core.mail import EmailMultiAlternatives
    >>> m = EmailMultiAlternatives(body="")
    >>> m.attach_alternative("<img src='https://example.com/example_image.png'>", "text/html")
    >>> "example_image.png" in m.message().as_string()
    True
    >>> m.attach('example.txt', "example attachment", 'text/plain')
    >>> "example_image.png" in m.message().as_string()
    False
    >>> print(m.message().as_string())
    Content-Type: multipart/mixed; boundary="===============2599669765216026223=="
    MIME-Version: 1.0
    Subject: 
    From: webmaster@localhost
    To: 
    Date: Thu, 10 Mar 2016 14:31:12 -0000
    Message-ID: <20160310143112.53907.61069@236.1.168.192.in-addr.arpa>

    --===============2599669765216026223==
    MIME-Version: 1.0
    Content-Type: text/plain; charset="utf-8"
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="example.txt"

    example attachment
    --===============2599669765216026223==--

It is probably because of this conditional. It checks if body is truthy, even though it is sometimes called from EmailMultiAlternatives with a msg that should be included.

Change History (4)

comment:1 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

I haven't completely verified the report to say what the proper solution should be, however, the if self.body check was introduced in 2d082a34dc61a832710d98a933858fd2c0059644 without any tests, so at least some test should be added to demonstrate what it accomplishes (if anything).

comment:2 by Tim Graham, 7 years ago

Has patch: set

comment:3 by Claude Paroz, 7 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: newclosed

In af35c69a:

Fixed #26344 -- Made EmailMessage include alternatives when the body is empty and it has attachments.

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