From 534b852fc248de10bd961385e25f4afd3af0607d Mon Sep 17 00:00:00 2001
From: remoremorali <69923370+remoremorali@users.noreply.github.com>
Date: Fri, 18 Sep 2020 08:27:44 +0200
Subject: [PATCH] :zap: Add support for custom rules when checking emails.
(#899)
* Add support for custom rules when checking emails.
* Add support for custom rules when checking emails.
---
.../nodes-base/nodes/EmailReadImap.node.ts | 76 ++++++++++++++++---
1 file changed, 66 insertions(+), 10 deletions(-)
diff --git a/packages/nodes-base/nodes/EmailReadImap.node.ts b/packages/nodes-base/nodes/EmailReadImap.node.ts
index 920c0d41fc..1c204690b2 100644
--- a/packages/nodes-base/nodes/EmailReadImap.node.ts
+++ b/packages/nodes-base/nodes/EmailReadImap.node.ts
@@ -30,7 +30,8 @@ export class EmailReadImap implements INodeType {
color: '#44AA22',
},
inputs: [],
- outputs: ['main'],
+ outputs: ['main', 'main'],
+ outputNames: ['data', 'error'],
credentials: [
{
name: 'imap',
@@ -130,6 +131,27 @@ export class EmailReadImap implements INodeType {
},
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: 'Use custom email config',
+ name: 'useCustomEmailConfig',
+ type: 'boolean',
+ default: false,
+ description: 'If custom email rules should be used.',
+ },
+ {
+ displayName: 'Custom email rules',
+ name: 'customEmailConfig',
+ type: 'string',
+ default: "['UNSEEN']",
+ displayOptions: {
+ show: {
+ useCustomEmailConfig: [
+ true
+ ],
+ },
+ },
+ description: 'Custom email fetching rules. See node-imap\'s search function for more details'
+ },
{
displayName: 'Options',
name: 'options',
@@ -177,7 +199,11 @@ export class EmailReadImap implements INodeType {
return '';
}
- return await connection.getPartData(message, textParts[0]);
+ try{
+ return await connection.getPartData(message, textParts[0]);
+ } catch {
+ return '';
+ }
};
@@ -211,10 +237,20 @@ export class EmailReadImap implements INodeType {
// Returns all the new unseen messages
const getNewEmails = async (connection: ImapSimple): Promise => {
const format = this.getNodeParameter('format', 0) as string;
- const searchCriteria = [
+
+ let searchCriteria = [
'UNSEEN'
];
-
+ const useCustomEmailConfig = this.getNodeParameter('useCustomEmailConfig') as boolean;
+ if (useCustomEmailConfig) {
+ const customEmailConfig = this.getNodeParameter('customEmailConfig') as string;
+ try {
+ searchCriteria = eval(customEmailConfig);
+ } catch (err) {
+ throw new Error(`Parsing of ${customEmailConfig}\nfailed with error ${err}`);
+ }
+ }
+
let fetchOptions = {};
if (format === 'simple' || format === 'raw') {
@@ -334,6 +370,19 @@ export class EmailReadImap implements INodeType {
let connection: ImapSimple;
+ let empty: INodeExecutionData[] = [];
+ let errToJson = (err: Error) => {
+ return {
+ json:
+ {
+ message: err.message,
+ stack: err.stack
+ }
+ };
+ }
+ let emitError = (err: Error) => {
+ this.emit([empty, [errToJson(err)]]);
+ }
const config: ImapSimpleOptions = {
imap: {
@@ -345,10 +394,14 @@ export class EmailReadImap implements INodeType {
authTimeout: 3000
},
onmail: async () => {
- const returnData = await getNewEmails(connection);
+ try{
+ const returnData = await getNewEmails(connection);
- if (returnData.length) {
- this.emit([returnData]);
+ if (returnData.length) {
+ this.emit([returnData, empty]);
+ }
+ }catch(e) {
+ emitError(e);
}
},
};
@@ -361,9 +414,12 @@ export class EmailReadImap implements INodeType {
// Connect to the IMAP server and open the mailbox
// that we get informed whenever a new email arrives
- connection = await imapConnect(config);
- await connection.openBox(mailbox);
-
+ try {
+ connection = await imapConnect(config);
+ await connection.openBox(mailbox);
+ } catch (e) {
+ emitError(e);
+ }
// When workflow and so node gets set to inactive close the connectoin
async function closeFunction() {