Code

Opened 6 years ago

Closed 6 years ago

#7574 closed (fixed)

EmailMultiAlternatives and send_email do not handle ugettext_lazy translated strings correctly

Reported by: gary.hutspot@… Owned by: mk
Component: Core (Mail) Version: master
Severity: Keywords:
Cc: gary.hutspot@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

When trying to send email with a subject that is lazily translated, the following TypeError is thrown:

argument of type 'proxy' is not iterable

Cut and paste compatible example:

from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.conf import settings

# If you do not have smtp running on your local system, specify your smtp server here
settings.configure(EMAIL_HOST='your.smtp.server')

send_mail(_('Subject here'), 'Here is the message.', 'from@example.com', ['to@example.com'], fail_silently=False)

Produces:

$ python proof.py 
Traceback (most recent call last):
  File "proof.py", line 8, in <module>
    fail_silently=False)
  File "/usr/lib/python2.5/site-packages/django/core/mail.py", line 333, in send_mail
    connection=connection).send()
  File "/usr/lib/python2.5/site-packages/django/core/mail.py", line 259, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/usr/lib/python2.5/site-packages/django/core/mail.py", line 168, in send_messages
    sent = self._send(message)
  File "/usr/lib/python2.5/site-packages/django/core/mail.py", line 182, in _send
    email_message.message().as_string())
  File "/usr/lib/python2.5/site-packages/django/core/mail.py", line 241, in message
    msg['Subject'] = self.subject
  File "/usr/lib/python2.5/site-packages/django/core/mail.py", line 92, in __setitem__
    name, val = forbid_multi_line_headers(name, val)
  File "/usr/lib/python2.5/site-packages/django/core/mail.py", line 74, in forbid_multi_line_headers
    if '\n' in val or '\r' in val:
TypeError: argument of type '__proxy__' is not iterable

Attachments (1)

7574.patch (919 bytes) - added by mk 6 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 6 years ago by erl

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I am having the same problem. Currently I use ugettext instead of ugettext_lazy as a workaround.

Changed 6 years ago by mk

comment:2 Changed 6 years ago by mk

  • Has patch set
  • Triage Stage changed from Unreviewed to Ready for checkin

Just changing the order of the force_unicode block and the \n/\r detection block is enough.

comment:3 Changed 6 years ago by mk

  • Owner changed from nobody to mk
  • Status changed from new to assigned

comment:4 Changed 6 years ago by mtredinnick

Since Header instances don't act as containers or sequences (they don't support in tests), this patch cannot work. Writing as test for the failing case might have helped detect that.

(Thanks for taking the time to write a patch, but please let somebody else triage it to "ready for checkin" so that we get another set of eyes on that patch for review.)

comment:5 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [8083]) Fixed #7574 -- Fixed the handling of lazy translation in email headers.

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.