From 164c08adb9fd710da65013f961589f059d390f0b Mon Sep 17 00:00:00 2001 From: Corey McCandless Date: Thu, 13 Sep 2018 12:04:28 -0400 Subject: [PATCH 1/4] Quote names containing comma or semicolon --- sendgrid/helpers/mail/email.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sendgrid/helpers/mail/email.py b/sendgrid/helpers/mail/email.py index 0cc633986..ea4f240e0 100644 --- a/sendgrid/helpers/mail/email.py +++ b/sendgrid/helpers/mail/email.py @@ -3,6 +3,20 @@ except ImportError: import email.utils as rfc822 +import sys +if sys.version_info[:3] >= (3, 5, 0): + import html + html_entity_decode = html.unescape +else: + try: + # Python 2.6-2.7 + from HTMLParser import HTMLParser + except ImportError: + # Python < 3.5 + from html.parser import HTMLParser + __html_parser__ = HTMLParser() + html_entity_decode = __html_parser__.unescape + class Email(object): """An email address with an optional name.""" @@ -35,6 +49,12 @@ def name(self): @name.setter def name(self, value): + if not (value is None or isinstance(value, str)): + raise TypeError('name must be of type string.') + + if value is not None and (',' in value or ';' in value): + value = html_entity_decode(value) + value = '"' + value + '"' self._name = value @property From d53d19369dc2d50f5e803cfae33e1a879d9c6510 Mon Sep 17 00:00:00 2001 From: Corey McCandless Date: Mon, 1 Oct 2018 09:04:43 -0400 Subject: [PATCH 2/4] add explanatory comment --- sendgrid/helpers/mail/email.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sendgrid/helpers/mail/email.py b/sendgrid/helpers/mail/email.py index ea4f240e0..432c9809e 100644 --- a/sendgrid/helpers/mail/email.py +++ b/sendgrid/helpers/mail/email.py @@ -52,6 +52,8 @@ def name(self, value): if not (value is None or isinstance(value, str)): raise TypeError('name must be of type string.') + # Escape common CSV delimiters as workaround for + # https://github.com/sendgrid/sendgrid-python/issues/578 if value is not None and (',' in value or ';' in value): value = html_entity_decode(value) value = '"' + value + '"' From 0042b7bb3da9919487026f2c13b54cd88a05ae00 Mon Sep 17 00:00:00 2001 From: Corey McCandless Date: Mon, 1 Oct 2018 09:29:47 -0400 Subject: [PATCH 3/4] Add test for comma in name --- test/test_email.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_email.py b/test/test_email.py index 902c59d4e..44be5c256 100644 --- a/test/test_email.py +++ b/test/test_email.py @@ -58,3 +58,10 @@ def test_empty_obj_add_email(self): email.email = address self.assertEqual(email.email, address) + + def test_add_name_with_comma(self): + email = Email() + name = "Name, Some" + email.name = name + + self.assertEqual(email.name, name) From 32183b70b548cb8d6cfb27ba7ca1eb40d66c7dd9 Mon Sep 17 00:00:00 2001 From: Corey McCandless Date: Mon, 1 Oct 2018 09:36:04 -0400 Subject: [PATCH 4/4] Add quotes around expected value in new test --- test/test_email.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_email.py b/test/test_email.py index 44be5c256..0604ef250 100644 --- a/test/test_email.py +++ b/test/test_email.py @@ -64,4 +64,4 @@ def test_add_name_with_comma(self): name = "Name, Some" email.name = name - self.assertEqual(email.name, name) + self.assertEqual(email.name, '"' + name + '"')