Skip to content

Quote names containing comma or semicolon #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also or '"' in value. (" in the name will cause the same problems as comma.)

value = html_entity_decode(value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you actually want email.utils.quote here. (Email display names don't really have anything to do with HTML entities.)

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 @@ -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 + '"')