diff --git a/sendgrid/helpers/mail/email.py b/sendgrid/helpers/mail/email.py index 5f2d541a5..3c2cb3bbb 100644 --- a/sendgrid/helpers/mail/email.py +++ b/sendgrid/helpers/mail/email.py @@ -17,6 +17,12 @@ __html_parser__ = HTMLParser() html_entity_decode = __html_parser__.unescape +try: + basestring = basestring +except NameError: + # Define basestring when Python >= 3.0 + basestring = str + class Email(object): """An email address with an optional name.""" @@ -82,7 +88,7 @@ def name(self, value): :param value: Name associated with this email. :type value: string """ - if not (value is None or isinstance(value, str)): + if not (value is None or isinstance(value, basestring)): raise TypeError('name must be of type string.') # Escape common CSV delimiters as workaround for diff --git a/test/test_email.py b/test/test_email.py index 665d7de52..eb50374aa 100644 --- a/test/test_email.py +++ b/test/test_email.py @@ -18,6 +18,12 @@ def test_add_name(self): self.assertEqual(email.name, name) + def test_add_unicode_name(self): + name = u"SomeName" + email = Email(name=name) + + self.assertEqual(email.name, name) + def test_add_name_email(self): name = "SomeName" address = "test@example.com" @@ -25,6 +31,13 @@ def test_add_name_email(self): self.assertEqual(email.name, name) self.assertEqual(email.email, "test@example.com") + def test_add_unicode_name_email(self): + name = u"SomeName" + address = u"test@example.com" + email = Email(email=address, name=name) + self.assertEqual(email.name, name) + self.assertEqual(email.email, u"test@example.com") + def test_add_rfc_function_finds_name_not_email(self): name = "SomeName" email = Email(name) @@ -60,3 +73,10 @@ def test_add_name_with_comma(self): email.name = name self.assertEqual(email.name, '"' + name + '"') + + def test_add_unicode_name_with_comma(self): + email = Email() + name = u"Name, Some" + email.name = name + + self.assertEqual(email.name, u'"' + name + u'"')