readme: fix/modify examples

fork
mscdex 11 years ago
parent 10f65e4d2a
commit 4208cbb952

@ -25,7 +25,7 @@ Installation
Example Example
======= =======
* Fetch the 'date', 'from', 'to', 'subject' message headers and the message structure of all unread messages in the Inbox since May 20, 2010: * Fetch the 'date', 'from', 'to', 'subject' message headers and the message structure of the first 3 messages in the Inbox:
```javascript ```javascript
var Imap = require('imap'), var Imap = require('imap'),
@ -40,27 +40,17 @@ var imap = new Imap({
secureOptions: { rejectUnauthorized: false } secureOptions: { rejectUnauthorized: false }
}); });
imap.once('ready', function() {
});
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended');
});
function openInbox(cb) { function openInbox(cb) {
imap.openBox('INBOX', true, cb); imap.openBox('INBOX', true, cb);
} }
imap.once('ready', function() {
openInbox(function(err, box) { openInbox(function(err, box) {
if (err) throw err; if (err) throw err;
imap.search([ 'UNSEEN', ['SINCE', 'May 20, 2010'] ], function(err, results) { var f = imap.fetch('1:3', {
if (err) throw err; bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
var f = imap.fetch(results, { bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)' }); struct: true
});
f.on('message', function(msg, seqno) { f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno); console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') '; var prefix = '(#' + seqno + ') ';
@ -89,6 +79,16 @@ openInbox(function(err, box) {
}); });
}); });
}); });
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended');
});
imap.connect();
``` ```
* Retrieve the 'from' header and buffer the entire body of the newest message: * Retrieve the 'from' header and buffer the entire body of the newest message:
@ -98,9 +98,7 @@ openInbox(function(err, box) {
openInbox(function(err, box) { openInbox(function(err, box) {
if (err) throw err; if (err) throw err;
imap.search([ 'UNSEEN', ['SINCE', 'May 20, 2010'] ], function(err, results) { var f = imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM)','TEXT'] });
if (err) throw err;
var f = imap.fetch(results, { bodies: ['HEADER.FIELDS (FROM)','TEXT'] });
f.on('message', function(msg, seqno) { f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno); console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') '; var prefix = '(#' + seqno + ') ';
@ -136,7 +134,6 @@ openInbox(function(err, box) {
imap.end(); imap.end();
}); });
}); });
});
``` ```
* Save raw unread emails since May 20, 2010 to files: * Save raw unread emails since May 20, 2010 to files:

Loading…
Cancel
Save