Ticket #11144: bug11144.2.patch

File bug11144.2.patch, 1.6 KB (added by rmt, 6 years ago)

Use email.Utils.getaddresses instead of toaddrstring.split(",") + regression test

  • django/core/mail.py

    diff --git a/django/core/mail.py b/django/core/mail.py
    index 6a45b46..677d59f 100644
    a b from email.MIMEText import MIMEText 
    1313from email.MIMEMultipart import MIMEMultipart
    1414from email.MIMEBase import MIMEBase
    1515from email.Header import Header
    16 from email.Utils import formatdate, parseaddr, formataddr
     16from email.Utils import formatdate, parseaddr, formataddr, getaddresses
    1717
    1818from django.conf import settings
    1919from django.utils.encoding import smart_str, force_unicode
    def forbid_multi_line_headers(name, val): 
    7979    except UnicodeEncodeError:
    8080        if name.lower() in ('to', 'from', 'cc'):
    8181            result = []
    82             for item in val.split(', '):
    83                 nm, addr = parseaddr(item)
     82            for nm,addr in getaddresses( (val,) ):
    8483                nm = str(Header(nm, settings.DEFAULT_CHARSET))
    8584                result.append(formataddr((nm, str(addr))))
    8685            val = ', '.join(result)
  • tests/regressiontests/mail/tests.py

    diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py
    index 40e2f76..a58c9bd 100644
    a b BadHeaderError: Header values can't contain newlines (got u'Subject\nInjection T 
    9595>>> message['From']
    9696'from@example.com'
    9797
     98#
     99
     100>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['"Sürname, Firstname" <to@example.com>','other@example.com'])
     101>>> email.message()['To']
     102'=?utf-8?q?S=C3=BCrname=2C_Firstname?= <to@example.com>, other@example.com'
     103
    98104"""
Back to Top