215 lines
7.5 KiB
JavaScript
215 lines
7.5 KiB
JavaScript
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 fetchOptions = {
|
|
bodies: ["HEADER.FIELDS (FROM TO SUBJECT DATE)", "TEXT"],
|
|
struct: true,
|
|
};
|
|
|
|
const f = imap.fetch(969, fetchOptions);
|
|
// var f = imap.seq.fetch('1:3', {
|
|
// bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
|
|
// struct: true
|
|
// });
|
|
f.on("message", function (msg, seqno) {
|
|
const emailData = {};
|
|
msg.on("body", (stream, info) => {
|
|
let buffer = "";
|
|
stream.on("data", (chunk) => {
|
|
buffer += chunk.toString("utf8");
|
|
});
|
|
stream.on("end", () => {
|
|
if (info.which === "TEXT") {
|
|
emailData.text = buffer;
|
|
} else {
|
|
const parsedHeader = Imap.parseHeader(buffer);
|
|
emailData.from = parsedHeader.from[0];
|
|
emailData.to = parsedHeader.to[0];
|
|
emailData.subject = parsedHeader.subject[0];
|
|
emailData.date = parsedHeader.date[0];
|
|
}
|
|
});
|
|
});
|
|
msg.once("attributes", (attrs) => {
|
|
emailData.uid = attrs.uid;
|
|
emailData.flags = attrs.flags;
|
|
emailData.struct = attrs.struct;
|
|
});
|
|
msg.once("end", () => {
|
|
console.log("Email data:", inspect(emailData));
|
|
});
|
|
});
|
|
|
|
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' => '<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,
|
|
};
|