Here is a module retrieve_email.js
which connects to my gmail account and download the UNSEEN
emails after a date. The code is pretty much copied from the example of the [imap
module]1.
const Imap = require('imap');
const inspect = require('util').inspect;
const simpleParser = require('mailparser').simpleParser;
const imap = new Imap({
user: '[email protected]',
password: 'mypassword',
host: 'imap.gmail.com',
port: 993,
tls: true
});
function openInbox(callback) {
imap.openBox('INBOX', true, callback);
};
async function parse_email(body) {
let parsed = simpleParser(body);
...............
};
module.exports = function() {
imap.once('ready', function() {
openInbox(function(err, box) {
if (error) throw err;
imap.search(['UNSEEN', ['SINCE', 'May 20, 2018']], function(err, results){
if (err) throw err;
var f = imap.fetch(results, {bodies: ''});
f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
if (info.which === 'TEXT')
console.log(prefix + 'Body [%s] found, %d total bytes', inspect(info.which), info.size);
var buffer = '', count = 0;
stream.on('data', function(chunk) {
count += chunk.length;
buffer += chunk.toString('utf8');
parse_email(buffer);
if (info.which === 'TEXT')
console.log(prefix + 'Body [%s] (%d/%d)', inspect(info.which), count, info.size);
});
stream.once('end', function() {
if (info.which !== 'TEXT')
console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
else
console.log(prefix + 'Body [%s] Finished', inspect(info.which));
});
});
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');
});
imap.connect();
};
When the module is called in index.js
, I can see in debug that code is scanned from top to the bottom and the last line of code scanned is imap.connect()
and then back to the next line in index.js
, with no connection to the gmail account and no action of retrieving the emails. What is wrong with the code above?
Simon Cadieux
, there are signin information inimap
definition. Nodemailer can't be used to retrieve imap emails as far as I understand. – Tjonindex.js
that uses it? – Rogarnt
,Styx
, I moved all theimap
code intoindex.js
and went through them many times in debug. I could see the value of theimap
was passed into the imap.connect(). ButopenInbox
was never called and executed. There is no error throw out. I don't quite understand howimap
module works. But it seems to me that the email retrieving (openInbox) was never happened – Tjonimap.once('ready', function () {.....}
, the execution jumps toimap.once('error') & imap.once('end')
, and thenimap.connect()
. Inimap.connect(), it called first
Connection.prototype.connect = function() {` and other methods inconnection.js
, and other methods before exit. The whole lot of code withinopenInbox(function (err,box )...
was not executed at all. This gave me the impression that the connection to inbox and email retrieving were never happened. – Tjon