Code

Opened 7 years ago

Closed 7 years ago

#4906 closed (invalid)

EmailMultiAlternatives.send() raises error when `to` isn't a list

Reported by: djoume@… Owned by: adrian
Component: Core (Mail) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Hi,

Following http://www.djangoproject.com/documentation/email/, I have tried the example :

from django.core.mail import EmailMultiAlternatives

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.'
msg = EmailMultiAlternatives(subject, text_content, from_email, to)
msg.attach_alternative(html_content, "text/html")
msg.send()

This raise a "cannot concatenate 'str' and 'list' objects" error.
The exception is raised by return self.to + self.bcc (line 251 in mail.py)
because bcc is set to [] by default (line 209 of mail.py) and in this example to is a string.

To fix this I had to use ['to@example.com'] instead of 'to@example.com'

Another solution is to changed __init__() to force self.to and self.bcc to being list.

Attachments (0)

Change History (2)

comment:1 Changed 7 years ago by anonymous

  • Component changed from Core framework to django.core.mail
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by SmileyChris

  • Resolution set to invalid
  • Status changed from new to closed
  • Summary changed from EmailMultiAlternatives.send() raise coercing to Unicode when bcc is None to EmailMultiAlternatives.send() raises error when `to` isn't a list

Plain case of user error - to should always be a list (as the documentation clearly shows). Coercing to to a list wouldn't achieve much, you'd just end up with ['t', 'o', '@', ...]

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.