mail/back/imap/index.js
2023-02-26 17:23:52 +01:00

162 lines
5.4 KiB
JavaScript

const Imap = require('imap');
const {simpleParser} = require('mailparser');
const inspect = require('util').inspect;
const saveMessage = require('./storeMessage').saveMessage;
const imap = new Imap({
user: '***REMOVED***',
password: '***REMOVED***',
tlsOptions: {servername: "imap.gmail.com"},
host: 'imap.gmail.com',
port: 993,
tls: true
});
imap.once('ready', function() {
const readOnly = true;
imap.openBox('INBOX', readOnly, (err, box) => {
// console.log(box); // uidvalidty uidnext, messages total and new
if (err) throw err;
const f = imap.seq.fetch('2:2', {
bodies: ['HEADER.FIELDS (FROM)','TEXT'],
struct: true,
envelope: true,
extensions: true
});
f.on('message', function(msg, seqno) {
// console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
let attributes = undefined;
let body = undefined;
msg.on('body', function(stream, info) {
simpleParser(stream, async (err, parsed) => {
body = parsed;
if (attributes) {
saveMessage(body, attributes);
};
// console.log(parsed.headers)
// const {from, subject, textAsHtml, text} = parsed;
// console.log(parsed.attachments)
// console.log(prefix + parsed.text)
// console.log(parsed.from.value);
// console.log(parsed.subject);
// console.log(parsed.date)
// console.log(parsed.replyTo.value);
// console.log(parsed.messageId);
// console.log(parsed.html);
// console.log(parsed.text);
// console.log(parsed.textAsHtml);
});
});
msg.once('attributes', attrs => {
attributes = attrs;
if (body) {
saveMessage(body, attributes);
};
// console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
});
msg.once('end', function() {
console.log(prefix + 'Finished');
});
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Done fetching all messages!');
imap.end();
});
});
});
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended');
});
imap.connect();
// const getEmails = () => {
// imap.once('ready', () => {
// imap.openBox('INBOX', false, () => {
// imap.search(['UNSEEN'], (err, results) => {
// const f = imap.fetch(results, {bodies: ''});
// f.on('message', msg => {
// msg.on('body', stream => {
// simpleParser(stream, async (err, parsed) => {
// // const {from, subject, textAsHtml, text} = parsed;
// // console.log(parsed.from.value);
// // console.log(parsed.subject);
// // console.log(parsed.date)
// // console.log(parsed.replyTo.value);
// // console.log(parsed.messageId);
// // console.log(parsed.html);
// // console.log(parsed.text);
// // console.log(parsed.textAsHtml);
// // 'x-emsg-mtaselection' => 'prod_5_emailing.carrefour.fr',
// // 'message-id' => '<emsg.6584.7d09.15dd1ec64c1@ukmme02.em.unica.net>',
// // 'feedback-id' => 'emailing.carrefour.fr:10070-6584:emsg-x',
// // 'list' => { unsubscribe: [Object] },
// // 'mime-version' => '1.0',
// // 'content-type' => { value: 'text/html', params: [Object] },
// // 'content-transfer-encoding' => 'quoted-printable'
// });
// });
// msg.once('attributes', attrs => {
// const {uid} = attrs;
// console.log(uid)
// // imap.addFlags(uid, ['\\Seen'], () => {
// // // Mark the email as read after reading it
// // console.log('Marked as read!');
// // });
// });
// });
// f.once('error', ex => {
// return Promise.reject(ex);
// });
// f.once('end', () => {
// console.log('Done fetching all messages!');
// imap.end();
// });
// });
// });
// });
// imap.once('error', err => {
// console.log(err);
// });
// imap.once('end', () => {
// console.log('Connection ended');
// });
// imap.connect();
// };
// getEmails();
function isValidEmail(email) {
// todo
return true;
}
module.exports = {
isValidEmail
}