Skip to content

Commit c015885

Browse files
author
elbuo8
committed
SMTAPI is wrapped inside of Mail object instead of inherited. Version
bump
1 parent b72c918 commit c015885

5 files changed

Lines changed: 90 additions & 11 deletions

File tree

README.rst

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ SendGrid-Python
44
This library allows you to quickly and easily send emails through
55
SendGrid using Python.
66

7+
Warning
8+
-------
9+
10+
If you upgrade to version ``1.2.x``, the ``add_to`` method behaves differently. In the past this method defaulted to using the ``SMTPAPI`` header. Now you must explicitly call the ``smtpapi.add_to`` method. More on the ``SMTPAPI`` section.
11+
712
Install
813
-------
914

@@ -148,45 +153,63 @@ If you wish to use the X-SMTPAPI on your own app, you can use the
148153

149154
There are implementations for setter methods too.
150155

156+
`Recipients`_
157+
~~~~~~~~~~~~~
158+
159+
.. code:: python
160+
161+
message = sendgrid.Mail()
162+
message.smtpapi.add_to('example@email.com')
163+
151164
`Substitution`_
152165
~~~~~~~~~~~~~~~
153166

154167
.. code:: python
155168
156169
message = sendgrid.Mail()
157-
message.add_substitution("key", "value")
170+
message.smtpapi.add_substitution('key', 'value')
171+
# or
172+
message.add_substitution('key', 'value')
158173
159174
`Section`_
160175
~~~~~~~~~~
161176

162177
.. code:: python
163178
164179
message = sendgrid.Mail()
165-
message.add_section("section", "value")
180+
message.smtpapi.add_section('section', 'value')
181+
# or
182+
message.add_section('section', 'value')
166183
167184
`Category`_
168185
~~~~~~~~~~~
169186

170187
.. code:: python
171188
172189
message = sendgrid.Mail()
173-
message.add_category("category")
190+
message.smtpapi.add_category('category')
191+
# or
192+
message.add_category('category')
174193
175194
`Unique Arguments`_
176195
~~~~~~~~~~~~~~~~~~~
177196

178197
.. code:: python
179198
180199
message = sendgrid.Mail()
181-
message.add_unique_arg("key", "value")
200+
message.smtpapi.add_unique_arg('key', 'value')
201+
# or
202+
message.add_unique_arg('key', 'value')
182203
183204
`Filter`_
184205
~~~~~~~~~
185206

186207
.. code:: python
187208
188209
message = sendgrid.Mail()
189-
message.add_filter("filter", "setting", "value")
210+
message.smtpapi.add_filter('filter', 'setting', 'value')
211+
# or
212+
message.add_filter('filter', 'setting', 'value')
190213
191214
192215
Tests

sendgrid/message.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from smtpapi import SMTPAPIHeader
88

99

10-
class Mail(SMTPAPIHeader):
10+
class Mail():
1111

1212
"""SendGrid Message."""
1313

@@ -29,7 +29,6 @@ def __init__(self, **opts):
2929
headers: Set headers
3030
files: Attachments
3131
"""
32-
super(Mail, self).__init__()
3332
self.to = []
3433
self.to_name = []
3534
self.cc = []
@@ -48,9 +47,9 @@ def __init__(self, **opts):
4847
self.headers = opts.get('headers', '')
4948
self.date = opts.get('date', rfc822.formatdate())
5049
self.content = opts.get('content', {})
50+
self.smtpapi = opts.get('smtpapi', SMTPAPIHeader())
5151

5252
def parse_and_add(self, to):
53-
super(Mail, self).add_to(to)
5453
name, email = rfc822.parseaddr(to.replace(',', ''))
5554
if email:
5655
self.to.append(email)
@@ -144,3 +143,35 @@ def set_headers(self, headers):
144143

145144
def set_date(self, date):
146145
self.date = date
146+
147+
# SMTPAPI Wrapper methods
148+
149+
def add_substitution(self, key, value):
150+
self.smtpapi.add_substitution(key, value)
151+
152+
def set_substitutions(self, subs):
153+
self.smtpapi.set_substitutions(subs)
154+
155+
def add_unique_arg(self, key, value):
156+
self.smtpapi.add_unique_arg(key, value)
157+
158+
def set_unique_args(self, args):
159+
self.smtpapi.set_unique_args(args)
160+
161+
def add_category(self, cat):
162+
self.smtpapi.add_category(cat)
163+
164+
def set_categories(self, cats):
165+
self.smtpapi.set_categories(cats)
166+
167+
def add_section(self, key, value):
168+
self.smtpapi.add_section(key, value)
169+
170+
def set_sections(self, sections):
171+
self.smtpapi.set_sections(sections)
172+
173+
def add_filter(self, filterKey, setting, value):
174+
self.smtpapi.add_filter(filterKey, setting, value)
175+
176+
def json_string(self):
177+
return self.smtpapi.json_string()

sendgrid/sendgrid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _build_body(self, message):
5454
values = {
5555
'api_user': self.username,
5656
'api_key': self.password,
57-
'to[]': message.to,
57+
'to[]': message.to if message.to else [message.from_email],
5858
'toname[]': message.to_name,
5959
'cc[]': message.cc,
6060
'bcc[]': message.bcc,

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, 1, 2)
1+
version_info = (1, 2, 0)
22
__version__ = '.'.join(str(v) for v in version_info)

test/__init__.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def test_send(self):
6666
''')
6767
test_url['x-smtpapi'] = json.dumps(json.loads('''
6868
{
69-
"to" : ["John, Doe <john@email.com>"],
7069
"sub": {
7170
"subKey": ["subValue"]
7271
},
@@ -110,6 +109,32 @@ def test__build_body_unicode(self):
110109
self.assertEqual(html, url['html'])
111110

112111

112+
def test_smtpapi_add_to(self):
113+
'''Test that message.to gets a dummy address for the header to work'''
114+
m = Mail()
115+
m.smtpapi.add_to('test@email.com')
116+
m.set_from('jon@doe.com')
117+
m.set_subject('test')
118+
url = self.sg._build_body(m)
119+
url.pop('api_key', None)
120+
url.pop('api_user', None)
121+
url.pop('date', None)
122+
test_url = json.loads('''
123+
{
124+
"to[]": ["jon@doe.com"],
125+
"subject": "test",
126+
"from": "jon@doe.com"
127+
}
128+
''')
129+
test_url['x-smtpapi'] = json.dumps(json.loads('''
130+
{
131+
"to": ["test@email.com"]
132+
}
133+
'''))
134+
self.assertEqual(url, test_url)
135+
136+
137+
113138
class SendGridClientUnderTest(SendGridClient):
114139

115140
def _make_request(self, message):

0 commit comments

Comments
 (0)