-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (47 loc) · 1.58 KB
/
index.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
'use strict';
console.log('Loading function');
const aws = require('aws-sdk');
const simpleParser = require('mailparser').simpleParser;
const request = require('request');
const querystring = require('querystring');
const s3 = new aws.S3();
exports.handler = (event, context, callback) => {
// Get the object from the event and show its content type
const sesNotification = event.Records[0].ses;
const timestamp = sesNotification.mail.timestamp;
const messageId = sesNotification.mail.messageId;
s3.getObject({
Bucket: 'ses-reviews-emails',
Key: sesNotification.mail.messageId
}, function(err, data) {
if (err) {
console.log(err, err.stack);
callback(err);
} else {
simpleParser(data.Body, (err, mail) => {
const toSave = {
messageId : messageId,
arrivalDate: timestamp,
fromEmail: mail.from.value[0].address,
fromDisplayName: mail.from.value[0].name,
subject: mail.subject,
body: mail.text,
published: false
};
request.post({
url: "http://resapp-env.eu-west-1.elasticbeanstalk.com/reviews",
headers: {
"Content-Type": "application/json"
},
body: toSave,
json:true
}, function(error, response, body){
if (error) {
console.log(error)
}
callback(null, null)
});
})
}
});
};