I am new to n8n. I want to build a workflow that retrieves older emails through IMAP within a specific date range. My idea was to provide n8n, via the “Edit Fields” node, with a specific date range, for example from 05.04.2025 to 20.04.2025, along with the date at which it should stop and the number of emails it should fetch.
This would then go through an “Edit Fields” node that adds days to the given dates (for example, adding 5 days to 05.04.2025 and 20.04.2025). After that, it would pass the data to the IMAP node.
The main problem is that I can’t figure out how to loop the dates. The loop should add, for example, 5 days each time until it reaches the final date I specified in the Edit Fields node.
The swich is for my logic. If you gave an range, it could happen that the end date 20.04.2025 could be over or equal to the “stop date” and errors could happen. that swich should route to an Edit Field that edits the end range to the stop date.
i tested it but wierdly the “add days and pass date” Edit Field loops and spits after the first round a invaild date error. I am not surprised, it is just wierd.
I know this is probably overcomplicated, but I’ve already searched quite a bit and i can’t come to an conclusion. I also don’t have experience with JavaScript or Python
You’ll have an easier time if you treat this as a backfill loop, not one IMAP query with moving dates.
For IMAP date ranges, build explicit search rules per batch, like ['ALL', ['SINCE', '2025-04-05'], ['BEFORE', '2025-04-21']]. BEFORE is exclusive, so set it to the day after your real end date.
A clean n8n shape is:
Set/Edit Fields -> Code (build next date window) -> Email Trigger/IMAP with Custom Email Rules -> Split Out / Limit
Two gotchas here:
the IMAP search dates need to be real parseable strings, not 05.04.2025 style values
if you want “max 100 emails”, cap that after the IMAP step, not by stretching the date math inside the node
If you want, paste the exact Custom Email Rules value you’re generating now. That’s usually where this breaks.
You’ll have an easier time if you generate the date windows first, then let the IMAP node run once per window.
I’d do it with a Code node that outputs items like { since, before }, then feed that into Loop Over Items and your IMAP node. Something like:
const start = new Date('2025-04-05');
const end = new Date('2025-04-20');
const stepDays = 5;
const out = [];
for (let cur = new Date(start); cur <= end; ) {
const next = new Date(cur);
next.setDate(next.getDate() + stepDays);
const before = next <= end ? next : new Date(end.getTime() + 24*60*60*1000);
out.push({
json: {
since: cur.toISOString().slice(0, 10),
before: before.toISOString().slice(0, 10),
},
});
cur = next;
}
return out;
Then in the IMAP node, map those fields into the search criteria, usually SINCE {{$json.since}} BEFORE {{$json.before}} if that community node expects raw IMAP search terms.
That’s usually cleaner than trying to keep incrementing dates inside Edit Fields, because each loop item is already one complete fetch window. If you want, I can sketch the exact node layout for this.
First of all. Thank you for your Help! and sorry for the late reply
I maybe know what you mean. I couldn’t upadate the codes i filled in, in the workflow i posted. One of the Edit Fields changes the format of the date to 2025-04-05. The problem is that the normal IMAP trigger didn’t really work for me. It only fetches 20 mails if i use the ‘ALL’ even with the ‘SINCE’ varibale. It was limited and i couldn’t edit the Limit. Thats why i use the the community node. Over all yes
I could also fetch every Email from 01.01.2025 to 01.01.2026 but i fear that n8n will break. I have like 50 mails per day.
I really appreciate that, if you can gave me a sketch. i tried it out, but i don’t get the loop over item thing to go. I think I made something wrong
That “invalid date” error usually happens because once n8n modifies a date in a loop, it often starts treating it as a plain string, causing the next mathematical operation to fail. I can help you clean up this logic so your date ranges increment perfectly until they hit your “stop date”.
A Quick Tip for Your Workflow:
Instead of trying to “math” the dates inside multiple Edit Fields, it is much more stable to use a Code Node with a tiny bit of JavaScript. It can generate the entire list of 5-day intervals as a single array, which n8n can then loop through naturally without any formatting errors.
How I Can Help:
Fix the Loop Logic: I’ll restructure the workflow to ensure the date objects stay valid throughout the entire process.
IMAP Optimization: I’ve worked extensively with server-side automation and specialized IMAP nodes, so I can ensure your filters are passed correctly to retrieve exactly what you need.
Infrastructure Support: I host and manage n8n instances on Docker/Ubuntu, so I understand the technical environment your workflow is running in.
About Me:
I’m Mikhail, an AI developer and automation specialist. I recently built a complex notification system that relied on similar date-checking logic and database queries, so I’m very familiar with the “logic traps” you’re facing.