diff --git a/sendgrid/header.py b/sendgrid/header.py index b981fd71d..fe8f4e5a5 100644 --- a/sendgrid/header.py +++ b/sendgrid/header.py @@ -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'] = {} diff --git a/sendgrid/sendgrid.py b/sendgrid/sendgrid.py index 82149efaa..65e572557 100644 --- a/sendgrid/sendgrid.py +++ b/sendgrid/sendgrid.py @@ -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 @@ -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 @@ -45,4 +47,4 @@ def smtp(self): Return smtp transport """ from transport import smtp - return smtp.Smtp(self.username, self.password, self.secure) \ No newline at end of file + return smtp.Smtp(self.username, self.password, tls=self.secure) diff --git a/sendgrid/transport/smtp.py b/sendgrid/transport/smtp.py index ac8aea7ab..ddb4edba3 100644 --- a/sendgrid/transport/smtp.py +++ b/sendgrid/transport/smtp.py @@ -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 @@ -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): """ diff --git a/sendgrid/transport/web.py b/sendgrid/transport/web.py index 023eb0fb3..1d543a30d 100644 --- a/sendgrid/transport/web.py +++ b/sendgrid/transport/web.py @@ -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): """ @@ -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, @@ -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: @@ -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)