Opened 2 years ago

Closed 2 years ago

#34126 closed Bug (invalid)

Exception sending EmailMessage with html_message=True

Reported by: openHBP Owned by: nobody
Component: Core (Other) Version: 4.1
Severity: Normal Keywords: send_mail
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

from django.core.mail import send_mail

send_mail(
    'Subject here',
    '<p>Here is the message.</p>',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
    html_message=True
)
Traceback (most recent call last):
  File "zcheck/send_mail.py", line 9, in <module>
    send_mail(
  File ".venv/lib/python3.9/site-packages/django/core/mail/__init__.py", line 61, in send_mail
    return mail.send()
  File ".venv/lib/python3.9/site-packages/django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File ".venv/lib/python3.9/site-packages/django/core/mail/backends/smtp.py", line 109, in send_messages
    sent = self._send(message)
  File ".venv/lib/python3.9/site-packages/django/core/mail/backends/smtp.py", line 123, in _send
    message = email_message.message()
  File ".venv/lib/python3.9/site-packages/django/core/mail/message.py", line 247, in message
    msg = self._create_message(msg)
  File ".venv/lib/python3.9/site-packages/django/core/mail/message.py", line 440, in _create_message
    return self._create_attachments(self._create_alternatives(msg))
  File ".venv/lib/python3.9/site-packages/django/core/mail/message.py", line 450, in _create_alternatives
    msg.attach(self._create_mime_attachment(*alternative))
  File ".venv/lib/python3.9/site-packages/django/core/mail/message.py", line 365, in _create_mime_attachment
    attachment = SafeMIMEText(content, subtype, encoding)
  File ".venv/lib/python3.9/site-packages/django/core/mail/message.py", line 159, in __init__
    MIMEText.__init__(self, _text, _subtype=_subtype, _charset=_charset)
  File "/home/pat/.pyenv/versions/3.9.7/lib/python3.9/email/mime/text.py", line 42, in __init__
    self.set_payload(_text, _charset)
  File ".venv/lib/python3.9/site-packages/django/core/mail/message.py", line 169, in set_payload
    for line in payload.splitlines()
AttributeError: 'bool' object has no attribute 'splitlines'

Change History (1)

comment:1 by Alex Morega, 2 years ago

Resolution: invalid
Status: newclosed

The html_message argument is meant to contain the HTML version of the email, it's not a boolean flag. The message argument should be the plaintext version.

https://docs.djangoproject.com/en/stable/topics/email/#send-mail

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
    html_message='<p>Here is the message.</p>'
)
Note: See TracTickets for help on using tickets.
Back to Top