From 61a2f9a4ecc1a4bcba9aa1d2a90efd977620555e Mon Sep 17 00:00:00 2001 From: Tim Schubert Date: Sun, 31 Mar 2019 17:39:47 +0200 Subject: [PATCH] Connect to imap and list messages --- inbox2matrix.ts | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 17 ++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 inbox2matrix.ts create mode 100644 package.json diff --git a/inbox2matrix.ts b/inbox2matrix.ts new file mode 100644 index 0000000..13f6b82 --- /dev/null +++ b/inbox2matrix.ts @@ -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(); diff --git a/package.json b/package.json new file mode 100644 index 0000000..142322a --- /dev/null +++ b/package.json @@ -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" +}