1
0
Fork 0
mirror of https://github.com/dadada/inbox2matrix.git synced 2025-06-07 17:43:57 +02:00

Connect to imap and list messages

This commit is contained in:
Tim Schubert 2019-03-31 17:39:47 +02:00
commit 61a2f9a4ec
2 changed files with 79 additions and 0 deletions

62
inbox2matrix.ts Normal file
View 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();

17
package.json Normal file
View file

@ -0,0 +1,17 @@
{
"name": "inbox2matrix",
"version": "1.0.0",
"description": "",
"main": "inbox2matrix.js",
"dependencies": {
"config": "^3.0.1",
"imap": "^0.8.19",
"process": "^0.11.10"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}