codecov[bot]: # [Codecov](https://codecov.io/gh/sendgrid/sendgrid-python/pull/618?src=pr&el=h1) Report
> Merging [#618](https://codecov.io/gh/sendgrid/sendgrid-python/pull/618?src=pr&el=desc) into [master](https://codecov.io/gh/sendgrid/sendgrid-python/commit/84f7cf258f77f179dce0ef3bee8dc3b90f4af2f3?src=pr&el=desc) will **decrease** coverage by `0.23%`.
> The diff coverage is `66.66%`.

[![Impacted file tree graph](https://codecov.io/gh/sendgrid/sendgrid-python/pull/618/graphs/tree.svg?width=650&token=Yq0yTs1Nfq&height=150&src=pr)](https://codecov.io/gh/sendgrid/sendgrid-python/pull/618?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #618      +/-   ##
==========================================
- Coverage   84.77%   84.54%   -0.24%     
==========================================
  Files          35       35              
  Lines        1156     1171      +15     
  Branches      172      175       +3     
==========================================
+ Hits          980      990      +10     
- Misses         90       93       +3     
- Partials       86       88       +2
```


| [Impacted Files](https://codecov.io/gh/sendgrid/sendgrid-python/pull/618?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [sendgrid/helpers/mail/email.py](https://codecov.io/gh/sendgrid/sendgrid-python/pull/618/diff?src=pr&el=tree#diff-c2VuZGdyaWQvaGVscGVycy9tYWlsL2VtYWlsLnB5) | `88.46% <66.66%> (-8.84%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/sendgrid/sendgrid-python/pull/618?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/sendgrid/sendgrid-python/pull/618?src=pr&el=footer). Last update [84f7cf2...dc9b241](https://codecov.io/gh/sendgrid/sendgrid-python/pull/618?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

thinkingserious: @cmccandless,

Could you please merge this agains [this branch](https://github.com/sendgrid/sendgrid-python/tree/v4)? Thanks!

With Best Regards,

Elmer
diff --git a/sendgrid/helpers/mail/email.py b/sendgrid/helpers/mail/email.py
index 0cc6339..432c980 100644
--- a/sendgrid/helpers/mail/email.py
+++ b/sendgrid/helpers/mail/email.py
@@ -3,6 +3,20 @@ try:
 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,14 @@ class Email(object):
 
     @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

JSON quoted comma not ignored
#### Issue Summary

I'm using a python script to send emails. It mostly works.  Comma embedded in quotes are causing the submission to fail.

#### Steps to Reproduce

The input is a CSV file.

"Joe User",addr1@example.com,54321
"User, Joe",addr2@example.com,55555
The second line fails.

Python correctly parses the line and formats it as JSON, but it fails on submission. Apparently the quotes on name field are ignored and sees the embedded comma as a delimiter. It then uses User as the name and Joe as the email address and fails.

Is this a known bug? Is there a work-around?


#### Technical details:

* sendgrid-python 5.3.0
* Python Version: 2.7.3
diff --git a/test/test_email.py b/test/test_email.py
index 902c59d..0604ef2 100644
--- a/test/test_email.py
+++ b/test/test_email.py
@@ -58,3 +58,10 @@ class TestEmailObject(unittest.TestCase):
         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 + '"')

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov pytest-xdist pytest-mock pytest-asyncio
apt-get update apt-get install -y gcc
3.6
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs==22.2.0
certifi==2021.5.30
coverage==6.2
dataclasses==0.8
execnet==1.9.0
Flask==0.10.1
importlib-metadata==4.8.3
iniconfig==1.1.1
itsdangerous==2.0.1
Jinja2==3.0.3
MarkupSafe==2.0.1
packaging==21.3
pluggy==1.0.0
py==1.11.0
pyparsing==3.1.4
pytest==7.0.1
pytest-asyncio==0.16.0
pytest-cov==4.0.0
pytest-mock==3.6.1
pytest-xdist==3.0.2
python-http-client==3.3.7
PyYAML==3.11
-e git+https://github.com/sendgrid/sendgrid-python.git@56a0ad09e7343e0c95321931bea0745927ad61ed#egg=sendgrid
six==1.10.0
tomli==1.2.3
typing_extensions==4.1.1
Werkzeug==2.0.3
zipp==3.6.0

name: sendgrid-python
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - attrs==22.2.0
      - coverage==6.2
      - dataclasses==0.8
      - execnet==1.9.0
      - flask==0.10.1
      - importlib-metadata==4.8.3
      - iniconfig==1.1.1
      - itsdangerous==2.0.1
      - jinja2==3.0.3
      - markupsafe==2.0.1
      - packaging==21.3
      - pluggy==1.0.0
      - py==1.11.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pytest-asyncio==0.16.0
      - pytest-cov==4.0.0
      - pytest-mock==3.6.1
      - pytest-xdist==3.0.2
      - python-http-client==3.3.7
      - pyyaml==3.11
      - six==1.10.0
      - tomli==1.2.3
      - typing-extensions==4.1.1
      - werkzeug==2.0.3
      - zipp==3.6.0
prefix: /opt/conda/envs/sendgrid-python

test/test_email.py::TestEmailObject::test_add_name_with_comma
test/test_email.py::TestEmailObject::test_add_email_address test/test_email.py::TestEmailObject::test_add_name test/test_email.py::TestEmailObject::test_add_name_email test/test_email.py::TestEmailObject::test_add_rfc_email test/test_email.py::TestEmailObject::test_add_rfc_function_finds_name_not_email test/test_email.py::TestEmailObject::test_empty_obj_add_email test/test_email.py::TestEmailObject::test_empty_obj_add_name
sendgrid/helpers/mail/email.py
sendgrid/helpers/mail/email.py