Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sendgrid/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def set_section(self, val):
def add_filter_setting(self, fltr, setting, val):
if 'filters' not in self.data:
self.data['filters'] = {}
if fltr not in self.data:
if fltr not in self.data['filters']:
self.data['filters'][fltr] = {}
if 'settings' not in self.data['filters'][fltr]:
self.data['filters'][fltr]['settings'] = {}
Expand Down
12 changes: 7 additions & 5 deletions sendgrid/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ class Sendgrid(object):
"""
Sendgrid API
"""
def __init__(self, username, password, secure=True):
def __init__(self, username, password, **opts):
"""
Construct Sendgrid API object

Args:
username: Sendgrid uaername
password: Sendgrid password
ssl: Use SSL
secure: Use SSL/TLS
user: Send mail on behalf of this user (web only)
"""
self.username = username
self.password = password
self.secure = secure
self.secure = opts.get('secure', True)
self.user = opts.get('user', None)


@property
Expand All @@ -35,7 +37,7 @@ def web(self):
Return web transport
"""
from transport import web
return web.Http(self.username, self.password, self.secure)
return web.Http(self.username, self.password, ssl=self.secure, user=self.user)


@property
Expand All @@ -45,4 +47,4 @@ def smtp(self):
Return smtp transport
"""
from transport import smtp
return smtp.Smtp(self.username, self.password, self.secure)
return smtp.Smtp(self.username, self.password, tls=self.secure)
4 changes: 2 additions & 2 deletions sendgrid/transport/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Smtp(object):
"""
HOSTPORT = ('smtp.sendgrid.net', 587)

def __init__(self, username, password, tls=True):
def __init__(self, username, password, **opts):
"""
Construct smtp transport object

Expand All @@ -34,7 +34,7 @@ def __init__(self, username, password, tls=True):
"""
self.username = username
self.password = password
self.tls = tls
self.tls = opts.get('tls', True)

def send(self, message):
"""
Expand Down
23 changes: 17 additions & 6 deletions sendgrid/transport/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ class Http(object):
"""
Transport to send emails using http
"""
def __init__(self, username, password, ssl=True):
HOSTPORT = ('sendgrid.com',)

def __init__(self, username, password, **opts):
"""
Construct web transport object

Args:
username: Sendgrid uaername
username: Sendgrid username
password: Sendgrid password
ssl: Use SSL
user: Send mail on behalf of this user
"""
self.username = username
self.password = password
self.ssl = ssl
self.ssl = opts.get('ssl', True)
self.user = opts.get('user', None)

def send(self, message):
"""
Expand All @@ -39,9 +43,12 @@ def send(self, message):
Raises:
SGServiceException: on error
"""
url = "https://sendgrid.com/api/mail.send.json"
protocol = "https://"
if not self.ssl:
url = "http://sendgrid.com/api/mail.send.json"
protocol = "http://"
endpoint = "/api/mail.send.json"

url = protocol + ":".join(map(str, self.HOSTPORT)) + endpoint

data = {
'api_user': self.username,
Expand Down Expand Up @@ -73,6 +80,7 @@ def send(self, message):
'bcc': message.bcc,
'fromname': message.from_name,
'replyto': message.reply_to,
'user': self.user,
}

for key in optional_params:
Expand All @@ -85,7 +93,10 @@ def send(self, message):
f = urllib2.urlopen(req)
output = f.read()
except IOError, e:
output = e.read()
try:
output = e.read()
except AttributeError:
raise exceptions.SGServiceException(e)

output = json.loads(output)

Expand Down