I am making a javascript program using node.js that create an account in a CEP platform and check for a confirmation email in my gmail inbox. the problem is that I am having trouble in the last part of the script where it check for that confirmation email in the inbox and it is getting into the inbox before it fills out the form and not at the end. I get a 409 error which means that the it has already checked the email hence the authentication error. there are two files that I am using, one where the main script is and the other where it goes into the inbox using imap. Thanks!
const P = require("bluebird");
const Imap = require('imap');
const inspect = require('util').inspect
var config = {
imap: {
user: 'email@gmail.com',
password: 'password1234',
host: 'imap.gmail.com',
port: '993',
tls: true
}
};
readInbox = (imap) => {
console.log(`Reading from Inbox...`);
P.promisify(imap.openBox, { context: imap })('INBOX', true)
.then(box => {
var fetch = imap.seq.fetch(box.messages.total + ':*',
{
bodies: ['HEADER.FIELDS (FROM)', 'TEXT',
]
});
fetch.on('message', function (msg, seqno) {
console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ')';
msg.on('body', function (stream, info) {
console.log('message body started');
if (info.which === 'TEXT', 'utf-8')
console.log(prefix + 'Body [%s] found, %d total bytes', inspect(info.which), info.size);
var buffer = ' ', count = 0;
stream.on('data', function (chunk) {
console.log('body #%d', inspect(info.which), info.size);
count += chunk.length;
buffer += chunk.toString('utf-8');
if (info.which === 'TEXT')
console.log(prefix + 'Body [%s] (%d %d)', inspect(info.which), count, info.size);
});
stream.once('end', function () {
console.log(`Buffer contents: ${buffer}`);
if (info.which !== 'TEXT')
console.log(prefix + 'Parse 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('buffer message', function (buffer) {
console.log(prefix + buffer, inspect(buffer))
})
msg.once('end', function (attrs) {
console.log(prefix + 'finished');
});
});
fetch.once('error', function () {
console.log('Fetch error : ' + err);
});
fetch.once('end', function () {
console.log('done fetching the email!')
imap.end();
})
}).catch(err => console.error("error", err));
};
const imap = new Imap(config.imap);
imap.once('ready', () => readInbox(imap));
imap.once('error', error => console.error(`Error in IMAP connection:`, error));
imap.once('done', () => console.log('Done.'));
imap.once('end', () => console.log('End.'));
imap.once('close', () => console.log('Close.'));
imap.connect();
module.exports = {
readInbox:() => readInbox(imap)
};
via Alberto Mercado
No comments:
Post a Comment