In this example, we want to parse all emails at address@email.sendgrid.biz
and post the parsed email to http://sendgrid.biz/parse
. We will be using Node and the Express framework.
Given this scenario, the following are the parameters you would set at the Parse API settings page:
Hostname: email.sendgrid.biz
URL: http://sendgrid.biz/parse
To test this scenario, we sent an email to example@example.com
and created the following code:
12var express = require('express');3var multer = require('multer');4var app = express();56app.configure(function(){7app.set('port', process.env.PORT || 3000);8app.use(multer());9});1011app.post('/parse', function (req, res) {12var from = req.body.from;13var text = req.body.text;14var subject = req.body.subject;15var num_attachments = req.body.attachments;16for (i = 1; i <= num_attachments; i++){17var attachment = req.files['attachment' + i];18// attachment will be a File object19}20});2122var server = app.listen(app.get('port'), function() {23console.log('Listening on port %d', server.address().port);24});
To use the Event Webhook, you must first setup Event Notification.
In this scenario, we assume you've set the Event Notification URL to go the endpoint /event
on your server. Given this scenario the following code will allow you to process events:
12var express = require('express');3var app = express();45app.configure(function(){6app.set('port', process.env.PORT || 3000);7app.use(express.bodyParser());8});910app.post('/event', function (req, res) {11var events = req.body;12events.forEach(function (event) {13// Here, you now have each event and can process them how you like14processEvent(event);15});16});1718var server = app.listen(app.get('port'), function() {19console.log('Listening on port %d', server.address().port);20});