Hello everyone,
I’ve only recently started working with n8n and I’m currently working on a workflow for automatic email sorting.
I’m having a problem with my Code Node and the subsequent IF Node. My Code Node apparently doesn’t reliably recognize emails that contain certain text. Additionally, the IF Node doesn’t output the Boolean value (true or false) as expected, so the emails aren’t being sorted correctly.
Unfortunately, I haven’t been able to find the cause so far.
Does anyone have experience with this problem and can give me a tip on how to configure my workflow correctly?
I’d appreciate any help and thank you in advance for your support!
Best regards
Tanushree Modak
What is the error message (if any)?
Please share your workflow
Share the output returned by the last node
const items = $input.all();
for (const item of items) {
const mail = item.json;
// Cover all possible fields of the IMAP Node
const betreff = mail.envelope?.subject || mail.subject || “”;
const text = mail.text || mail.textContent || mail.body || “”;
const html = mail.html || mail.htmlContent || “”;
// Also search bodyStructure as fallback (as JSON string)
const bodyStr = JSON.stringify(mail.bodyStructure || “”);
const suchtext = (betreff + " " + text + " " + html + " " + bodyStr).toLowerCase();
// Check if “severity: info” (with or without spaces) occurs
mail.infoGefunden = /severity\s*:\sinfo\b/i.test(suchtext);
// Read severity level directly (e.g. “Info”, “Warning”, “Error”)
const match = suchtext.match(/severity\s:\s*(\w+)/i);
mail.severityLevel = match ? match[1] : “unknown”;
}
return items;
Could you share a sanitized example of the IMAP node’s output for an email that should match, along with a screenshot of the IF node’s condition?
In the Code node’s output, check whether infoGefunden is true or false. In the IF node, you can reference {{ $json.infoGefunden }} and use the Boolean is true condition. The IF node then routes the item to its true or false output.
Also check the IMAP node’s Format setting. RAW returns an encoded message, so the text fields your code searches may not be present in that format.
Hi @Tanushree1, your IF screenshot shows a second AND condition with an empty value. Remove it and leave just one condition:
Value: {{ $json.infoGefunden }}
Type/operator: Boolean → is true
The other screenshot shows infoGefunden: false and severityLevel: "unbekannt". With those values, False is the expected branch.
Run the Code node for one email that should contain Severity: Info and check those two fields. If they still show false and unbekannt, please paste that email’s redacted textContent, including the severity line. That will let us check the text matching separately from the IF configuration.
I noticed two things worth checking in your Code node.
First, the code contains curly quotes (“ ”) around values such as the empty string and "unknown". JavaScript string literals need regular quotes ("" or ''), otherwise the code may fail to parse.
Second, your two regex patterns use different whitespace rules. You can simplify the detection and extraction with a shared pattern:
Also, check the actual IMAP node output to confirm where the email body is stored. bodyStructure describes the MIME structure and may not contain the decoded message text, so searching its JSON string may not find the email content.
Finally, in the IF node, check infoGefunden as a Boolean rather than comparing it to the string "true".
If it still fails, sharing one sanitized IMAP output item would help identify the correct subject and body fields.
Hi @Tanushree1, two things in the Code node are worth fixing.
The return shape is not the problem here: mutating item.json and returning items works in Run Once for All Items. But it is safer to return fresh items, so the output is always [{ json: … }]. The real issue is your regexes. /severity\s*:\sinfo\b/ allows exactly one space after the colon, and /severity\s:\s*(\w+)/ requires a space before it, so “Severity: Info” can miss one and fall to “unbekannt”. Also make sure the quotes are plain " characters, not curly ones.
Here is a version with one shared pattern that sets infoGefunden as a real boolean:
const out = $input.all().map((item, index) => {
const mail = item.json;
const subject = mail.envelope?.subject || mail.subject || ‘’;
const text = mail.textContent || mail.text || mail.body || ‘’;
const html = mail.htmlContent || mail.html || ‘’;
const hay = (subject + ’ ’ + text + ’ ’ + html).toLowerCase();
const m = hay.match(/severity\s*:\s*(info|warning|error)\b/);
return {
json: { …mail, infoGefunden: m?.[1] === ‘info’, severityLevel: m?.[1] ?? ‘unbekannt’ },
pairedItem: { item: index }
};
});
return out;
If you prefer Run Once for Each Item, return a single { json: { … } } instead, not an array. Then in the IF node use one condition only: {{ $json.infoGefunden }} with Boolean, is true (remove the empty second AND condition). In the IMAP node, set Format to Resolved or Simple so the text fields exist, since RAW gives an encoded message with no searchable text. The pairedItem line keeps each output linked to its source email.