Skip to content

Instantly share code, notes, and snippets.

@richsad
Created March 24, 2014 15:09
Show Gist options
  • Save richsad/9742048 to your computer and use it in GitHub Desktop.
Save richsad/9742048 to your computer and use it in GitHub Desktop.
/**
* be sure to do:
npm install imap
npm install mailparser
**/
var Imap = require('imap'),
MailParser = require('mailparser').MailParser,
fs = require("fs");
/**
* You must replace the parameters to the Imap constructor
* with the appropriate email, password, host, port, tls
* and tlsOptions. See imap module's docs. The
* settings below for host, port, tls and tlsOptions
* work for gmail.
**/
var imap = new Imap({
user: 'somebody@somemail.com',
password: 'yourPassword',
host: 'imap.gmail.com',
port: 993,
tls: true,
tlsOptions: { rejectUnauthorized: false }
});
/**
* this is more or less the example code from the imap README.md page
* modified to work with mailparser. Changed it to use imap.search
* method, changed the fetch to include all "bodies", and added
* an instance of mailparser per email.
*
* wish there had been an example like this that showed the two
* together when I started. All examples were written back
* in imap 0.7 days and didn't work with imap 0.8. This does work
* with current versions of imap and mailparser as of 2014-03-24
**/
function openInbox(cb) {
imap.openBox('INBOX', true, cb);
}
imap.once('ready', function() {
openInbox(function(err, box) {
if (err) throw err;
//search for unread messages
imap.search([ 'UNSEEN', ['SINCE', 'March 22, 2014'] ], function(err, results) {
if (err) throw err;
try {
var fetch = imap.fetch(results, { bodies: '', struct: true });
} catch(error) {
//fetch throws an exception if no errors are found
console.log('no messages were found meeting search criteria');
process.exit();
}
fetch.on('message', function(msg, seqno) {
var prefix = '(#' + seqno + ') ';
var buffer = '';
var mailParser = new MailParser({ streamAttachments: true });
mailParser.on("attachment", function(attachment){
console.log('writing attachment: ' + attachment.generatedFileName);
var output = fs.createWriteStream(attachment.generatedFileName);
attachment.stream.pipe(output);
});
mailParser.on('end', function(mail_object) {
console.log('in mailParser::end');
});
console.log('Message ' + prefix);
msg.on('body', function(stream, info) {
stream.on('data', function(chunk) {
//build buffer with all the pieces of the mail message
buffer += chunk.toString('utf8');
});
stream.once('end', function() {
//send the entire mail message to mailParser
console.log(prefix + " writing buffer to mailParser");
mailParser.write(buffer);
});
});
msg.once('end', function() {
console.log(prefix + 'Finished, calling mailParser');
mailParser.end();
});
});
fetch.once('error', function(err) {
console.log('Fetch error: ' + err);
});
fetch.once('end', function() {
console.log('Done fetching all messages!');
imap.end();
});
});
});
});
imap.once('error', function(err) {
console.log('Imap error: ' + err);
});
imap.once('end', function() {
console.log('Connection ended');
});
imap.connect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment