Skip to content

Commit

Permalink
Reply To header now supports friendly name
Browse files Browse the repository at this point in the history
- Reply To header now supports friendly name [#110](#110)
  • Loading branch information
tushdante committed Sep 24, 2015
1 parent bdfc104 commit afe99dc
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ python:
- '3.2'
install:
- python setup.py install
script: python test/__init__.py
script:
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then unit2 discover; else python -m unittest discover; fi
notifications:
hipchat:
rooms:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## [1.4.3] - 2015-10-22
### Fixed
- Reply To header now supports friendly name [#110](https://github.com/sendgrid/sendgrid-python/issues/110)

## [1.4.2] - 2015-09-15
### Added
- Upgrade Mail to new-style class, on Python 2.x.
Expand Down
29 changes: 23 additions & 6 deletions sendgrid/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ def __init__(self, **opts):
self.html = opts.get('html', '')
self.bcc = []
self.add_bcc(opts.get('bcc', []))
self.reply_to = opts.get('reply_to', '')
self.reply_to = ''
self.set_replyto(opts.get('reply_to', ''))
self.files = opts.get('files', {})
self.set_headers(opts.get('headers', ''))
self.headers = {}
self.set_headers(opts.get('headers', {}))
self.date = opts.get('date', rfc822.formatdate())
self.content = opts.get('content', {})
self.smtpapi = opts.get('smtpapi', SMTPAPIHeader())
Expand Down Expand Up @@ -123,7 +125,18 @@ def add_bcc(self, bcc):
self.add_bcc(email)

def set_replyto(self, replyto):
self.reply_to = replyto
name, email = rfc822.parseaddr(replyto.replace(',', ''))
if name and email:
self.set_reply_to_name(replyto)
elif email:
self.reply_to = email

def set_reply_to_name(self, replyto):
headers = {
"Reply-To": replyto
}
self.reply_to = ''
self.set_headers(headers)

def add_attachment(self, name, file_):
if sys.version_info < (3, 0) and isinstance(name, unicode):
Expand All @@ -146,10 +159,14 @@ def add_content_id(self, cid, value):
self.content[cid] = value

def set_headers(self, headers):
if sys.version_info < (3, 0) and isinstance(headers, unicode):
headers = headers.encode('utf-8')
if isinstance(self.headers, str):
self.headers = json.loads(self.headers)
if isinstance(headers, str):
self.headers = headers
else:
self.headers = json.dumps(headers)
headers = json.loads(headers)
for key, value in headers.iteritems():

This comment has been minimized.

Copy link
@urda

urda Sep 25, 2015

iteritems was removed in Python3. This breaks the sendgrid library for all Python 3 users. This has been reported in GitHub Issue #126

self.headers[key] = value

def set_date(self, date):
self.date = date
Expand Down
3 changes: 2 additions & 1 deletion sendgrid/sendgrid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import json
from socket import timeout
from .version import __version__
try:
Expand Down Expand Up @@ -71,7 +72,7 @@ def _build_body(self, message):
'text': message.text,
'html': message.html,
'replyto': message.reply_to,
'headers': message.headers,
'headers': json.dumps(message.headers) if message.headers else '',
'date': message.date,
'x-smtpapi': message.json_string()
}
Expand Down
2 changes: 1 addition & 1 deletion sendgrid/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version_info = (1, 4, 2)
version_info = (1, 4, 3)
__version__ = '.'.join(str(v) for v in version_info)
11 changes: 7 additions & 4 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
from sendgrid.sendgrid import HTTPError

SG_USER = os.getenv('SG_USER') or 'SENDGRID_USERNAME'
SG_PWD = os.getenv('SG_PWD') or 'SENDGRID_PASSWORD'
SG_PWD = os.getenv('SG_PWD') or 'SENDGRID_PASSWORD'


class TestSendGrid(unittest.TestCase):

def setUp(self):
self.sg = SendGridClient(SG_USER, SG_PWD)

Expand Down Expand Up @@ -57,6 +57,7 @@ def test_send(self):
m.add_unique_arg('testUnique', 'uniqueValue')
m.add_filter('testFilter', 'filter', 'filterValue')
m.add_attachment_stream('testFile', 'fileValue')
m.set_replyto('John, Doe <[email protected]>')
url = self.sg._build_body(m)
url.pop('api_key', None)
url.pop('api_user', None)
Expand All @@ -72,8 +73,11 @@ def test_send(self):
"from": "[email protected]",
"cc[]": ["[email protected]"],
"bcc[]": ["[email protected]"]
}
''')
test_url['headers'] = "{\"Reply-To\": \"John, Doe <[email protected]>\"}"

test_url['x-smtpapi'] = json.dumps(json.loads('''
{
"sub": {
Expand Down Expand Up @@ -120,7 +124,6 @@ def test__build_body_unicode(self):
self.assertEqual(text, url['text'])
self.assertEqual(html, url['html'])


def test_smtpapi_add_to(self):
'''Test that message.to gets a dummy address for the header to work'''
m = Mail()
Expand All @@ -146,14 +149,14 @@ def test_smtpapi_add_to(self):
self.assertEqual(url, test_url)



class SendGridClientUnderTest(SendGridClient):

def _make_request(self, message):
raise self.error


class TestSendGridErrorHandling(unittest.TestCase):

def setUp(self):
self.sg = SendGridClientUnderTest(SG_USER, SG_PWD, raise_errors=True)

Expand Down

0 comments on commit afe99dc

Please sign in to comment.