Opened 15 months ago

Closed 15 months ago

Last modified 15 months ago

#29140 closed Bug (fixed)

Exception sending EmailMessage with body=None

Reported by: Alex Schokking Owned by: Williams Mendez
Component: Core (Mail) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Alex Schokking)

This had been working fine on 1.9 but when we upgraded to 1.11 instantly started hitting an exception when django tries to call splitlines() on the None body without checking if there's a value there to split on.

 File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 348, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 111, in send_messages
    sent = self._send(message)
  File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 125, in _send
    message = email_message.message()
  File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 307, in message
    msg = SafeMIMEText(self.body, self.content_subtype, encoding)
  File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 214, in __init__
    MIMEText.__init__(self, _text, _subtype=_subtype, _charset=_charset)
  File "/usr/lib/python2.7/email/mime/text.py", line 30, in __init__
    self.set_payload(_text, _charset)
  File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 224, in set_payload
    for l in payload.splitlines()
AttributeError: 'NoneType' object has no attribute 'splitlines'

We've hacked around it by passing in an empty string instead but would probably be more elegant for this has_long_lines logic to check if there's a value instead.

For context we're not passing in a body because we're attaching the plaintext and html versions of the email as a SafeMIMEMultipart attachment.

Change History (9)

comment:1 Changed 15 months ago by Alex Schokking

Description: modified (diff)
Type: UncategorizedBug

comment:2 Changed 15 months ago by Williams Mendez

I'd like to contribute to this ticket.

comment:3 Changed 15 months ago by Herbert Fortes

Hi,

Can you show how do you call EmailMessage?

If body=None the AttributeError will change to - 'NonType' object has no attribute 'encode' at some point. A long time ago.

My first hint is to set |self.body = body or ' ' (single_quotes_here)

Regards,
Herbert

comment:4 Changed 15 months ago by Williams Mendez

Owner: changed from nobody to Williams Mendez
Status: newassigned

comment:5 Changed 15 months ago by Herbert Fortes

It is just a curiosity.

I got Django-1.9.13 put None in some tests.

I found one test that fails: test_send() line 625. Three times.

comment:6 Changed 15 months ago by Herbert Fortes

Tests do not fail.

EmailMessage does not raises errors

I am not dealing with python2.7 right.

comment:7 Changed 15 months ago by Tim Graham

Has patch: set
Triage Stage: UnreviewedReady for checkin

comment:8 Changed 15 months ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 2691ed7:

Fixed #29140 -- Fixed EmailMessage crash when body is None.

comment:9 in reply to:  6 Changed 15 months ago by Alex Schokking

Replying to Herbert Fortes:

Tests do not fail.

EmailMessage does not raises errors

I am not dealing with python2.7 right.

We were using Python 2.7 on this project

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