Fix UnicodeEncodeError error while reporting invalid email address.

Fixes #148.
This commit is contained in:
medmunds
2019-05-19 11:19:21 -07:00
parent 44518ed69f
commit 5dce0895f1
4 changed files with 18 additions and 5 deletions

View File

@@ -36,6 +36,9 @@ Fixes
* Support using `AnymailMessage` with django-mailer and similar packages that pickle * Support using `AnymailMessage` with django-mailer and similar packages that pickle
messages. (See `#147`_. Thanks to `@ewingrj`_ for identifying the problem.) messages. (See `#147`_. Thanks to `@ewingrj`_ for identifying the problem.)
* Fix UnicodeEncodeError error while reporting invalid email address on Python 2.7.
(See `#148`_. Thanks to `@fdemmer`_ for reporting the problem.)
v6.0 v6.0
---- ----
@@ -941,12 +944,14 @@ Features
.. _#112: https://github.com/anymail/issues/112 .. _#112: https://github.com/anymail/issues/112
.. _#115: https://github.com/anymail/issues/115 .. _#115: https://github.com/anymail/issues/115
.. _#147: https://github.com/anymail/issues/147 .. _#147: https://github.com/anymail/issues/147
.. _#148: https://github.com/anymail/issues/148
.. _@ailionx: https://github.com/ailionx .. _@ailionx: https://github.com/ailionx
.. _@calvin: https://github.com/calvin .. _@calvin: https://github.com/calvin
.. _@costela: https://github.com/costela .. _@costela: https://github.com/costela
.. _@decibyte: https://github.com/decibyte .. _@decibyte: https://github.com/decibyte
.. _@ewingrj: https://github.com/ewingrj .. _@ewingrj: https://github.com/ewingrj
.. _@fdemmer: https://github.com/fdemmer
.. _@janneThoft: https://github.com/janneThoft .. _@janneThoft: https://github.com/janneThoft
.. _@joshkersey: https://github.com/joshkersey .. _@joshkersey: https://github.com/joshkersey
.. _@Lekensteyn: https://github.com/Lekensteyn .. _@Lekensteyn: https://github.com/Lekensteyn

View File

@@ -1,3 +1,5 @@
from __future__ import unicode_literals
import json import json
from traceback import format_exception_only from traceback import format_exception_only
@@ -40,7 +42,7 @@ class AnymailError(Exception):
def __str__(self): def __str__(self):
parts = [ parts = [
" ".join([str(arg) for arg in self.args]), " ".join([six.text_type(arg) for arg in self.args]),
self.describe_raised_from(), self.describe_raised_from(),
self.describe_send(), self.describe_send(),
self.describe_response(), self.describe_response(),

View File

@@ -158,13 +158,13 @@ def parse_address_list(address_list, field=None):
for address in parsed: for address in parsed:
if address.username == '' or address.domain == '': if address.username == '' or address.domain == '':
# Django SMTP allows username-only emails, but they're not meaningful with an ESP # Django SMTP allows username-only emails, but they're not meaningful with an ESP
errmsg = "Invalid email address '{problem}' parsed from '{source}'{where}.".format( errmsg = u"Invalid email address '{problem}' parsed from '{source}'{where}.".format(
problem=address.addr_spec, problem=address.addr_spec,
source=", ".join(address_list_strings), source=u", ".join(address_list_strings),
where=" in `%s`" % field if field else "", where=u" in `%s`" % field if field else "",
) )
if len(parsed) > len(address_list): if len(parsed) > len(address_list):
errmsg += " (Maybe missing quotes around a display-name?)" errmsg += u" (Maybe missing quotes around a display-name?)"
raise AnymailInvalidAddress(errmsg) raise AnymailInvalidAddress(errmsg)
return parsed return parsed

View File

@@ -138,6 +138,12 @@ class ParseAddressListTests(SimpleTestCase):
with self.assertRaisesMessage(AnymailInvalidAddress, "Display Name"): with self.assertRaisesMessage(AnymailInvalidAddress, "Display Name"):
parse_address_list(['"Display Name"', '<valid@example.com>']) parse_address_list(['"Display Name"', '<valid@example.com>'])
def test_invalid_with_unicode(self):
# (assertRaisesMessage can't handle unicode in Python 2)
with self.assertRaises(AnymailInvalidAddress) as cm:
parse_address_list([u"\N{ENVELOPE}"])
self.assertIn(u"Invalid email address '\N{ENVELOPE}'", six.text_type(cm.exception))
def test_single_string(self): def test_single_string(self):
# bare strings are used by the from_email parsing in BasePayload # bare strings are used by the from_email parsing in BasePayload
parsed_list = parse_address_list("one@example.com") parsed_list = parse_address_list("one@example.com")