Skip to content

Instantly share code, notes, and snippets.

@jkantr
Forked from zacharyhill/getNewMail.js
Last active January 31, 2018 18:12
Show Gist options
  • Save jkantr/a4962b28dc58a0e8be1108c5a3fd4d6e to your computer and use it in GitHub Desktop.
Save jkantr/a4962b28dc58a0e8be1108c5a3fd4d6e to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const writeFileAsync = promisify(fs.writeFile);
const s3etm = require('s3-emails-to-mongo');
// MAKE THIS PART DYNAMIC LATER
s3etm.configure({
Bucket: 'zhillb-mail',
});
module.exports = async (req, res, next) => {
try {
const newMail = await s3etm();
// check if new mail has attachment(s), if so save them
await Promise.all(newMail.map((msg) => {
if (msg.attachments.length) {
return Promise.all(msg.attachments.map((attachment) => {
const file = attachment.content;
const cid = attachment.cid;
const filePath = path.join(__dirname, '..', 'data/uploads', cid);
return writeFileAsync(filePath, file).then(() => {
console.log('saved file: ' + filePath);
console.log('mime type: ', attachment.contentType);
}).catch((err) => {
console.log('err with file: ', err);
throw err;
})
}));
}
}))
res.json(newMail);
} catch(err) {
next(err);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment