first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled

This commit is contained in:
2026-03-17 16:22:57 +03:30
commit 3d5eaf9445
15349 changed files with 2847338 additions and 0 deletions
@@ -0,0 +1,28 @@
{
"node": "n8n-nodes-base.emailReadImap",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": ["Communication", "Core Nodes"],
"resources": {
"credentialDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/credentials/imap/"
}
],
"primaryDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.emailimap/"
}
],
"generic": [
{
"label": "Build your own virtual assistant with n8n: A step by step guide",
"icon": "👦",
"url": "https://n8n.io/blog/build-your-own-virtual-assistant-with-n8n-a-step-by-step-guide/"
}
]
},
"subcategories": {
"Core Nodes": ["Other Trigger Nodes"]
}
}
@@ -0,0 +1,26 @@
import type { INodeTypeBaseDescription, IVersionedNodeType } from 'n8n-workflow';
import { VersionedNodeType } from 'n8n-workflow';
import { EmailReadImapV1 } from './v1/EmailReadImapV1.node';
import { EmailReadImapV2 } from './v2/EmailReadImapV2.node';
export class EmailReadImap extends VersionedNodeType {
constructor() {
const baseDescription: INodeTypeBaseDescription = {
displayName: 'Email Trigger (IMAP)',
name: 'emailReadImap',
icon: 'fa:inbox',
group: ['trigger'],
description: 'Triggers the workflow when a new email is received',
defaultVersion: 2.1,
};
const nodeVersions: IVersionedNodeType['nodeVersions'] = {
1: new EmailReadImapV1(baseDescription),
2: new EmailReadImapV2(baseDescription),
2.1: new EmailReadImapV2(baseDescription),
};
super(nodeVersions, baseDescription);
}
}
@@ -0,0 +1,67 @@
import { mock } from 'jest-mock-extended';
import type { INode, INodeTypeBaseDescription, ITriggerFunctions } from 'n8n-workflow';
import { type ICredentialsDataImap } from '@credentials/Imap.credentials';
import { EmailReadImapV2 } from '../../v2/EmailReadImapV2.node';
jest.mock('@n8n/imap', () => {
const originalModule = jest.requireActual('@n8n/imap');
return {
...originalModule,
connect: jest.fn().mockImplementation(() => ({
then: jest.fn().mockImplementation(() => ({
openBox: jest.fn().mockResolvedValue({}),
})),
})),
};
});
describe('Test IMap V2', () => {
const triggerFunctions = mock<ITriggerFunctions>({
helpers: {
createDeferredPromise: jest.fn().mockImplementation(() => {
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}),
},
});
const credentials: ICredentialsDataImap = {
host: 'imap.gmail.com',
port: 993,
user: 'user',
password: 'password',
secure: false,
allowUnauthorizedCerts: false,
};
triggerFunctions.getCredentials.calledWith('imap').mockResolvedValue(credentials);
triggerFunctions.getNode.mockReturnValue(mock<INode>({ typeVersion: 2.1 }));
triggerFunctions.logger.debug = jest.fn();
triggerFunctions.getNodeParameter.calledWith('options').mockReturnValue({
name: 'Mark as Read',
value: 'read',
});
const baseDescription: INodeTypeBaseDescription = {
displayName: 'EmailReadImapV2',
name: 'emailReadImapV2',
icon: 'file:removeDuplicates.svg',
group: ['transform'],
description: 'Delete items with matching field values',
};
afterEach(() => jest.resetAllMocks());
it('should run return a close function on success', async () => {
const result = await new EmailReadImapV2(baseDescription).trigger.call(triggerFunctions);
expect(result.closeFunction).toBeDefined();
});
});
@@ -0,0 +1,100 @@
import { type ImapSimple } from '@n8n/imap';
import { mock, mockDeep } from 'jest-mock-extended';
import { returnJsonArray } from 'n8n-core';
import type { INode, ITriggerFunctions } from 'n8n-workflow';
import { getNewEmails } from '../../v2/utils';
describe('Test IMap V2 utils', () => {
afterEach(() => jest.resetAllMocks());
describe('getNewEmails', () => {
const triggerFunctions = mockDeep<ITriggerFunctions>({
helpers: { returnJsonArray },
});
const message = {
attributes: {
uuid: 1,
uid: 873,
struct: {},
},
parts: [
{ which: '', body: 'Body content' },
{ which: 'HEADER', body: 'h' },
{ which: 'TEXT', body: 'txt' },
],
};
const imapConnection = mock<ImapSimple>({
search: jest.fn().mockReturnValue(Promise.resolve([message])),
});
const getText = jest.fn().mockReturnValue('text');
const getAttachment = jest.fn().mockReturnValue(['attachment']);
it('should return new emails', async () => {
const expectedResults = [
{
format: 'resolved',
expected: {
json: {
attachments: undefined,
headers: { '': 'Body content' },
headerLines: undefined,
html: false,
attributes: {
uid: 873,
},
},
binary: undefined,
},
},
{
format: 'simple',
expected: {
json: {
textHtml: 'text',
textPlain: 'text',
metadata: {
'0': 'h',
},
attributes: {
uid: 873,
},
},
},
},
{
format: 'raw',
expected: {
json: { raw: 'txt' },
},
},
];
expectedResults.forEach(async (expectedResult) => {
triggerFunctions.getNode.mockReturnValue(mock<INode>({ typeVersion: 2.1 }));
triggerFunctions.getNodeParameter
.calledWith('format')
.mockReturnValue(expectedResult.format);
triggerFunctions.getNodeParameter
.calledWith('dataPropertyAttachmentsPrefixName')
.mockReturnValue('resolved');
triggerFunctions.getWorkflowStaticData.mockReturnValue({});
const onEmailBatch = jest.fn();
await getNewEmails.call(triggerFunctions, {
imapConnection,
searchCriteria: [],
postProcessAction: '',
getText,
getAttachment,
onEmailBatch,
});
expect(onEmailBatch).toHaveBeenCalledTimes(1);
expect(onEmailBatch).toHaveBeenCalledWith([expectedResult.expected]);
});
});
});
});
@@ -0,0 +1,642 @@
import type { ImapSimple, ImapSimpleOptions, Message, SearchCriteria } from '@n8n/imap';
import { connect as imapConnect, getParts } from '@n8n/imap';
import find from 'lodash/find';
import isEmpty from 'lodash/isEmpty';
import type { Source as ParserSource } from 'mailparser';
import { simpleParser } from 'mailparser';
import { NodeConnectionTypes, NodeOperationError } from 'n8n-workflow';
import type {
ITriggerFunctions,
IBinaryData,
IBinaryKeyData,
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
INodeCredentialTestResult,
INodeExecutionData,
INodeType,
INodeTypeBaseDescription,
INodeTypeDescription,
ITriggerResponse,
} from 'n8n-workflow';
export async function parseRawEmail(
this: ITriggerFunctions,
messageEncoded: ParserSource,
dataPropertyNameDownload: string,
): Promise<INodeExecutionData> {
const responseData = await simpleParser(messageEncoded);
const headers: IDataObject = {};
for (const header of responseData.headerLines) {
headers[header.key] = header.line;
}
// @ts-ignore
responseData.headers = headers;
// @ts-ignore
responseData.headerLines = undefined;
const binaryData: IBinaryKeyData = {};
if (responseData.attachments) {
for (let i = 0; i < responseData.attachments.length; i++) {
const attachment = responseData.attachments[i];
binaryData[`${dataPropertyNameDownload}${i}`] = await this.helpers.prepareBinaryData(
attachment.content,
attachment.filename,
attachment.contentType,
);
}
// @ts-ignore
responseData.attachments = undefined;
}
return {
json: responseData as unknown as IDataObject,
binary: Object.keys(binaryData).length ? binaryData : undefined,
} as INodeExecutionData;
}
const versionDescription: INodeTypeDescription = {
displayName: 'Email Trigger (IMAP)',
name: 'emailReadImap',
icon: 'fa:inbox',
group: ['trigger'],
version: 1,
description: 'Triggers the workflow when a new email is received',
eventTriggerDescription: 'Waiting for you to receive an email',
defaults: {
name: 'Email Trigger (IMAP)',
color: '#44AA22',
},
triggerPanel: {
header: '',
executionsHelp: {
inactive:
"<b>While building your workflow</b>, click the 'execute step' button, then send an email to make an event happen. This will trigger an execution, which will show up in this editor.<br /> <br /><b>Once you're happy with your workflow</b>, publish it. Then every time an email is received, the workflow will execute. These executions will show up in the <a data-key='executions'>executions list</a>, but not in the editor.",
active:
"<b>While building your workflow</b>, click the 'execute step' button, then send an email to make an event happen. This will trigger an execution, which will show up in this editor.<br /> <br /><b>Your workflow will also execute automatically</b>, since it's activated. Every time an email is received, this node will trigger an execution. These executions will show up in the <a data-key='executions'>executions list</a>, but not in the editor.",
},
activationHint:
'Once youve finished building your workflow, publish it to have it also listen continuously (you just wont see those executions here).',
},
inputs: [],
outputs: [NodeConnectionTypes.Main],
credentials: [
{
name: 'imap',
required: true,
testedBy: 'imapConnectionTest',
},
],
properties: [
{
displayName: 'Mailbox Name',
name: 'mailbox',
type: 'string',
default: 'INBOX',
},
{
displayName: 'Action',
name: 'postProcessAction',
type: 'options',
options: [
{
name: 'Mark as Read',
value: 'read',
},
{
name: 'Nothing',
value: 'nothing',
},
],
default: 'read',
description:
'What to do after the email has been received. If "nothing" gets selected it will be processed multiple times.',
},
{
displayName: 'Download Attachments',
name: 'downloadAttachments',
type: 'boolean',
default: false,
displayOptions: {
show: {
format: ['simple'],
},
},
description:
'Whether attachments of emails should be downloaded. Only set if needed as it increases processing.',
},
{
displayName: 'Format',
name: 'format',
type: 'options',
options: [
{
name: 'RAW',
value: 'raw',
description:
'Returns the full email message data with body content in the raw field as a base64url encoded string; the payload field is not used',
},
{
name: 'Resolved',
value: 'resolved',
description:
'Returns the full email with all data resolved and attachments saved as binary data',
},
{
name: 'Simple',
value: 'simple',
description:
'Returns the full email; do not use if you wish to gather inline attachments',
},
],
default: 'simple',
description: 'The format to return the message in',
},
{
displayName: 'Property Prefix Name',
name: 'dataPropertyAttachmentsPrefixName',
type: 'string',
default: 'attachment_',
displayOptions: {
show: {
format: ['resolved'],
},
},
description:
'Prefix for name of the binary property to which to write the attachments. An index starting with 0 will be added. So if name is "attachment_" the first attachment is saved to "attachment_0"',
},
{
displayName: 'Property Prefix Name',
name: 'dataPropertyAttachmentsPrefixName',
type: 'string',
default: 'attachment_',
displayOptions: {
show: {
format: ['simple'],
downloadAttachments: [true],
},
},
description:
'Prefix for name of the binary property to which to write the attachments. An index starting with 0 will be added. So if name is "attachment_" the first attachment is saved to "attachment_0"',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add option',
default: {},
options: [
{
displayName: 'Custom Email Rules',
name: 'customEmailConfig',
type: 'string',
default: '["UNSEEN"]',
description:
'Custom email fetching rules. See <a href="https://github.com/mscdex/node-imap">node-imap</a>\'s search function for more details.',
},
{
displayName: 'Ignore SSL Issues (Insecure)',
name: 'allowUnauthorizedCerts',
type: 'boolean',
default: false,
description: 'Whether to connect even if SSL certificate validation is not possible',
},
{
displayName: 'Force Reconnect',
name: 'forceReconnect',
type: 'number',
default: 60,
description: 'Sets an interval (in minutes) to force a reconnection',
},
],
},
],
};
export class EmailReadImapV1 implements INodeType {
description: INodeTypeDescription;
constructor(baseDescription: INodeTypeBaseDescription) {
this.description = {
...baseDescription,
...versionDescription,
};
}
methods = {
credentialTest: {
async imapConnectionTest(
this: ICredentialTestFunctions,
credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
const credentials = credential.data as ICredentialDataDecryptedObject;
try {
const config: ImapSimpleOptions = {
imap: {
user: credentials.user as string,
password: credentials.password as string,
host: (credentials.host as string).trim(),
port: credentials.port as number,
tls: credentials.secure as boolean,
authTimeout: 20000,
},
};
const tlsOptions: IDataObject = {};
if (credentials.secure) {
tlsOptions.servername = (credentials.host as string).trim();
}
if (!isEmpty(tlsOptions)) {
config.imap.tlsOptions = tlsOptions;
}
const connection = await imapConnect(config);
await connection.getBoxes();
} catch (error) {
return {
status: 'Error',
message: error.message,
};
}
return {
status: 'OK',
message: 'Connection successful!',
};
},
},
};
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const credentials = await this.getCredentials('imap');
const mailbox = this.getNodeParameter('mailbox') as string;
const postProcessAction = this.getNodeParameter('postProcessAction') as string;
const options = this.getNodeParameter('options', {}) as IDataObject;
const staticData = this.getWorkflowStaticData('node');
this.logger.debug('Loaded static data for node "EmailReadImap"', { staticData });
let connection: ImapSimple;
// Returns the email text
const getText = async (parts: any[], message: Message, subtype: string): Promise<string> => {
if (!message.attributes.struct) {
return '';
}
const textParts = parts.filter((part) => {
return (
part.type.toUpperCase() === 'TEXT' && part.subtype.toUpperCase() === subtype.toUpperCase()
);
});
const part = textParts[0];
if (!part) {
return '';
}
try {
const partData = await connection.getPartData(message, part);
return partData.toString();
} catch {
return '';
}
};
// Returns the email attachments
const getAttachment = async (
imapConnection: ImapSimple,
parts: any[],
message: Message,
): Promise<IBinaryData[]> => {
if (!message.attributes.struct) {
return [];
}
// Check if the message has attachments and if so get them
const attachmentParts = parts.filter((part) => {
return part.disposition && part.disposition.type.toUpperCase() === 'ATTACHMENT';
});
const attachmentPromises = [];
let attachmentPromise;
for (const attachmentPart of attachmentParts) {
attachmentPromise = imapConnection
.getPartData(message, attachmentPart)
.then(async (partData) => {
// Return it in the format n8n expects
return await this.helpers.prepareBinaryData(
partData.buffer,
attachmentPart.disposition.params.filename as string,
);
});
attachmentPromises.push(attachmentPromise);
}
return await Promise.all(attachmentPromises);
};
// Returns all the new unseen messages
const getNewEmails = async (
imapConnection: ImapSimple,
searchCriteria: SearchCriteria[],
): Promise<INodeExecutionData[]> => {
const format = this.getNodeParameter('format', 0) as string;
let fetchOptions = {};
if (format === 'simple' || format === 'raw') {
fetchOptions = {
bodies: ['TEXT', 'HEADER'],
markSeen: false,
struct: true,
};
} else if (format === 'resolved') {
fetchOptions = {
bodies: [''],
markSeen: false,
struct: true,
};
}
const results = await imapConnection.search(searchCriteria, fetchOptions);
const newEmails: INodeExecutionData[] = [];
let newEmail: INodeExecutionData;
let attachments: IBinaryData[];
let propertyName: string;
// All properties get by default moved to metadata except the ones
// which are defined here which get set on the top level.
const topLevelProperties = ['cc', 'date', 'from', 'subject', 'to'];
if (format === 'resolved') {
const dataPropertyAttachmentsPrefixName = this.getNodeParameter(
'dataPropertyAttachmentsPrefixName',
) as string;
for (const message of results) {
if (
staticData.lastMessageUid !== undefined &&
message.attributes.uid <= (staticData.lastMessageUid as number)
) {
continue;
}
if (
staticData.lastMessageUid === undefined ||
(staticData.lastMessageUid as number) < message.attributes.uid
) {
staticData.lastMessageUid = message.attributes.uid;
}
const part = find(message.parts, { which: '' });
if (part === undefined) {
throw new NodeOperationError(this.getNode(), 'Email part could not be parsed.');
}
const parsedEmail = await parseRawEmail.call(
this,
part.body as Buffer,
dataPropertyAttachmentsPrefixName,
);
newEmails.push(parsedEmail);
}
} else if (format === 'simple') {
const downloadAttachments = this.getNodeParameter('downloadAttachments') as boolean;
let dataPropertyAttachmentsPrefixName = '';
if (downloadAttachments) {
dataPropertyAttachmentsPrefixName = this.getNodeParameter(
'dataPropertyAttachmentsPrefixName',
) as string;
}
for (const message of results) {
if (
staticData.lastMessageUid !== undefined &&
message.attributes.uid <= (staticData.lastMessageUid as number)
) {
continue;
}
if (
staticData.lastMessageUid === undefined ||
(staticData.lastMessageUid as number) < message.attributes.uid
) {
staticData.lastMessageUid = message.attributes.uid;
}
const parts = getParts(message.attributes.struct!);
newEmail = {
json: {
textHtml: await getText(parts, message, 'html'),
textPlain: await getText(parts, message, 'plain'),
metadata: {} as IDataObject,
},
};
const messageHeader = message.parts.filter((part) => part.which === 'HEADER');
const messageBody = messageHeader[0].body as Record<string, string[]>;
for (propertyName of Object.keys(messageBody as IDataObject)) {
if (messageBody[propertyName].length) {
if (topLevelProperties.includes(propertyName)) {
newEmail.json[propertyName] = messageBody[propertyName][0];
} else {
(newEmail.json.metadata as IDataObject)[propertyName] =
messageBody[propertyName][0];
}
}
}
if (downloadAttachments) {
// Get attachments and add them if any get found
attachments = await getAttachment(imapConnection, parts, message);
if (attachments.length) {
newEmail.binary = {};
for (let i = 0; i < attachments.length; i++) {
newEmail.binary[`${dataPropertyAttachmentsPrefixName}${i}`] = attachments[i];
}
}
}
newEmails.push(newEmail);
}
} else if (format === 'raw') {
for (const message of results) {
if (
staticData.lastMessageUid !== undefined &&
message.attributes.uid <= (staticData.lastMessageUid as number)
) {
continue;
}
if (
staticData.lastMessageUid === undefined ||
(staticData.lastMessageUid as number) < message.attributes.uid
) {
staticData.lastMessageUid = message.attributes.uid;
}
const part = find(message.parts, { which: 'TEXT' });
if (part === undefined) {
throw new NodeOperationError(this.getNode(), 'Email part could not be parsed.');
}
// Return base64 string
newEmail = {
json: {
raw: part.body,
},
};
newEmails.push(newEmail);
}
}
// only mark messages as seen once processing has finished
if (postProcessAction === 'read') {
const uidList = results.map((e) => e.attributes.uid);
if (uidList.length > 0) {
await imapConnection.addFlags(uidList, '\\SEEN');
}
}
return newEmails;
};
const returnedPromise = this.helpers.createDeferredPromise();
const establishConnection = async (): Promise<ImapSimple> => {
let searchCriteria: SearchCriteria[] = ['UNSEEN'];
if (options.customEmailConfig !== undefined) {
try {
searchCriteria = JSON.parse(options.customEmailConfig as string);
} catch (error) {
throw new NodeOperationError(this.getNode(), 'Custom email config is not valid JSON.');
}
}
const config: ImapSimpleOptions = {
imap: {
user: credentials.user as string,
password: credentials.password as string,
host: (credentials.host as string).trim(),
port: credentials.port as number,
tls: credentials.secure as boolean,
authTimeout: 20000,
},
onMail: async () => {
if (connection) {
if (staticData.lastMessageUid !== undefined) {
searchCriteria.push(['UID', `${staticData.lastMessageUid as number}:*`]);
/**
* A short explanation about UIDs and how they work
* can be found here: https://dev.to/kehers/imap-new-messages-since-last-check-44gm
* TL;DR:
* - You cannot filter using ['UID', 'CURRENT ID + 1:*'] because IMAP
* won't return correct results if current id + 1 does not yet exist.
* - UIDs can change but this is not being treated here.
* If the mailbox is recreated (lets say you remove all emails, remove
* the mail box and create another with same name, UIDs will change)
* - You can check if UIDs changed in the above example
* by checking UIDValidity.
*/
this.logger.debug('Querying for new messages on node "EmailReadImap"', {
searchCriteria,
});
}
try {
const returnData = await getNewEmails(connection, searchCriteria);
if (returnData.length) {
this.emit([returnData]);
}
} catch (error) {
this.logger.error('Email Read Imap node encountered an error fetching new emails', {
error,
});
// Wait with resolving till the returnedPromise got resolved, else n8n will be unhappy
// if it receives an error before the workflow got activated
await returnedPromise.promise.then(() => {
this.emitError(error as Error);
});
}
}
},
};
const tlsOptions: IDataObject = {};
if (options.allowUnauthorizedCerts === true) {
tlsOptions.rejectUnauthorized = false;
}
if (credentials.secure) {
tlsOptions.servername = (credentials.host as string).trim();
}
if (!isEmpty(tlsOptions)) {
config.imap.tlsOptions = tlsOptions;
}
// Connect to the IMAP server and open the mailbox
// that we get informed whenever a new email arrives
return await imapConnect(config).then(async (conn) => {
conn.on('error', async (error) => {
const errorCode = error.code.toUpperCase();
if (['ECONNRESET', 'EPIPE'].includes(errorCode as string)) {
this.logger.debug(`IMAP connection was reset (${errorCode}) - reconnecting.`, {
error,
});
try {
connection = await establishConnection();
await connection.openBox(mailbox);
return;
} catch (e) {
this.logger.error('IMAP reconnect did fail', { error: e });
// If something goes wrong we want to run emitError
}
} else {
this.logger.error('Email Read Imap node encountered a connection error', { error });
this.emitError(error as Error);
}
});
return conn;
});
};
connection = await establishConnection();
await connection.openBox(mailbox);
let reconnectionInterval: NodeJS.Timeout | undefined;
if (options.forceReconnect !== undefined) {
reconnectionInterval = setInterval(
async () => {
this.logger.debug('Forcing reconnection of IMAP node.');
connection.end();
connection = await establishConnection();
await connection.openBox(mailbox);
},
(options.forceReconnect as number) * 1000 * 60,
);
}
// When workflow and so node gets set to inactive close the connectoin
async function closeFunction() {
if (reconnectionInterval) {
clearInterval(reconnectionInterval);
}
connection.end();
}
// Resolve returned-promise so that waiting errors can be emitted
returnedPromise.resolve();
return {
closeFunction,
};
}
}
@@ -0,0 +1,537 @@
import type { ICredentialsDataImap } from '@credentials/Imap.credentials';
import { isCredentialsDataImap } from '@credentials/Imap.credentials';
import type {
ImapSimple,
ImapSimpleOptions,
Message,
MessagePart,
SearchCriteria,
} from '@n8n/imap';
import { connect as imapConnect } from '@n8n/imap';
import isEmpty from 'lodash/isEmpty';
import { DateTime } from 'luxon';
import type {
ITriggerFunctions,
IBinaryData,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
INodeCredentialTestResult,
INodeType,
INodeTypeBaseDescription,
INodeTypeDescription,
ITriggerResponse,
JsonObject,
INodeExecutionData,
} from 'n8n-workflow';
import { NodeConnectionTypes, NodeOperationError, TriggerCloseError } from 'n8n-workflow';
import rfc2047 from 'rfc2047';
import { getNewEmails } from './utils';
const versionDescription: INodeTypeDescription = {
displayName: 'Email Trigger (IMAP)',
name: 'emailReadImap',
icon: 'fa:inbox',
iconColor: 'green',
group: ['trigger'],
version: [2, 2.1],
description: 'Triggers the workflow when a new email is received',
eventTriggerDescription: 'Waiting for you to receive an email',
defaults: {
name: 'Email Trigger (IMAP)',
color: '#44AA22',
},
triggerPanel: {
header: '',
executionsHelp: {
inactive:
"<b>While building your workflow</b>, click the 'execute step' button, then send an email to make an event happen. This will trigger an execution, which will show up in this editor.<br /> <br /><b>Once you're happy with your workflow</b>, publish it. Then every time an email is received, the workflow will execute. These executions will show up in the <a data-key='executions'>executions list</a>, but not in the editor.",
active:
"<b>While building your workflow</b>, click the 'execute step' button, then send an email to make an event happen. This will trigger an execution, which will show up in this editor.<br /> <br /><b>Your workflow will also execute automatically</b>, since it's activated. Every time an email is received, this node will trigger an execution. These executions will show up in the <a data-key='executions'>executions list</a>, but not in the editor.",
},
activationHint:
'Once youve finished building your workflow, publish it to have it also listen continuously (you just wont see those executions here).',
},
inputs: [],
outputs: [NodeConnectionTypes.Main],
credentials: [
{
name: 'imap',
required: true,
testedBy: 'imapConnectionTest',
},
],
properties: [
{
displayName: 'Mailbox Name',
name: 'mailbox',
type: 'string',
default: 'INBOX',
},
{
displayName: 'Action',
name: 'postProcessAction',
type: 'options',
options: [
{
name: 'Mark as Read',
value: 'read',
},
{
name: 'Nothing',
value: 'nothing',
},
],
default: 'read',
description:
'What to do after the email has been received. If "nothing" gets selected it will be processed multiple times.',
},
{
displayName: 'Download Attachments',
name: 'downloadAttachments',
type: 'boolean',
default: false,
displayOptions: {
show: {
format: ['simple'],
},
},
description:
'Whether attachments of emails should be downloaded. Only set if needed as it increases processing.',
},
{
displayName: 'Format',
name: 'format',
type: 'options',
options: [
{
name: 'RAW',
value: 'raw',
description:
'Returns the full email message data with body content in the raw field as a base64url encoded string; the payload field is not used',
},
{
name: 'Resolved',
value: 'resolved',
description:
'Returns the full email with all data resolved and attachments saved as binary data',
},
{
name: 'Simple',
value: 'simple',
description:
'Returns the full email; do not use if you wish to gather inline attachments',
},
],
default: 'simple',
description: 'The format to return the message in',
},
{
displayName: 'Property Prefix Name',
name: 'dataPropertyAttachmentsPrefixName',
type: 'string',
default: 'attachment_',
displayOptions: {
show: {
format: ['resolved'],
},
},
description:
'Prefix for name of the binary property to which to write the attachments. An index starting with 0 will be added. So if name is "attachment_" the first attachment is saved to "attachment_0"',
},
{
displayName: 'Property Prefix Name',
name: 'dataPropertyAttachmentsPrefixName',
type: 'string',
default: 'attachment_',
displayOptions: {
show: {
format: ['simple'],
downloadAttachments: [true],
},
},
description:
'Prefix for name of the binary property to which to write the attachments. An index starting with 0 will be added. So if name is "attachment_" the first attachment is saved to "attachment_0"',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add option',
default: {},
options: [
{
displayName: 'Custom Email Rules',
name: 'customEmailConfig',
type: 'string',
default: '["UNSEEN"]',
description:
'Custom email fetching rules. See <a href="https://github.com/mscdex/node-imap">node-imap</a>\'s search function for more details.',
},
{
displayName: 'Force Reconnect Every Minutes',
name: 'forceReconnect',
type: 'number',
default: 60,
description: 'Sets an interval (in minutes) to force a reconnection',
},
{
displayName: 'Fetch Only New Emails',
name: 'trackLastMessageId',
type: 'boolean',
default: true,
description:
'Whether to fetch only new emails since the last run, or all emails that match the "Custom Email Rules" (["UNSEEN"] by default)',
displayOptions: {
show: {
'@version': [{ _cnd: { gte: 2.1 } }],
},
},
},
],
},
],
};
export class EmailReadImapV2 implements INodeType {
description: INodeTypeDescription;
constructor(baseDescription: INodeTypeBaseDescription) {
this.description = {
...baseDescription,
...versionDescription,
};
}
methods = {
credentialTest: {
async imapConnectionTest(
this: ICredentialTestFunctions,
credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
if (isCredentialsDataImap(credential.data)) {
const credentials = credential.data as ICredentialsDataImap;
try {
const config: ImapSimpleOptions = {
imap: {
user: credentials.user,
password: credentials.password,
host: credentials.host.trim(),
port: credentials.port,
tls: credentials.secure,
authTimeout: 20000,
},
};
const tlsOptions: IDataObject = {};
if (credentials.allowUnauthorizedCerts) {
tlsOptions.rejectUnauthorized = false;
}
if (credentials.secure) {
tlsOptions.servername = credentials.host.trim();
}
if (!isEmpty(tlsOptions)) {
config.imap.tlsOptions = tlsOptions;
}
const connection = await imapConnect(config);
await connection.getBoxes();
connection.end();
} catch (error) {
return {
status: 'Error',
message: (error as Error).message,
};
}
return {
status: 'OK',
message: 'Connection successful!',
};
} else {
return {
status: 'Error',
message: 'Credentials are no IMAP credentials.',
};
}
},
},
};
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const node = this.getNode();
const credentialsObject = await this.getCredentials('imap');
const credentials = isCredentialsDataImap(credentialsObject) ? credentialsObject : undefined;
if (!credentials) {
throw new NodeOperationError(this.getNode(), 'Credentials are not valid for imap node.');
}
const mailbox = this.getNodeParameter('mailbox') as string;
const postProcessAction = this.getNodeParameter('postProcessAction') as string;
const options = this.getNodeParameter('options', {}) as IDataObject;
const activatedAt = DateTime.now();
const staticData = this.getWorkflowStaticData('node');
if (node.typeVersion <= 2) {
// before v 2.1 staticData.lastMessageUid was never set, preserve that behavior
staticData.lastMessageUid = undefined;
}
if (options.trackLastMessageId === false) {
staticData.lastMessageUid = undefined;
}
this.logger.debug('Loaded static data for node "EmailReadImap"', { staticData });
let connection: ImapSimple;
let closeFunctionWasCalled = false;
let isCurrentlyReconnecting = false;
// Returns the email text
const getText = async (
parts: MessagePart[],
message: Message,
subtype: string,
): Promise<string> => {
if (!message.attributes.struct) {
return '';
}
const textParts = parts.filter((part) => {
return (
part.type.toUpperCase() === 'TEXT' && part.subtype.toUpperCase() === subtype.toUpperCase()
);
});
const part = textParts[0];
if (!part) {
return '';
}
try {
const partData = await connection.getPartData(message, part);
return partData.toString();
} catch {
return '';
}
};
// Returns the email attachments
const getAttachment = async (
imapConnection: ImapSimple,
parts: MessagePart[],
message: Message,
): Promise<IBinaryData[]> => {
if (!message.attributes.struct) {
return [];
}
// Check if the message has attachments and if so get them
const attachmentParts = parts.filter(
(part) => part.disposition?.type?.toUpperCase() === 'ATTACHMENT',
);
const decodeFilename = (filename: string) => {
const regex = /=\?([\w-]+)\?Q\?.*\?=/i;
if (regex.test(filename)) {
return rfc2047.decode(filename);
}
return filename;
};
const attachmentPromises = [];
let attachmentPromise;
for (const attachmentPart of attachmentParts) {
attachmentPromise = imapConnection
.getPartData(message, attachmentPart)
.then(async (partData) => {
// if filename contains utf-8 encoded characters, decode it
const fileName = decodeFilename(
((attachmentPart.disposition as IDataObject)?.params as IDataObject)
?.filename as string,
);
// Return it in the format n8n expects
return await this.helpers.prepareBinaryData(partData.buffer, fileName);
});
attachmentPromises.push(attachmentPromise);
}
return await Promise.all(attachmentPromises);
};
const returnedPromise = this.helpers.createDeferredPromise();
const establishConnection = async (): Promise<ImapSimple> => {
let searchCriteria: SearchCriteria[] = ['UNSEEN'];
if (options.customEmailConfig !== undefined) {
try {
searchCriteria = JSON.parse(options.customEmailConfig as string) as SearchCriteria[];
} catch (error) {
throw new NodeOperationError(this.getNode(), 'Custom email config is not valid JSON.');
}
}
const config: ImapSimpleOptions = {
imap: {
user: credentials.user,
password: credentials.password,
host: credentials.host.trim(),
port: credentials.port,
tls: credentials.secure,
authTimeout: 20000,
},
onMail: async (numEmails) => {
this.logger.debug('New emails received in node "EmailReadImap"', {
numEmails,
});
if (connection) {
/**
* Only process new emails:
* - If we've seen emails before (lastMessageUid is set), fetch messages higher UID.
* - Otherwise, fetch emails received since the workflow activation date.
*
* Note: IMAP 'SINCE' only filters by date (not time),
* so it may include emails from earlier on the activation day.
*/
if (staticData.lastMessageUid !== undefined) {
/**
* A short explanation about UIDs and how they work
* can be found here: https://dev.to/kehers/imap-new-messages-since-last-check-44gm
* TL;DR:
* - You cannot filter using ['UID', 'CURRENT ID + 1:*'] because IMAP
* won't return correct results if current id + 1 does not yet exist.
* - UIDs can change but this is not being treated here.
* If the mailbox is recreated (lets say you remove all emails, remove
* the mail box and create another with same name, UIDs will change)
* - You can check if UIDs changed in the above example
* by checking UIDValidity.
*/
searchCriteria.push(['UID', `${staticData.lastMessageUid as number}:*`]);
} else if (node.typeVersion > 2 && options.trackLastMessageId !== false) {
searchCriteria.push(['SINCE', activatedAt.toFormat('dd-LLL-yyyy')]);
}
this.logger.debug('Querying for new messages on node "EmailReadImap"', {
searchCriteria,
});
try {
await getNewEmails.call(this, {
imapConnection: connection,
searchCriteria,
postProcessAction,
getText,
getAttachment,
onEmailBatch: async (returnData: INodeExecutionData[]) => {
if (returnData.length) {
this.emit([returnData]);
}
},
});
} catch (error) {
this.logger.error('Email Read Imap node encountered an error fetching new emails', {
error: error as Error,
});
// Wait with resolving till the returnedPromise got resolved, else n8n will be unhappy
// if it receives an error before the workflow got activated
await returnedPromise.promise.then(() => {
this.emitError(error as Error);
});
}
}
},
onUpdate: (seqNo: number, info) => {
this.logger.debug(`Email Read Imap:update ${seqNo}`, info);
},
};
const tlsOptions: IDataObject = {};
if (credentials.allowUnauthorizedCerts) {
tlsOptions.rejectUnauthorized = false;
}
if (credentials.secure) {
tlsOptions.servername = credentials.host.trim();
}
if (!isEmpty(tlsOptions)) {
config.imap.tlsOptions = tlsOptions;
}
// Connect to the IMAP server and open the mailbox
// that we get informed whenever a new email arrives
return await imapConnect(config).then((conn) => {
conn.on('close', (_hadError: boolean) => {
if (isCurrentlyReconnecting) {
this.logger.debug('Email Read Imap: Connected closed for forced reconnecting');
} else if (closeFunctionWasCalled) {
this.logger.debug('Email Read Imap: Shutting down workflow - connected closed');
} else {
this.logger.error('Email Read Imap: Connected closed unexpectedly');
this.emitError(new Error('Imap connection closed unexpectedly'));
}
});
conn.on('error', (error) => {
const errorCode = ((error as JsonObject).code as string).toUpperCase();
this.logger.debug(`IMAP connection experienced an error: (${errorCode})`, {
error: error as Error,
});
this.emitError(error as Error);
});
return conn;
});
};
connection = await establishConnection();
await connection.openBox(mailbox);
let reconnectionInterval: NodeJS.Timeout | undefined;
const handleReconnect = async () => {
this.logger.debug('Forcing reconnect to IMAP server');
try {
isCurrentlyReconnecting = true;
if (connection.closeBox) await connection.closeBox(false);
connection.end();
connection = await establishConnection();
await connection.openBox(mailbox);
} catch (error) {
this.logger.error(error as string);
} finally {
isCurrentlyReconnecting = false;
}
};
if (options.forceReconnect !== undefined) {
reconnectionInterval = setInterval(
handleReconnect,
(options.forceReconnect as number) * 1000 * 60,
);
}
// When workflow and so node gets set to inactive close the connection
const closeFunction = async () => {
closeFunctionWasCalled = true;
if (reconnectionInterval) {
clearInterval(reconnectionInterval);
}
try {
if (connection.closeBox) await connection.closeBox(false);
connection.end();
} catch (error) {
throw new TriggerCloseError(this.getNode(), { cause: error as Error, level: 'warning' });
}
};
// Resolve returned-promise so that waiting errors can be emitted
returnedPromise.resolve();
return {
closeFunction,
};
}
}
@@ -0,0 +1,260 @@
import {
getParts,
type ImapSimple,
type Message,
type MessagePart,
type SearchCriteria,
} from '@n8n/imap';
import find from 'lodash/find';
import { simpleParser, type Source as ParserSource } from 'mailparser';
import {
type IBinaryData,
type INodeExecutionData,
type IDataObject,
type ITriggerFunctions,
NodeOperationError,
type IBinaryKeyData,
} from 'n8n-workflow';
async function parseRawEmail(
this: ITriggerFunctions,
messageEncoded: ParserSource,
dataPropertyNameDownload: string,
): Promise<INodeExecutionData> {
const responseData = await simpleParser(messageEncoded);
const headers: IDataObject = {};
const additionalData: IDataObject = {};
for (const header of responseData.headerLines) {
headers[header.key] = header.line;
}
additionalData.headers = headers;
additionalData.headerLines = undefined;
const binaryData: IBinaryKeyData = {};
if (responseData.attachments) {
for (let i = 0; i < responseData.attachments.length; i++) {
const attachment = responseData.attachments[i];
binaryData[`${dataPropertyNameDownload}${i}`] = await this.helpers.prepareBinaryData(
attachment.content,
attachment.filename,
attachment.contentType,
);
}
additionalData.attachments = undefined;
}
return {
json: { ...responseData, ...additionalData },
binary: Object.keys(binaryData).length ? binaryData : undefined,
} as INodeExecutionData;
}
const EMAIL_BATCH_SIZE = 20;
export async function getNewEmails(
this: ITriggerFunctions,
{
getAttachment,
getText,
onEmailBatch,
imapConnection,
postProcessAction,
searchCriteria,
}: {
imapConnection: ImapSimple;
searchCriteria: SearchCriteria[];
postProcessAction: string;
getText: (parts: MessagePart[], message: Message, subtype: string) => Promise<string>;
getAttachment: (
imapConnection: ImapSimple,
parts: MessagePart[],
message: Message,
) => Promise<IBinaryData[]>;
onEmailBatch: (data: INodeExecutionData[]) => Promise<void>;
},
) {
const format = this.getNodeParameter('format', 0) as string;
let fetchOptions = {};
if (format === 'simple' || format === 'raw') {
fetchOptions = {
bodies: ['TEXT', 'HEADER'],
markSeen: false,
struct: true,
};
} else if (format === 'resolved') {
fetchOptions = {
bodies: [''],
markSeen: false,
struct: true,
};
}
let results: Message[] = [];
let maxUid = 0;
const staticData = this.getWorkflowStaticData('node');
const limit = this.getNode().typeVersion >= 2.1 ? EMAIL_BATCH_SIZE : undefined;
do {
if (maxUid) {
searchCriteria = searchCriteria.filter((criteria: SearchCriteria) => {
if (Array.isArray(criteria)) {
return !['UID', 'SINCE'].includes(criteria[0]);
}
return true;
});
searchCriteria.push(['UID', `${maxUid}:*`]);
}
results = await imapConnection.search(searchCriteria, fetchOptions, limit);
this.logger.debug(`Process ${results.length} new emails in node "EmailReadImap"`);
const newEmails: INodeExecutionData[] = [];
let newEmail: INodeExecutionData;
let attachments: IBinaryData[];
let propertyName: string;
// All properties get by default moved to metadata except the ones
// which are defined here which get set on the top level.
const topLevelProperties = ['cc', 'date', 'from', 'subject', 'to'];
if (format === 'resolved') {
const dataPropertyAttachmentsPrefixName = this.getNodeParameter(
'dataPropertyAttachmentsPrefixName',
) as string;
for (const message of results) {
const lastMessageUid = this.getWorkflowStaticData('node').lastMessageUid as number;
if (lastMessageUid !== undefined && message.attributes.uid <= lastMessageUid) {
continue;
}
// Track the maximum UID to update staticData later
if (message.attributes.uid > maxUid) {
maxUid = message.attributes.uid;
}
const part = find(message.parts, { which: '' });
if (part === undefined) {
throw new NodeOperationError(this.getNode(), 'Email part could not be parsed.');
}
const parsedEmail = await parseRawEmail.call(
this,
part.body as Buffer,
dataPropertyAttachmentsPrefixName,
);
parsedEmail.json.attributes = {
uid: message.attributes.uid,
};
newEmails.push(parsedEmail);
}
} else if (format === 'simple') {
const downloadAttachments = this.getNodeParameter('downloadAttachments') as boolean;
let dataPropertyAttachmentsPrefixName = '';
if (downloadAttachments) {
dataPropertyAttachmentsPrefixName = this.getNodeParameter(
'dataPropertyAttachmentsPrefixName',
) as string;
}
for (const message of results) {
const lastMessageUid = this.getWorkflowStaticData('node').lastMessageUid as number;
if (lastMessageUid !== undefined && message.attributes.uid <= lastMessageUid) {
continue;
}
// Track the maximum UID to update staticData later
if (message.attributes.uid > maxUid) {
maxUid = message.attributes.uid;
}
const parts = getParts(message.attributes.struct as IDataObject[]);
newEmail = {
json: {
textHtml: await getText(parts, message, 'html'),
textPlain: await getText(parts, message, 'plain'),
metadata: {} as IDataObject,
attributes: {
uid: message.attributes.uid,
} as IDataObject,
},
};
const messageHeader = message.parts.filter((part) => part.which === 'HEADER');
const messageBody = messageHeader[0].body as Record<string, string[]>;
for (propertyName of Object.keys(messageBody)) {
if (messageBody[propertyName].length) {
if (topLevelProperties.includes(propertyName)) {
newEmail.json[propertyName] = messageBody[propertyName][0];
} else {
(newEmail.json.metadata as IDataObject)[propertyName] = messageBody[propertyName][0];
}
}
}
if (downloadAttachments) {
// Get attachments and add them if any get found
attachments = await getAttachment(imapConnection, parts, message);
if (attachments.length) {
newEmail.binary = {};
for (let i = 0; i < attachments.length; i++) {
newEmail.binary[`${dataPropertyAttachmentsPrefixName}${i}`] = attachments[i];
}
}
}
newEmails.push(newEmail);
}
} else if (format === 'raw') {
for (const message of results) {
const lastMessageUid = this.getWorkflowStaticData('node').lastMessageUid as number;
if (lastMessageUid !== undefined && message.attributes.uid <= lastMessageUid) {
continue;
}
// Track the maximum UID to update staticData later
if (message.attributes.uid > maxUid) {
maxUid = message.attributes.uid;
}
const part = find(message.parts, { which: 'TEXT' });
if (part === undefined) {
throw new NodeOperationError(this.getNode(), 'Email part could not be parsed.');
}
// Return base64 string
newEmail = {
json: {
raw: part.body as string,
},
};
newEmails.push(newEmail);
}
}
// only mark messages as seen once processing has finished
if (postProcessAction === 'read') {
const uidList = results.map((e) => e.attributes.uid);
if (uidList.length > 0) {
await imapConnection.addFlags(uidList, '\\SEEN');
}
}
await onEmailBatch(newEmails);
} while (results.length >= EMAIL_BATCH_SIZE);
// Update lastMessageUid after processing all messages
if (maxUid > ((staticData.lastMessageUid as number) ?? 0)) {
this.getWorkflowStaticData('node').lastMessageUid = maxUid;
}
}