Skip to content

Commit

Permalink
Merge pull request #618 from cmccandless/issue-578
Browse files Browse the repository at this point in the history
Quote names containing comma or semicolon
  • Loading branch information
thinkingserious authored Oct 5, 2018
2 parents ae378dc + 32183b7 commit 6326325
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
22 changes: 22 additions & 0 deletions sendgrid/helpers/mail/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -35,6 +49,14 @@ 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.')

# 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 + '"'
self._name = value

@property
Expand Down
7 changes: 7 additions & 0 deletions test/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,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 + '"')

0 comments on commit 6326325

Please sign in to comment.