Skip to content

Instantly share code, notes, and snippets.

@a-sitnikov
Created May 23, 2017 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-sitnikov/3f222788490ff5a54d1057258262476c to your computer and use it in GitHub Desktop.
Save a-sitnikov/3f222788490ff5a54d1057258262476c to your computer and use it in GitHub Desktop.
work with imap
const Imap = require('imap');
let imap = new Imap({
user: 'user@mail.ru',
password: 'pass',
host: 'imap.mail.ru',
port: 993,
tls: true
});
function imapConnect(imap) {
return new Promise((resolve, reject) => {
console.log('Connecting');
imap.connect();
imap.once('ready', resolve);
});
}
function openInbox(name) {
return new Promise((resolve, reject) => {
console.log('Open inbox');
imap.openBox(name, true, resolve);
});
}
function getMessage(msgEE) {
return new Promise((resolve, reject) => {
let msgObj = {};
msgEE.on('body', async (stream, info) => {
msgObj[info.which] = {
info,
"data": await getStreamData(stream)
};
});
msgEE.once('end', () => resolve(msgObj));
});
}
function getStreamData(stream) {
return new Promise((resolve, reject) => {
let buffer = '', count = 0;
stream.on('data', function(chunk) {
count += chunk.length;
buffer += decode(chunk);
});
stream.once('end', () => resolve(buffer));
});
}
async function run() {
await imapConnect(imap);
await openInbox('INBOX/заявки');
var f = imap.seq.fetch('1:10', {
bodies: ['HEADER.FIELDS (FROM)','TEXT'],
struct: false
});
f.on('message', async (msgEE, seqno) => {
let msg = await getMessage(msgEE);
console.log(msg);
});
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment