Skip to content

Commit

Permalink
Merge pull request #348 from sendgrid/issue_277
Browse files Browse the repository at this point in the history
Allows users to submit rfc822 formatted email addresses
  • Loading branch information
thinkingserious authored Oct 23, 2017
2 parents 0884519 + 3f876af commit ae54113
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
36 changes: 31 additions & 5 deletions sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):

Expand Down
59 changes: 59 additions & 0 deletions test/test_email.py
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)

0 comments on commit ae54113

Please sign in to comment.