Skip to content

Instantly share code, notes, and snippets.

@Anjireddy4246
Created October 1, 2019 14:50
Show Gist options
  • Save Anjireddy4246/430a6d1790127ec9c999423b68531e79 to your computer and use it in GitHub Desktop.
Save Anjireddy4246/430a6d1790127ec9c999423b68531e79 to your computer and use it in GitHub Desktop.
'use strict';
const https = require('https');
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
var bucketName = 'sessale123';
var simpleParser = require('mailparser').simpleParser;
const doPostMailData = () => {
console.log('Start posting the data');
const data = {
value1: 1,
value2: 2,
};
return new Promise((resolve, reject) => {
const options = {
host: 'apigw-integ.flptitan.com',
path: '/api/admin/V1/portals/3/countries',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
//create the request object with the callback with the result
const req = https.request(options, (res) => {
console.log(JSON.stringify(res.statusCode));
resolve(JSON.stringify(res.statusCode));
});
// handle the possible errors
req.on('error', (e) => {
reject(e.message);
});
//do the request
req.write(JSON.stringify(data));
//finish the request
req.end();
});
}
const parseEmailSync = (data) => {
console.log('parseEmailSync ->' + JSON.stringify(parsed));
simpleParser(data.Body).then(function (parsed) {
console.log("date:", parsed.date);
console.log("subject:", parsed.subject);
console.log("body:", parsed.text);
console.log("from:", parsed.from.text);
console.log("attachments:", parsed.attachments);
});
// simpleParser(data.Body, (err, parsed) => {
// console.log('Simple email parser ->' + JSON.stringify(parsed));
// if (err) {
// console.log(err, err.stack);
// } else {
// console.log("date:", parsed.date);
// console.log("subject:", parsed.subject);
// console.log("body:", parsed.text);
// console.log("from:", parsed.from.text);
// console.log("attachments:", parsed.attachments);
// }
// });
}
const parseEmail = (s3ObjectDetails) => {
return new Promise((resolve, reject) => {
simpleParser(s3ObjectDetails, (err, parsed) => {
var email = new Object();
console.log(parsed, {});
if (err) {
console.log(err, err.stack);
reject(err);
} else {
console.log("date:", parsed.date);
email.date= parsed.date;
email.subject = parsed.subject;
email.body = parsed.text;
email.from = parsed.from.text;
console.log("subject:", parsed.subject);
console.log("body:", parsed.text);
console.log("from:", parsed.from.text);
console.log("attachments:", parsed.attachments);
if (parsed.attachments.length !== undefined && parsed.attachments.length > 0) {
parsed.attachments.forEach(x => {
console.log('Filename' + x.filename);
console.log('Filename' + x.contentType);
s3.putObject({
Bucket: bucketName,
Key: x.filename,
Body: x.content,
ContentType: x.contentType
},function (resp) {
console.log(arguments);
console.log('Successfully uploaded package.');
});
})
}
resolve(email);
}
});
});
}
const getS3ObjectInfo = (req) => {
return new Promise((resolve, reject) => {
s3.getObject(req, function (err, data) {
if (err) {
console.log(err, err.stack);
reject(err);
} else {
console.log("Raw email:\n" + data.Body);
resolve(data);
}
});
});
}
module.exports.handler = async function (event, context, callback) {
console.log('1. message logged from the event');
console.log('2. Read S3 Object started');
console.log('Read S3 Object started');
var s3Object = event.Records[0].s3.object;
console.log("S3 Obj.:\n", JSON.stringify(s3Object, null, 2));
// Retrieve the email from your bucket
var req = {
Bucket: bucketName,
Key: s3Object.key
};
try {
var s3ObjectDetails;
var data = await getS3ObjectInfo(req)
.then(result => {
console.log('Object Body Info -> ' + JSON.stringify(result.Body.toString('utf-8')));
s3ObjectDetails = result.Body.toString('utf-8');
simpleParser(result.Body.toString('utf-8'))
.then(function (parsed) {
// console.log("date:", parsed.date);
// console.log("subject:", parsed.subject);
// console.log("body:", parsed.text);
// console.log("from:", parsed.from.text);
// console.log("attachments:", parsed.attachments);
}).catch(function (err) {
console.log('Error Occured while processing the simple parser:', err.message);
});
console.log('parseEmailSync execution completed');
})
.catch(err => console.log('Error ' + JSON.stringify(err)));
console.log('S3 Object details ->' + s3ObjectDetails);
await parseEmail(s3ObjectDetails)
.then(result => console.log(JSON.stringify(result)))
.catch(err => console.log(err));
}
catch (err) {
console.log('error occurred ->' + JSON.stringify(err));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment