Skip to content

Commit afe99dc

Browse files
committed
Reply To header now supports friendly name
- Reply To header now supports friendly name [#110](#110)
1 parent bdfc104 commit afe99dc

File tree

6 files changed

+39
-13
lines changed

6 files changed

+39
-13
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ python:
55
- '3.2'
66
install:
77
- python setup.py install
8-
script: python test/__init__.py
8+
script:
9+
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then unit2 discover; else python -m unittest discover; fi
910
notifications:
1011
hipchat:
1112
rooms:

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
## [1.4.3] - 2015-10-22
5+
### Fixed
6+
- Reply To header now supports friendly name [#110](https://github.com/sendgrid/sendgrid-python/issues/110)
7+
48
## [1.4.2] - 2015-09-15
59
### Added
610
- Upgrade Mail to new-style class, on Python 2.x.

sendgrid/message.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ def __init__(self, **opts):
4343
self.html = opts.get('html', '')
4444
self.bcc = []
4545
self.add_bcc(opts.get('bcc', []))
46-
self.reply_to = opts.get('reply_to', '')
46+
self.reply_to = ''
47+
self.set_replyto(opts.get('reply_to', ''))
4748
self.files = opts.get('files', {})
48-
self.set_headers(opts.get('headers', ''))
49+
self.headers = {}
50+
self.set_headers(opts.get('headers', {}))
4951
self.date = opts.get('date', rfc822.formatdate())
5052
self.content = opts.get('content', {})
5153
self.smtpapi = opts.get('smtpapi', SMTPAPIHeader())
@@ -123,7 +125,18 @@ def add_bcc(self, bcc):
123125
self.add_bcc(email)
124126

125127
def set_replyto(self, replyto):
126-
self.reply_to = replyto
128+
name, email = rfc822.parseaddr(replyto.replace(',', ''))
129+
if name and email:
130+
self.set_reply_to_name(replyto)
131+
elif email:
132+
self.reply_to = email
133+
134+
def set_reply_to_name(self, replyto):
135+
headers = {
136+
"Reply-To": replyto
137+
}
138+
self.reply_to = ''
139+
self.set_headers(headers)
127140

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

148161
def set_headers(self, headers):
162+
if sys.version_info < (3, 0) and isinstance(headers, unicode):
163+
headers = headers.encode('utf-8')
164+
if isinstance(self.headers, str):
165+
self.headers = json.loads(self.headers)
149166
if isinstance(headers, str):
150-
self.headers = headers
151-
else:
152-
self.headers = json.dumps(headers)
167+
headers = json.loads(headers)
168+
for key, value in headers.iteritems():
169+
self.headers[key] = value
153170

154171
def set_date(self, date):
155172
self.date = date

sendgrid/sendgrid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import json
23
from socket import timeout
34
from .version import __version__
45
try:
@@ -71,7 +72,7 @@ def _build_body(self, message):
7172
'text': message.text,
7273
'html': message.html,
7374
'replyto': message.reply_to,
74-
'headers': message.headers,
75+
'headers': json.dumps(message.headers) if message.headers else '',
7576
'date': message.date,
7677
'x-smtpapi': message.json_string()
7778
}

sendgrid/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version_info = (1, 4, 2)
1+
version_info = (1, 4, 3)
22
__version__ = '.'.join(str(v) for v in version_info)

test/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
from sendgrid.sendgrid import HTTPError
1717

1818
SG_USER = os.getenv('SG_USER') or 'SENDGRID_USERNAME'
19-
SG_PWD = os.getenv('SG_PWD') or 'SENDGRID_PASSWORD'
19+
SG_PWD = os.getenv('SG_PWD') or 'SENDGRID_PASSWORD'
20+
2021

2122
class TestSendGrid(unittest.TestCase):
22-
2323
def setUp(self):
2424
self.sg = SendGridClient(SG_USER, SG_PWD)
2525

@@ -57,6 +57,7 @@ def test_send(self):
5757
m.add_unique_arg('testUnique', 'uniqueValue')
5858
m.add_filter('testFilter', 'filter', 'filterValue')
5959
m.add_attachment_stream('testFile', 'fileValue')
60+
m.set_replyto('John, Doe <john@email.com>')
6061
url = self.sg._build_body(m)
6162
url.pop('api_key', None)
6263
url.pop('api_user', None)
@@ -72,8 +73,11 @@ def test_send(self):
7273
"from": "doe@email.com",
7374
"cc[]": ["cc@email.com"],
7475
"bcc[]": ["bcc@email.com"]
76+
7577
}
7678
''')
79+
test_url['headers'] = "{\"Reply-To\": \"John, Doe <john@email.com>\"}"
80+
7781
test_url['x-smtpapi'] = json.dumps(json.loads('''
7882
{
7983
"sub": {
@@ -120,7 +124,6 @@ def test__build_body_unicode(self):
120124
self.assertEqual(text, url['text'])
121125
self.assertEqual(html, url['html'])
122126

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

148151

149-
150152
class SendGridClientUnderTest(SendGridClient):
151153

152154
def _make_request(self, message):
153155
raise self.error
154156

155157

156158
class TestSendGridErrorHandling(unittest.TestCase):
159+
157160
def setUp(self):
158161
self.sg = SendGridClientUnderTest(SG_USER, SG_PWD, raise_errors=True)
159162

0 commit comments

Comments
 (0)