Skip to content

Commit

Permalink
Document SendGrid transport, support API keys
Browse files Browse the repository at this point in the history
vitorio committed Apr 10, 2016
1 parent 7e3b0d1 commit f143887
Showing 2 changed files with 22 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.textile
Original file line number Diff line number Diff line change
@@ -352,6 +352,15 @@ table(configuration).
| @host@ | @"email.us-east-1.amazonaws.com"@ | The API endpoint to utilize. |


h4(#sendgrid-transport). %6.3.1.% SendGrid

The @sendgrid@ transport uses the email service provider SendGrid to deliver your transactional and marketing messages. Use your SendGrid username and password (@user@ and @key@), or supply an API key (only @key@).

table(configuration).
|_. Directive |_. Default |_. Description |
| @user@ | — | Your SendGrid username. Don't include this if you're using an API key. |
| @key@ | — | Your SendGrid password, or a SendGrid account API key. |


h2(#extending). %7.% Extending Marrow Mailer

17 changes: 13 additions & 4 deletions marrow/mailer/transport/sendgrid.py
Original file line number Diff line number Diff line change
@@ -11,10 +11,14 @@


class SendgridTransport(object):
__slots__ = ('ephemeral', 'user', 'key')
__slots__ = ('ephemeral', 'user', 'key', 'bearer')

def __init__(self, config):
self.user = config.get('user')
self.bearer = False
if not 'user' in config:
self.bearer = True
else:
self.user = config.get('user')
self.key = config.get('key')

def startup(self):
@@ -29,8 +33,6 @@ def deliver(self, message):
to.extend(message.cc)

args = dict({
'api_user': self.user,
'api_key': self.key,
'from': [fromaddr.address.encode(message.encoding) for fromaddr in message.author],
'fromname': [fromaddr.name.encode(message.encoding) for fromaddr in message.author],
'to': [toaddr.address.encode(message.encoding) for toaddr in to],
@@ -62,12 +64,19 @@ def deliver(self, message):
msg.attachments = attachments
"""
raise MailConfigurationException()

if not self.bearer:
args['api_user'] = self.user
args['api_key'] = self.key

request = urllib2.Request(
"https://sendgrid.com/api/mail.send.json",
urllib.urlencode(args, True)
)

if self.bearer:
request.add_header("Authorization", "Bearer %s" % self.key)

try:
response = urllib2.urlopen(request)
except (urllib2.HTTPError, urllib2.URLError):

0 comments on commit f143887

Please sign in to comment.