Imap Trigger - Loop over multiple items

Hi all,

I’m connecting an IMAP trigger on an email address. But I have an issue, when I set for instance 20 emails to unread, then they are taken into account as a single call by the IMAP trigger, resulting in 20 items in the imap call.

The issue is that I then want to manage each email independently, but only the first one is taken into account. How to manage this?

Please share your workflow

Information on your n8n setup

  • n8n version: 1.83.2
  • Database (default: SQLite): SQLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Own
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system:

but only the first one is taken into account.

Short answer:

Because you tell it so by using $input.first()
Although it’s not the only issue here.

Longer answer:

The problem is that the Code node is not tuned properly.
You need to change its mode to Run once for each item and replace all instances of $input.first().json to $json

Its absolutely possible to achieve the same objective using Run once for all items mode. But then you’d need to pick all items using $input.all() and map each inbound item yourself.

FYI, $input.first() gives you access ony to the first item of all.

I recommed to read through docs (Data | n8n Docs and its subpages) to better understand the n8n peculiar data structure. It will save you a ton of time.

If the above resolves your question, please mark this post as a :white_check_mark: Solution.

Hey Olek,

Thanks a lot for your reply.
I’ll have a look at the link you provided.

Sure, it’s not the only issue here. I know that first() will just serve the first item, but it seems like if I try on all items, editing the code to get all items instead of the first one, then execution still consider only one item:

Here I have 7 items in checking-spam, but only the first one is saved in Execution Data2… Or maybe I’m not seeing something?

Also, I’m not sure it would be great to use a loop, as I have some ‘wait’ elements for multiple days at the end of my flow (sending an email, then another one 3 days later etc.)

→ If I loop on elements, it’ll wait until the loop is processed for each elements right?

You need to show/look at the output panel on the right to know.

The central panel only shows 1 item to give you a hint if you are on the right track overall.

Actually it is better to post the workflow with some data pinned. It can tell more than a 100 of screenshots.

Here is how: How to get help on n8n community forum fast · GitHub (check the part on pinning the data)

I do not think this is a good strategy. Wait can fail. BTW, on some plans the duration of wait is limited.

The better strategy would be to keep a record of actions (using e.g. status for every entry or next step to do along with the action date), and then a build a process that would scan the registry daily and perform actions accordingly. This way you will also be able to watch the progress.

But this is an altogether different topic. I’d recommend to start a new one for this discussion to not confuse the future readers.

Yes, the Wait would block the workflow in its entirety.

Sure, you’re right. Thanks again for your time and answers.

As it’s emails I’m receiving I was not willing to share info on pinned data, but there you go:

I’ve pined the 2nd node as the first one contains binary data, I can’t pin it…

I understand that I might create kind of a loop for the whole process, with status right?
Actually this is related to the current topic: how to loop on IMAP elements, while not having to wait few days before the next item, as at the end of the loop (which is still long, I send emails, then wait 3 days, then resend an email).

A quick comment: you can edit pinned data.

You do not need to refer distant nodes when you have the very same data at hand.

I mean, $json.fieldName (cleanEmail, subject etc) are absolutely accessible and refer to the record that just arrived from the Loop node (in this case).

Avoid $(nodeName) notation at all cost. It is a source of bugs, a nightmare to debug and understand what’s going on. One change in the code can break all those references.

Instead you need to pass the required (and only objectively required) data down the pipeline explicitly. It sounds like an overhead but this effort will pay when you will want to add functionality to your workflow or someone else will need to look into it and fix anything.

You may ask “how do I achieve that when nodes like HTTP Request and AI agent simply do not allow to pass any data from before them?”

This is where you use Merge node.

An example, based on your case:

2 Likes

Well, it is all connected in the meaning that it is part of the process you want to implement.

But it is a perfectly isolatable problem and I would recommend to decouple any following up from per-customer communication initialization, when you need any delays longer than 60 seconds.

Thanks again for your answer.

Super clear.
I’ve then created sub-workflows for later emails. How do I create a trigger much later? Or stack actions to perform on next days?

In your entry workflow (new customer or request) add nodes to store the customer, request data and next action along with the date to execute it.

E.g.

user email subject next step next interaction date
[email protected] Bright new world Reminder 1 2025-04-02
[email protected] Re-hydrate Reminder 2 2025-04-08

Google Sheets, to me, does great job of holding this sort of data.

Then create a standalone workflow (workflows) that will start with a Schedule Trigger node (once a day), it will read the registry and follow a branch based on Next action if current date is the action date, for each record there. For valid (actionable today) actions it will do things and update the record with the next action and next action date.

1 Like

Thanks a lot for all your reply, I manage to make it works now :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.