-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (46 loc) · 1.47 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var express = require('express');
var multiparty = require('multiparty');
var bodyParser = require('body-parser');
var email = require('./email');
var app = express();
app.use(bodyParser.json());
// insert your parse webhook email address
var parseMail = '{PARSE WEBHOOK EMAIL ADDRESS}';
// clientMail addresses can be inserted from the application
var clientAMail = '{CLIENTA@EXAMPLE.COM}';
var clientBMail = '{CLIENTB@EXAMPLE.COM}';
// assumes conversation is between two people and sets recipient as non-sender
function getRecipient(fields, callback) {
var fromField = fields.from[0];
// from field comes formatted as: "Joe Shmoe" <email@example.com>
sender = fromField.slice((fromField.indexOf("<") + 1), -1);
var recipient;
// set recipient choose opposite of sender from clients A/B
if (sender == clientAMail) {
recipient = clientBMail;
} else if (sender == clientBMail) {
recipient = clientAMail;
}
callback(recipient);
}
// parse webhook route
app.post('/incoming', function (req, res) {
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
getRecipient(fields, function(recipient) {
var payload = {
to: recipient,
from: fields.from[0],
replyto: parseMail,
subject: fields.subject[0],
html: fields.html[0],
};
email.emit('send', payload);
});
// response required or webhook will keep trying to send email
res.send('OK');
});
});
app.listen(3000, function() {
console.log('Listening on port 3000');
});