Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to Email Read Imap node #2991

Merged
merged 3 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

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

35 changes: 21 additions & 14 deletions packages/nodes-base/nodes/EmailReadImap/EmailReadImap.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
INodeType,
INodeTypeDescription,
ITriggerResponse,
LoggerProxy,
NodeOperationError,
} from 'n8n-workflow';

Expand Down Expand Up @@ -377,6 +378,18 @@ export class EmailReadImap implements INodeType {
};

const establishConnection = (): Promise<ImapSimple> => {

let searchCriteria = [
'UNSEEN',
] as Array<string | string[]>;
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,
Expand All @@ -388,16 +401,6 @@ export class EmailReadImap implements INodeType {
},
onmail: async () => {
if (connection) {
let searchCriteria = [
'UNSEEN',
] as Array<string | string[]>;
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.`);
}
}
if (staticData.lastMessageUid !== undefined) {
searchCriteria.push(['UID', `${staticData.lastMessageUid as number}:*`]);
/**
Expand All @@ -415,10 +418,14 @@ export class EmailReadImap implements INodeType {
Logger.debug('Querying for new messages on node "EmailReadImap"', {searchCriteria});
}

const returnData = await getNewEmails(connection, searchCriteria);

if (returnData.length) {
this.emit([returnData]);
try {
const returnData = await getNewEmails(connection, searchCriteria);
if (returnData.length) {
this.emit([returnData]);
}
} catch (error) {
Logger.error('Email Read Imap node encountered an error fetching new emails', { error });
throw error;
}
}
},
Expand Down