How to get IMAP inbox historical messages with attachements?

Hi

I’m trying to build a workflow that retrieves all messages currently in my IMAP inbox (e.g. to batch-process existing mail). I can’t find any node or configuration that will let me fetch everything that’s already in the folder, I see only triger for new messages

The question is what the node should I use to get messages from IMAP inbox?

I use self-hosted docker version n8n 1.92.2

However, it’s important to clarify that the IMAP Email Trigger node in n8n is designed solely to listen for new incoming messages. That means it only activates when a new email arrives in your inbox — it’s not intended to retrieve existing messages that are already in the folder.

What type of email are you trying to connect to for this, Office 365 or G Suite?

Because there are other alternatives but it is important to know your email service provider

@Erick_Torres Thanks so much for your reply. My email is hosted by the Polish provider https://poczta.o2.pl, which doesn’t expose a public API (or at least I’ve been unable to find one). Unfortunately, it isn’t one of the services—like Office 365 or G Suite—that offer a built-in “get all existing messages” operation.

If I were using one of those platforms, the solution would be much more straightforward. Is there any way to add or install a custom IMAP node (or similar) into n8n to enable fetching all existing emails in a folder?

I think what you could do is use a node code with a JS code like the one mentioned here. It might be a solution for you.

You can search on Google for: get all emails imap from js

There’s an example on Stack Overflow.

1 Like

This is an idea for fetching all messages from an IMAP inbox using the node-imap module. The key point is to wait until the connection is ready before trying to open the mailbox.

It would be worth trying something similar because this requires an additional node to be installed: node-imap

const Imap = require('imap');
const { inspect } = require('util');

const imap = new Imap({
    user: Email,
    password: Password,
    host: 'imap.gmail.com',
    port: 993,
    tls: true
});

function openInbox(cb) {
    imap.openBox('INBOX', true, cb);
}

imap.once('ready', function () {
    openInbox(function (err, box) {
        if (err) throw err;

        const f = imap.seq.fetch('1:*', {
            bodies: ['HEADER.FIELDS (FROM)', 'TEXT'],
            struct: true
        });

        f.on('message', function (msg, seqno) {
            console.log('Message #%d', seqno);
            const prefix = '(#' + seqno + ') ';
            msg.on('body', function (stream, info) {
                let buffer = '';
                stream.on('data', function (chunk) {
                    buffer += chunk.toString('utf8');
                });
                stream.once('end', function () {
                    if (info.which === 'TEXT') {
                        console.log(prefix + 'Body:\n', buffer);
                    } else {
                        console.log(prefix + 'Header:\n', inspect(Imap.parseHeader(buffer)));
                    }
                });
            });

            msg.once('attributes', function (attrs) {
                console.log(prefix + 'Attributes:', 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('Connection error: ' + err);
});

imap.once('end', function () {
    console.log('Connection ended');
});

// IMPORTANT: You need to explicitly call connect()
imap.connect();

@Erick_Torres Thank You :slight_smile: I have to check it .