Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Event Webhook Python Code Example



Parse Webhook

parse-webhook page anchor
(information)

Info

We recommend using our official Python SDK, our client library with full documentation, when integrating with SendGrid's Inbound Parse Webhook(link takes you to an external page).

In this example, we want to parse all emails at address@email.sendgrid.biz and post the parsed email to http://sendgrid.biz/parse. In this example we will be using Python the Flask framework.

Given this scenario, the following are the parameters you would set at the Parse API settings page(link takes you to an external page):


_10
Hostname: email.sendgrid.biz


_10
URL: http://sendgrid.biz/parse

To test this scenario, we sent an email to example@example.com and created the following code:


_31
from flask import Flask, request
_31
import simplejson
_31
app = Flask(__name__)
_31
_31
@app.route('/parse', methods=['POST'])
_31
def sendgrid_parser():
_31
# Consume the entire email
_31
envelope = simplejson.loads(request.form.get('envelope'))
_31
_31
# Get some header information
_31
to_address = envelope['to'][0]
_31
from_address = envelope['from']
_31
_31
# Now, onto the body
_31
text = request.form.get('text')
_31
html = request.form.get('html')
_31
subject = request.form.get('subject')
_31
_31
# Process the attachements, if any
_31
num_attachments = int(request.form.get('attachments', 0))
_31
attachments = []
_31
if num_attachments > 0:
_31
for num in range(1, (num_attachments + 1)):
_31
attachment = request.files.get(('attachment%d' % num))
_31
attachments.append(attachment.read())
_31
# attachment will have all the parameters expected in a Flask file upload
_31
_31
return "OK"
_31
_31
if __name__ == '__main__':
_31
app.run(debug=True)


Rate this page: