Last active
December 23, 2015 17:39
-
-
Save andris9/6669992 to your computer and use it in GitHub Desktop.
node-imap with hoodiecrow
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
// Install dependencies: | |
// npm install imap hoodiecrow | |
var Imap = require('imap'), | |
hoodiecrow = require("hoodiecrow"), | |
inspect = require('util').inspect; | |
var | |
// Message store for hoodiecrow | |
messages = [ | |
{raw: "From: Sender <sender@example.com>\r\n"+ | |
"To: Receiver <receiver@example.com>\r\n"+ | |
"Message-Id: <abcde>\r\n"+ | |
"Subject: Hello world!\r\n"+ | |
"\r\n"+ | |
"Hello world!", | |
flags: ["\\Seen"]} | |
], | |
// Define Hoodiecrow IMAP server | |
server = hoodiecrow({ | |
storage: { | |
"INBOX": { | |
messages: messages | |
} | |
} | |
}); | |
var imap = new Imap({ | |
user: 'testuser', | |
password: 'testpass', | |
host: 'localhost', | |
port: 1143, | |
tls: false | |
}); | |
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('1:3', { | |
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('error', function(err) { | |
console.log(err); | |
}); | |
imap.once('end', function() { | |
console.log('Connection ended'); | |
// Close the server as well | |
server.close(); | |
}); | |
// Connect to the server once it is actually listening | |
server.listen(1143, function(){ | |
imap.connect(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment