const Imap = require("imap"); const { simpleParser } = require("mailparser"); const inspect = require("util").inspect; const saveMessage = require("./storeMessage").saveMessage; const imapConfig = require("./config.json").mail; const fs = require("fs"); const imap = new Imap({ user: imapConfig.user, password: imapConfig.password, 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 imap.search(["ALL"], function (err, results) { console.log(results[results.length - 1]); }); const f = imap.fetch(969, { size: true, struct: true, envelope: true }); // var f = imap.seq.fetch('1:3', { // bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)', // struct: true // }); f.on("message", function (msg, seqno) { // console.log("Message #%d", seqno); // var prefix = "(#" + seqno + ") "; // msg.on("body", function (stream, info) { // simpleParser(stream, async (err, parsed) => { // // find box id; // console.log(parsed) // const boxId = 1; // // saveMessage(parsed, boxId); // console.log(parsed.subject); // fs.writeFileSync("./test.json", JSON.stringify(parsed)); // }); // // console.log(prefix + 'Body'); // // stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt')); // }); msg.once('attributes', attrs => { // todo find boxId const boxId = 1; saveMessage(attrs, boxId, imap); }); }); f.once("error", function (err) { console.log("Fetch error: " + err); }); f.once("end", function () { console.log("Done fetching all messages!"); imap.end(); }); // }); return; // 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; // // console.log(body) // 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; // console.log(attributes) // 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' => '', // // '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, };