Created
May 23, 2017 09:28
-
-
Save a-sitnikov/3f222788490ff5a54d1057258262476c to your computer and use it in GitHub Desktop.
work with imap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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