mirror of
https://github.com/dadada/inbox2matrix.git
synced 2025-06-08 01:53:57 +02:00
Connect to imap and list messages
This commit is contained in:
commit
61a2f9a4ec
2 changed files with 79 additions and 0 deletions
62
inbox2matrix.ts
Normal file
62
inbox2matrix.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
import process = require('process');
|
||||
import Imap = require('imap');
|
||||
import { inspect } from 'util';
|
||||
import config = require('config');
|
||||
|
||||
let account = config.get('account');
|
||||
|
||||
var imap = new Imap(account);
|
||||
|
||||
function openInbox(cb) {
|
||||
imap.openBox('INBOX', true, cb);
|
||||
}
|
||||
|
||||
imap.once('ready', function() {
|
||||
openInbox(function(err, box) {
|
||||
if (err) throw err;
|
||||
var f = imap.seq.fetch('*', {
|
||||
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) {
|
||||
var buffer = '';
|
||||
stream.on('data', function(chunk) {
|
||||
buffer += chunk.toString('utf8');
|
||||
});
|
||||
stream.once('end', function() {
|
||||
console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
|
||||
});
|
||||
});
|
||||
msg.once('attributes', function(attrs) {
|
||||
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('mail', (numNewMsgs : number) => {
|
||||
console.log(numNewMsgs);
|
||||
});
|
||||
|
||||
imap.once('error', function(err) {
|
||||
console.log(err);
|
||||
});
|
||||
|
||||
imap.once('end', function() {
|
||||
console.log('Connection ended');
|
||||
});
|
||||
|
||||
imap.connect();
|
Loading…
Add table
Add a link
Reference in a new issue