We recommend using our official Python SDK, our client library with full documentation, when integrating with SendGrid's Inbound Parse Webhook.
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:
_10Hostname: email.sendgrid.biz
_10URL: http://sendgrid.biz/parse
To test this scenario, we sent an email to example@example.com
and created the following code:
_31from flask import Flask, request_31import simplejson_31app = Flask(__name__)_31_31@app.route('/parse', methods=['POST'])_31def 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_31if __name__ == '__main__':_31 app.run(debug=True)