Repository: sendgrid/sendgrid-python

Issue:
Inconsistent format of email from header “name <email>”
#### Issue Summary

Current implementation restrict use default `email_from` behavior. For more info please see https://tools.ietf.org/html/rfc2822#section-3.4

#### Steps to Reproduce
1. When user try to create `Email` instance from https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/mail.py#L173-L179. You must specify 
`from_email` and `from_name` as seperate parametrs
```
    def get(self):
        email = {}
        if self.name != None:
            email["name"] = self.name
        if self.email != None:
            email["email"] = self.email
        return email

from_email = Email("test@example.com", "Example Name")
```
As a result client should split `from_email`:
```
try:
    import rfc822
except ImportError:
    import email.utils as rfc822


from_name, from_email = rfc822.parseaddr(email.from_email)
# Python sendgrid client should improve
# sendgrid/helpers/mail/mail.py:164
if not from_name:
    from_name = None
Mail(from_email, from_name)
```

Proposal to how to improve this bahaviour.
Because if user now create `Email` from:
```
from_email = Email("Andrii Soldatenko <hello@asoldatenko.com>")
```
**Actual result:**
![image](https://cloud.githubusercontent.com/assets/1374633/20647168/e2159a04-b495-11e6-95f9-20c2bb5fc21f.png)

#### Technical details:

* sendgrid-python Version: master
* Python Version: all

#### References:
- https://github.com/sendgrid/sendgrid-python/issues/277
- RFC: https://tools.ietf.org/html/rfc2822#section-3.4
- Django docs: https://docs.djangoproject.com/en/1.10/topics/email/#emailmessage-objects

**NOTE:**
In `sengrid-python==1.4` this feature is working

Comments:

Patch:
diff --git a/sendgrid/helpers/mail/mail.py b/sendgrid/helpers/mail/mail.py
index de41bad..a2159b2 100644
--- a/sendgrid/helpers/mail/mail.py
+++ b/sendgrid/helpers/mail/mail.py
@@ -262,11 +262,15 @@ class Email(object):
     def __init__(self, email=None, name=None):
         self._name = None
         self._email = None
-
-        if email is not None:
-            self.email = email
-        if name is not None:
-            self.name = name
+        if name or email:
+            if not name:
+                # allows passing emails as "dude Fella <example@example.com>"
+                self.parse_email(email)
+            else:
+                #allows backwards compatibility for Email(email, name)
+                if email is not None:
+                    self.email = email
+                self.name = name
 
     @property
     def name(self):
@@ -293,6 +297,28 @@ class Email(object):
             email["email"] = self.email
         return email
 
+    def parse_email(self, email_info):
+        try:
+            import rfc822
+        except ImportError:
+            import email.utils as rfc822
+        
+        name, email = rfc822.parseaddr(email_info)
+        
+        # more than likely a string was passed here instead of an email address
+        if "@" not in email:
+            name = email
+            email = None
+
+        if not name:
+            name = None
+            
+        if not email:
+            email = None
+
+        self.name = name
+        self.email = email
+        return name, email
 
 class Content(object):
 


Test patch:
diff --git a/test/test_email.py b/test/test_email.py
new file mode 100644
index 0000000..92ae10a
--- /dev/null
+++ b/test/test_email.py
@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+import json
+
+from sendgrid.helpers.mail import (Email)
+
+try:
+    import unittest2 as unittest
+except ImportError:
+    import unittest
+
+
+class TestEmailObject(unittest.TestCase):
+    def test_add_email_address(self):
+        address = "test@example.com"
+        email = Email(address)
+
+        self.assertEqual(email.email, "test@example.com")
+    
+    def test_add_name(self):
+        name = "SomeName"
+        email = Email(name=name)
+
+        self.assertEqual(email.name, name)
+    
+    def test_add_name_email(self):
+        name = "SomeName"
+        address = "test@example.com"
+        email = Email(email=address, name=name)
+        self.assertEqual(email.name, name)
+        self.assertEqual(email.email, "test@example.com")
+
+    def test_add_rfc_function_finds_name_not_email(self):
+        name = "SomeName"
+        email = Email(name)
+
+        self.assertEqual(email.name, name)
+        self.assertIsNone(email.email)
+
+    def test_add_rfc_email(self):
+        name = "SomeName"
+        address = "test@example.com"
+        name_address = "{0} <{1}>".format(name, address)
+        email = Email(name_address)
+        self.assertEqual(email.name, name)
+        self.assertEqual(email.email, "test@example.com")
+    
+    def test_empty_obj_add_name(self):
+        email = Email()
+        name = "SomeName"
+        email.name = name
+
+        self.assertEqual(email.name, name)
+
+    def test_empty_obj_add_email(self):
+        email = Email()
+        address = "test@example.com"
+        email.email = address
+
+        self.assertEqual(email.email, address)
\ No newline at end of file