Skip to content

Commit

Permalink
fix(Email Trigger (IMAP) Node): UTF-8 attachments are not correctly n…
Browse files Browse the repository at this point in the history
…amed (#6856)
  • Loading branch information
michael-radency authored and netroy committed Aug 17, 2023
1 parent 1de71c6 commit f3f1c14
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 44 deletions.
114 changes: 70 additions & 44 deletions packages/nodes-base/nodes/EmailReadImap/v2/EmailReadImapV2.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import type {
INodeTypeBaseDescription,
INodeTypeDescription,
ITriggerResponse,
JsonObject,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';

import type { ImapSimple, ImapSimpleOptions, Message } from 'imap-simple';
import { connect as imapConnect, getParts } from 'imap-simple';
import type { Source as ParserSource } from 'mailparser';
import { simpleParser } from 'mailparser';

import rfc2047 from 'rfc2047';
import isEmpty from 'lodash/isEmpty';
import find from 'lodash/find';

import type { ICredentialsDataImap } from '../../../credentials/Imap.credentials';
import { isCredentialsDataImap } from '../../../credentials/Imap.credentials';

Expand All @@ -32,14 +34,14 @@ export async function parseRawEmail(
): Promise<INodeExecutionData> {
const responseData = await simpleParser(messageEncoded);
const headers: IDataObject = {};
const addidtionalData: IDataObject = {};

for (const header of responseData.headerLines) {
headers[header.key] = header.line;
}

// @ts-ignore
responseData.headers = headers;
// @ts-ignore
responseData.headerLines = undefined;
addidtionalData.headers = headers;
addidtionalData.headerLines = undefined;

const binaryData: IBinaryKeyData = {};
if (responseData.attachments) {
Expand All @@ -51,12 +53,12 @@ export async function parseRawEmail(
attachment.contentType,
);
}
// @ts-ignore
responseData.attachments = undefined;

addidtionalData.attachments = undefined;
}

return {
json: responseData as unknown as IDataObject,
json: { ...responseData, ...addidtionalData },
binary: Object.keys(binaryData).length ? binaryData : undefined,
} as INodeExecutionData;
}
Expand Down Expand Up @@ -260,7 +262,7 @@ export class EmailReadImapV2 implements INodeType {
} catch (error) {
return {
status: 'Error',
message: error.message,
message: (error as Error).message,
};
}
return {
Expand Down Expand Up @@ -296,14 +298,15 @@ export class EmailReadImapV2 implements INodeType {

// Returns the email text

const getText = async (parts: any[], message: Message, subtype: string) => {
const getText = async (parts: IDataObject[], message: Message, subtype: string) => {
if (!message.attributes.struct) {
return '';
}

const textParts = parts.filter((part) => {
return (
part.type.toUpperCase() === 'TEXT' && part.subtype.toUpperCase() === subtype.toUpperCase()
(part.type as string).toUpperCase() === 'TEXT' &&
(part.subtype as string).toUpperCase() === subtype.toUpperCase()
);
});

Expand All @@ -312,7 +315,7 @@ export class EmailReadImapV2 implements INodeType {
}

try {
return await connection.getPartData(message, textParts[0]);
return (await connection.getPartData(message, textParts[0])) as string;
} catch {
return '';
}
Expand All @@ -321,7 +324,7 @@ export class EmailReadImapV2 implements INodeType {
// Returns the email attachments
const getAttachment = async (
imapConnection: ImapSimple,
parts: any[],
parts: IDataObject[],
message: Message,
): Promise<IBinaryData[]> => {
if (!message.attributes.struct) {
Expand All @@ -330,20 +333,33 @@ export class EmailReadImapV2 implements INodeType {

// Check if the message has attachments and if so get them
const attachmentParts = parts.filter((part) => {
return part.disposition && part.disposition.type.toUpperCase() === 'ATTACHMENT';
return (
part.disposition &&
((part.disposition as IDataObject)?.type as string).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) => {
// Return it in the format n8n expects
return this.helpers.prepareBinaryData(
partData as Buffer,
attachmentPart.disposition.params.filename as string,
// 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 this.helpers.prepareBinaryData(partData as Buffer, fileName);
});

attachmentPromises.push(attachmentPromise);
Expand Down Expand Up @@ -440,7 +456,7 @@ export class EmailReadImapV2 implements INodeType {
) {
staticData.lastMessageUid = message.attributes.uid;
}
const parts = getParts(message.attributes.struct!);
const parts = getParts(message.attributes.struct as IDataObject[]) as IDataObject[];

newEmail = {
json: {
Expand All @@ -454,14 +470,15 @@ export class EmailReadImapV2 implements INodeType {
return part.which === 'HEADER';
});

messageBody = messageHeader[0].body;
for (propertyName of Object.keys(messageBody as IDataObject)) {
if (messageBody[propertyName].length) {
messageBody = messageHeader[0].body as IDataObject;
for (propertyName of Object.keys(messageBody)) {
if ((messageBody[propertyName] as IDataObject[]).length) {
if (topLevelProperties.includes(propertyName)) {
newEmail.json[propertyName] = messageBody[propertyName][0];
newEmail.json[propertyName] = (messageBody[propertyName] as string[])[0];
} else {
(newEmail.json.metadata as IDataObject)[propertyName] =
messageBody[propertyName][0];
(newEmail.json.metadata as IDataObject)[propertyName] = (
messageBody[propertyName] as string[]
)[0];
}
}
}
Expand Down Expand Up @@ -501,7 +518,7 @@ export class EmailReadImapV2 implements INodeType {
// Return base64 string
newEmail = {
json: {
raw: part.body,
raw: part.body as string,
},
};

Expand All @@ -525,7 +542,9 @@ export class EmailReadImapV2 implements INodeType {
let searchCriteria = ['UNSEEN'] as Array<string | string[]>;
if (options.customEmailConfig !== undefined) {
try {
searchCriteria = JSON.parse(options.customEmailConfig as string);
searchCriteria = JSON.parse(options.customEmailConfig as string) as Array<
string | string[]
>;
} catch (error) {
throw new NodeOperationError(this.getNode(), 'Custom email config is not valid JSON.');
}
Expand Down Expand Up @@ -568,7 +587,7 @@ export class EmailReadImapV2 implements INodeType {
}
} catch (error) {
this.logger.error('Email Read Imap node encountered an error fetching new emails', {
error,
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
Expand Down Expand Up @@ -611,8 +630,10 @@ export class EmailReadImapV2 implements INodeType {
}
});
conn.on('error', async (error) => {
const errorCode = error.code.toUpperCase();
this.logger.verbose(`IMAP connection experienced an error: (${errorCode})`, { error });
const errorCode = ((error as JsonObject).code as string).toUpperCase();
this.logger.verbose(`IMAP connection experienced an error: (${errorCode})`, {
error: error as Error,
});
// eslint-disable-next-line @typescript-eslint/no-use-before-define
await closeFunction();
this.emitError(error as Error);
Expand All @@ -627,21 +648,26 @@ export class EmailReadImapV2 implements INodeType {

let reconnectionInterval: NodeJS.Timeout | undefined;

const handleReconect = async () => {
this.logger.verbose('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(async () => {
this.logger.verbose('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;
}
}, (options.forceReconnect as number) * 1000 * 60);
reconnectionInterval = setInterval(
handleReconect,
(options.forceReconnect as number) * 1000 * 60,
);
}

// When workflow and so node gets set to inactive close the connectoin
Expand Down
2 changes: 2 additions & 0 deletions packages/nodes-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@
"@types/promise-ftp": "^1.3.4",
"@types/redis": "^2.8.11",
"@types/request-promise-native": "~1.0.15",
"@types/rfc2047": "^2.0.1",
"@types/showdown": "^1.9.4",
"@types/snowflake-sdk": "^1.6.12",
"@types/ssh2-sftp-client": "^5.1.0",
Expand Down Expand Up @@ -834,6 +835,7 @@
"pyodide": "^0.22.1",
"redis": "^3.1.1",
"request": "^2.88.2",
"rfc2047": "^4.0.1",
"rhea": "^1.0.11",
"rss-parser": "^3.7.0",
"semver": "^7.5.4",
Expand Down
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f3f1c14

Please sign in to comment.