Skip to content

Commit 899c3b6

Browse files
author
Kane-Sendgrid
committed
Merge pull request sendgrid#10 from davidmcguire/master
Sync up with the official version
2 parents 3cc3059 + 4580135 commit 899c3b6

4 files changed

Lines changed: 27 additions & 14 deletions

File tree

sendgrid/header.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def set_section(self, val):
7171
def add_filter_setting(self, fltr, setting, val):
7272
if 'filters' not in self.data:
7373
self.data['filters'] = {}
74-
if fltr not in self.data:
74+
if fltr not in self.data['filters']:
7575
self.data['filters'][fltr] = {}
7676
if 'settings' not in self.data['filters'][fltr]:
7777
self.data['filters'][fltr]['settings'] = {}

sendgrid/sendgrid.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ class Sendgrid(object):
1414
"""
1515
Sendgrid API
1616
"""
17-
def __init__(self, username, password, secure=True):
17+
def __init__(self, username, password, **opts):
1818
"""
1919
Construct Sendgrid API object
2020
2121
Args:
2222
username: Sendgrid uaername
2323
password: Sendgrid password
24-
ssl: Use SSL
24+
secure: Use SSL/TLS
25+
user: Send mail on behalf of this user (web only)
2526
"""
2627
self.username = username
2728
self.password = password
28-
self.secure = secure
29+
self.secure = opts.get('secure', True)
30+
self.user = opts.get('user', None)
2931

3032

3133
@property
@@ -35,7 +37,7 @@ def web(self):
3537
Return web transport
3638
"""
3739
from transport import web
38-
return web.Http(self.username, self.password, self.secure)
40+
return web.Http(self.username, self.password, ssl=self.secure, user=self.user)
3941

4042

4143
@property
@@ -45,4 +47,4 @@ def smtp(self):
4547
Return smtp transport
4648
"""
4749
from transport import smtp
48-
return smtp.Smtp(self.username, self.password, self.secure)
50+
return smtp.Smtp(self.username, self.password, tls=self.secure)

sendgrid/transport/smtp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Smtp(object):
2323
"""
2424
HOSTPORT = ('smtp.sendgrid.net', 587)
2525

26-
def __init__(self, username, password, tls=True):
26+
def __init__(self, username, password, **opts):
2727
"""
2828
Construct smtp transport object
2929
@@ -34,7 +34,7 @@ def __init__(self, username, password, tls=True):
3434
"""
3535
self.username = username
3636
self.password = password
37-
self.tls = tls
37+
self.tls = opts.get('tls', True)
3838

3939
def send(self, message):
4040
"""

sendgrid/transport/web.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ class Http(object):
1313
"""
1414
Transport to send emails using http
1515
"""
16-
def __init__(self, username, password, ssl=True):
16+
HOSTPORT = ('sendgrid.com',)
17+
18+
def __init__(self, username, password, **opts):
1719
"""
1820
Construct web transport object
1921
2022
Args:
21-
username: Sendgrid uaername
23+
username: Sendgrid username
2224
password: Sendgrid password
2325
ssl: Use SSL
26+
user: Send mail on behalf of this user
2427
"""
2528
self.username = username
2629
self.password = password
27-
self.ssl = ssl
30+
self.ssl = opts.get('ssl', True)
31+
self.user = opts.get('user', None)
2832

2933
def send(self, message):
3034
"""
@@ -39,9 +43,12 @@ def send(self, message):
3943
Raises:
4044
SGServiceException: on error
4145
"""
42-
url = "https://sendgrid.com/api/mail.send.json"
46+
protocol = "https://"
4347
if not self.ssl:
44-
url = "http://sendgrid.com/api/mail.send.json"
48+
protocol = "http://"
49+
endpoint = "/api/mail.send.json"
50+
51+
url = protocol + ":".join(map(str, self.HOSTPORT)) + endpoint
4552

4653
data = {
4754
'api_user': self.username,
@@ -73,6 +80,7 @@ def send(self, message):
7380
'bcc': message.bcc,
7481
'fromname': message.from_name,
7582
'replyto': message.reply_to,
83+
'user': self.user,
7684
}
7785

7886
for key in optional_params:
@@ -85,7 +93,10 @@ def send(self, message):
8593
f = urllib2.urlopen(req)
8694
output = f.read()
8795
except IOError, e:
88-
output = e.read()
96+
try:
97+
output = e.read()
98+
except AttributeError:
99+
raise exceptions.SGServiceException(e)
89100

90101
output = json.loads(output)
91102

0 commit comments

Comments
 (0)