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:
_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:
_24_24var express = require('express');_24var multer = require('multer');_24var app = express();_24_24app.configure(function(){_24 app.set('port', process.env.PORT || 3000);_24 app.use(multer());_24});_24_24app.post('/parse', function (req, res) {_24 var from = req.body.from;_24 var text = req.body.text;_24 var subject = req.body.subject;_24 var num_attachments = req.body.attachments;_24 for (i = 1; i <= num_attachments; i++){_24 var attachment = req.files['attachment' + i];_24 // attachment will be a File object_24 }_24});_24_24var server = app.listen(app.get('port'), function() {_24 console.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:
_20_20var express = require('express');_20var app = express();_20_20app.configure(function(){_20 app.set('port', process.env.PORT || 3000);_20 app.use(express.bodyParser());_20});_20_20app.post('/event', function (req, res) {_20 var events = req.body;_20 events.forEach(function (event) {_20 // Here, you now have each event and can process them how you like_20 processEvent(event);_20 });_20});_20_20var server = app.listen(app.get('port'), function() {_20 console.log('Listening on port %d', server.address().port);_20});