code
let Imap = require('imap'),
inspect = require('util').inspect;
let imap = new Imap({
user: '********@free.fr',
password: '***********',
host: 'imap.free.fr',
port: 993,
tls: true,
tlsOptions: { rejectUnauthorized: false }
});
function openInbox(cb) {
imap.openBox('INBOX', true, cb);
}
imap.once('ready', function() {
console.log('imap.once ready')
openInbox(function(err, box) {
if (err) throw err;
let f = imap.seq.fetch('1:8', {
bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
struct: true
});
f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno);
let prefix = '(#' + seqno + ') ';
let buffer = '', count = 0;
msg.on('body', function(stream, info) {
if (info.which === 'TEXT')
console.log(prefix + 'Body [%s] found, %d total bytes', inspect(info.which), info.size);
let buffer = '', count = 0;
stream.on('data', function(chunk) {
count += chunk.length;
buffer += chunk.toString('utf8');
console.log('BUFFER', buffer) //HEre i am able to view the body
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('imap.once error',err)
});
imap.once('end', function() {
console.log('imap.once ready Connection ended');
});
imap.connect();
result
I20230220-11:32:56.186(1)? Message #8
I20230220-11:32:56.186(1)? BUFFER Date: Thu, 28 Apr 2022 04:08:43 +0000 (UTC)
I20230220-11:32:56.186(1)? From: christophe Laithier <chriselec@yahoo.com>
I20230220-11:32:56.188(1)? To: "yvancoyaud@free.fr" <yvancoyaud@free.fr>
I20230220-11:32:56.189(1)? Subject: facture
I20230220-11:32:56.190(1)?
I20230220-11:32:56.190(1)?
I20230220-11:32:56.192(1)? (#8) Parsed header: {
I20230220-11:32:56.194(1)? date: [ 'Thu, 28 Apr 2022 04:08:43 +0000 (UTC)' ],
I20230220-11:32:56.194(1)? from: [ 'christophe Laithier <chriselec@yahoo.com>' ],
I20230220-11:32:56.194(1)? to: [ '"yvancoyaud@free.fr" <yvancoyaud@free.fr>' ],
I20230220-11:32:56.194(1)? subject: [ 'facture' ]
I20230220-11:32:56.194(1)? }
I20230220-11:32:56.238(1)? (#8) Attributes: {
I20230220-11:32:56.239(1)? struct: [
I20230220-11:32:56.239(1)? {
I20230220-11:32:56.239(1)? type: 'mixed',
I20230220-11:32:56.239(1)? params: { boundary: '----=_Part_3345193_364952653.1651118923025' },
I20230220-11:32:56.242(1)? disposition: null,
I20230220-11:32:56.243(1)? language: null,
I20230220-11:32:56.243(1)? location: null
I20230220-11:32:56.243(1)? },
I20230220-11:32:56.243(1)? [
I20230220-11:32:56.243(1)? {
I20230220-11:32:56.244(1)? type: 'alternative',
I20230220-11:32:56.244(1)? params: { boundary: '----=_Part_3345192_240620121.1651118923023' },
I20230220-11:32:56.244(1)? disposition: null,
I20230220-11:32:56.263(1)? language: null,
I20230220-11:32:56.267(1)? location: null
I20230220-11:32:56.267(1)? },
I20230220-11:32:56.267(1)? [
I20230220-11:32:56.267(1)? {
I20230220-11:32:56.278(1)? partID: '1.1',
I20230220-11:32:56.279(1)? type: 'text',
I20230220-11:32:56.279(1)? subtype: 'plain',
I20230220-11:32:56.279(1)? params: { charset: 'UTF-8' },
I20230220-11:32:56.279(1)? id: null,
I20230220-11:32:56.279(1)? description: null,
I20230220-11:32:56.279(1)? encoding: 'QUOTED-PRINTABLE',
I20230220-11:32:56.279(1)? size: 177,
I20230220-11:32:56.279(1)? lines: 4,
I20230220-11:32:56.279(1)? md5: null,
I20230220-11:32:56.279(1)? disposition: null,
I20230220-11:32:56.280(1)? language: null,
I20230220-11:32:56.283(1)? location: null
I20230220-11:32:56.285(1)? }
I20230220-11:32:56.285(1)? ],
I20230220-11:32:56.285(1)? [
I20230220-11:32:56.285(1)? {
I20230220-11:32:56.285(1)? partID: '1.2',
I20230220-11:32:56.290(1)? type: 'text',
I20230220-11:32:56.291(1)? subtype: 'html',
I20230220-11:32:56.292(1)? params: { charset: 'UTF-8' },
I20230220-11:32:56.292(1)? id: null,
I20230220-11:32:56.293(1)? description: null,
I20230220-11:32:56.294(1)? encoding: 'QUOTED-PRINTABLE',
I20230220-11:32:56.294(1)? size: 584,
I20230220-11:32:56.295(1)? lines: 8,
I20230220-11:32:56.295(1)? md5: null,
I20230220-11:32:56.295(1)? disposition: null,
I20230220-11:32:56.295(1)? language: null,
I20230220-11:32:56.296(1)? location: null
I20230220-11:32:56.296(1)? }
I20230220-11:32:56.296(1)? ]
I20230220-11:32:56.296(1)? ],
I20230220-11:32:56.296(1)? [
I20230220-11:32:56.296(1)? {
I20230220-11:32:56.296(1)? partID: '2',
I20230220-11:32:56.296(1)? type: 'application',
I20230220-11:32:56.296(1)? subtype: 'pdf',
I20230220-11:32:56.296(1)? params: null,
I20230220-11:32:56.296(1)? id: '<9da31cfe-609e-a92d-7f60-dfc13dd9740b@yahoo.com>',
I20230220-11:32:56.296(1)? description: null,
I20230220-11:32:56.296(1)? encoding: 'BASE64',
I20230220-11:32:56.297(1)? size: 176034,
I20230220-11:32:56.299(1)? md5: null,
I20230220-11:32:56.299(1)? disposition: { type: 'attachment', params: { filename: 'Mr Coyaud.pdf' } },
I20230220-11:32:56.299(1)? language: null,
I20230220-11:32:56.299(1)? location: null
I20230220-11:32:56.301(1)? }
I20230220-11:32:56.301(1)? ]
I20230220-11:32:56.302(1)? ],
I20230220-11:32:56.302(1)? date: 2022-04-28T04:08:40.000Z,
I20230220-11:32:56.302(1)? flags: [ '\\Seen' ],
I20230220-11:32:56.302(1)? uid: 339214,
I20230220-11:32:56.302(1)? modseq: '1363827'
I20230220-11:32:56.302(1)? }
I20230220-11:32:56.302(1)? (#8) Finished
I20230220-11:32:56.302(1)? Done fetching all messages!
Hi all
Can someone tell me why the code above does not return the body of the emails ?
Thank you for your answers.
YC