-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #348 from sendgrid/issue_277
Allows users to submit rfc822 formatted email addresses
- Loading branch information
Showing
2 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]>" | ||
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 @@ def get(self): | |
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): | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = "[email protected]" | ||
email = Email(address) | ||
|
||
self.assertEqual(email.email, "[email protected]") | ||
|
||
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 = "[email protected]" | ||
email = Email(email=address, name=name) | ||
self.assertEqual(email.name, name) | ||
self.assertEqual(email.email, "[email protected]") | ||
|
||
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 = "[email protected]" | ||
name_address = "{0} <{1}>".format(name, address) | ||
email = Email(name_address) | ||
self.assertEqual(email.name, name) | ||
self.assertEqual(email.email, "[email protected]") | ||
|
||
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 = "[email protected]" | ||
email.email = address | ||
|
||
self.assertEqual(email.email, address) |