n8n/packages/nodes-base/nodes/EmailReadImap.node.ts
Iván Ovejero 1d27a9e87e
Improve node error handling (#1309)
* Add path mapping and response error interfaces

* Add error handling and throwing functionality

* Refactor error handling into a single function

* Re-implement error handling in Hacker News node

* Fix linting details

* Re-implement error handling in Spotify node

* Re-implement error handling in G Suite Admin node

* 🚧 create basic setup NodeError

* 🚧 add httpCodes

* 🚧 add path priolist

* 🚧 handle statusCode in error, adjust interfaces

* 🚧 fixing type issues w/Ivan

* 🚧 add error exploration

* 👔 fix linter issues

* 🔧 improve object check

* 🚧 remove path passing from NodeApiError

* 🚧 add multi error + refactor findProperty method

* 👔 allow any

* 🔧 handle multi error message callback

*  change return type of callback

*  add customCallback to MultiError

* 🚧 refactor to use INode

* 🔨 handle arrays, continue search after first null property found

* 🚫 refactor method access

* 🚧 setup NodeErrorView

*  change timestamp to Date.now

* 📚 Add documentation for methods and constants

* 🚧 change message setting

* 🚚 move NodeErrors to workflow

*  add new ErrorView for Nodes

* 🎨 improve error notification

* 🎨 refactor interfaces

*  add WorkflowOperationError, refactor error throwing

* 👕 fix linter issues

* 🎨 rename param

* 🐛 fix handling normal errors

*  add usage of NodeApiError

* 🎨 fix throw new error instead of constructor

* 🎨 remove unnecessary code/comments

* 🎨 adjusted spacing + updated status messages

* 🎨 fix tab indentation

*  Replace current errors with custom errors (#1576)

*  Introduce NodeApiError in catch blocks

*  Introduce NodeOperationError in nodes

*  Add missing errors and remove incompatible

*  Fix NodeOperationError in incompatible nodes

* 🔧 Adjust error handling in missed nodes

PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes

* 🔨 Adjust Strava Trigger node error handling

* 🔨 Adjust AWS nodes error handling

* 🔨 Remove duplicate instantiation of NodeApiError

* 🐛 fix strava trigger node error handling

* Add XML parsing to NodeApiError constructor (#1633)

* 🐛 Remove type annotation from catch variable

*  Add XML parsing to NodeApiError

*  Simplify error handling in Rekognition node

*  Pass in XML flag in generic functions

* 🔥 Remove try/catch wrappers at call sites

* 🔨 Refactor setting description from XML

* 🔨 Refactor let to const in resource loaders

*  Find property in parsed XML

*  Change let to const

* 🔥 Remove unneeded try/catch block

* 👕 Fix linting issues

* 🐛 Fix errors from merge conflict resolution

*  Add custom errors to latest contributions

* 👕 Fix linting issues

*  Refactor MongoDB helpers for custom errors

* 🐛 Correct custom error type

*  Apply feedback to A nodes

*  Apply feedback to missed A node

*  Apply feedback to B-D nodes

*  Apply feedback to E-F nodes

*  Apply feedback to G nodes

*  Apply feedback to H-L nodes

*  Apply feedback to M nodes

*  Apply feedback to P nodes

*  Apply feedback to R nodes

*  Apply feedback to S nodes

*  Apply feedback to T nodes

*  Apply feedback to V-Z nodes

*  Add HTTP code to iterable node error

* 🔨 Standardize e as error

* 🔨 Standardize err as error

*  Fix error handling for non-standard nodes

Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>

Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>
Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
2021-04-16 18:33:36 +02:00

428 lines
12 KiB
TypeScript

import { ITriggerFunctions } from 'n8n-core';
import {
IBinaryData,
IBinaryKeyData,
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
ITriggerResponse,
NodeOperationError,
} from 'n8n-workflow';
import {
connect as imapConnect,
getParts,
ImapSimple,
ImapSimpleOptions,
Message,
} from 'imap-simple';
import {
simpleParser,
Source as ParserSource,
} from 'mailparser';
import * as lodash from 'lodash';
export class EmailReadImap implements INodeType {
description: INodeTypeDescription = {
displayName: 'EmailReadImap',
name: 'emailReadImap',
icon: 'fa:inbox',
group: ['trigger'],
version: 1,
description: 'Triggers the workflow when a new email gets received',
defaults: {
name: 'IMAP Email',
color: '#44AA22',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'imap',
required: true,
},
],
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<br />selected it will be processed multiple times.',
},
{
displayName: 'Download Attachments',
name: 'downloadAttachments',
type: 'boolean',
default: false,
displayOptions: {
show: {
format: [
'simple',
],
},
},
description: 'If attachments of emails should be downloaded.<br />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<br />write the attachments. An index starting with 0 will be added.<br />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<br />write the attachments. An index starting with 0 will be added.<br />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',
name: 'allowUnauthorizedCerts',
type: 'boolean',
default: false,
description: 'Do connect even if SSL certificate validation is not possible.',
},
],
},
],
};
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const credentials = this.getCredentials('imap');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}
const mailbox = this.getNodeParameter('mailbox') as string;
const postProcessAction = this.getNodeParameter('postProcessAction') as string;
const options = this.getNodeParameter('options', {}) as IDataObject;
let 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.`);
}
}
// Returns the email text
const getText = async (parts: any[], message: Message, subtype: string) => { // tslint:disable-line:no-any
if (!message.attributes.struct) {
return '';
}
const textParts = parts.filter((part) => {
return part.type.toUpperCase() === 'TEXT' && part.subtype.toUpperCase() === subtype.toUpperCase();
});
if (textParts.length === 0) {
return '';
}
try{
return await connection.getPartData(message, textParts[0]);
} catch {
return '';
}
};
// Returns the email attachments
const getAttachment = async (connection: ImapSimple, parts: any[], message: Message): Promise<IBinaryData[]> => { // tslint:disable-line:no-any
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 = connection.getPartData(message, attachmentPart)
.then((partData) => {
// Return it in the format n8n expects
return this.helpers.prepareBinaryData(partData, attachmentPart.disposition.params.filename);
});
attachmentPromises.push(attachmentPromise);
}
return Promise.all(attachmentPromises);
};
// Returns all the new unseen messages
const getNewEmails = async (connection: ImapSimple, searchCriteria: string[]): Promise<INodeExecutionData[]> => {
const format = this.getNodeParameter('format', 0) as string;
let fetchOptions = {};
if (format === 'simple' || format === 'raw') {
fetchOptions = {
bodies: ['TEXT', 'HEADER'],
markSeen: postProcessAction === 'read',
struct: true,
};
} else if (format === 'resolved') {
fetchOptions = {
bodies: [''],
markSeen: postProcessAction === 'read',
struct: true,
};
}
const results = await connection.search(searchCriteria, fetchOptions);
const newEmails: INodeExecutionData[] = [];
let newEmail: INodeExecutionData, messageHeader, messageBody;
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 part = lodash.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, dataPropertyAttachmentsPrefixName);
newEmails.push(parsedEmail);
}
} else if (format === 'simple') {
const downloadAttachments = this.getNodeParameter('downloadAttachments') as boolean;
let dataPropertyAttachmentsPrefixName = '';
if (downloadAttachments === true) {
dataPropertyAttachmentsPrefixName = this.getNodeParameter('dataPropertyAttachmentsPrefixName') as string;
}
for (const message of results) {
const parts = getParts(message.attributes.struct!);
newEmail = {
json: {
textHtml: await getText(parts, message, 'html'),
textPlain: await getText(parts, message, 'plain'),
metadata: {} as IDataObject,
},
};
messageHeader = message.parts.filter((part) => {
return part.which === 'HEADER';
});
messageBody = messageHeader[0].body;
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 === true) {
// Get attachments and add them if any get found
attachments = await getAttachment(connection, 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 part = lodash.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);
}
}
return newEmails;
};
let connection: ImapSimple;
const config: ImapSimpleOptions = {
imap: {
user: credentials.user as string,
password: credentials.password as string,
host: credentials.host as string,
port: credentials.port as number,
tls: credentials.secure as boolean,
authTimeout: 20000,
},
onmail: async () => {
const returnData = await getNewEmails(connection, searchCriteria);
if (returnData.length) {
this.emit([returnData]);
}
},
};
if (options.allowUnauthorizedCerts === true) {
config.imap.tlsOptions = {
rejectUnauthorized: false,
};
}
// 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);
// When workflow and so node gets set to inactive close the connectoin
async function closeFunction() {
await connection.end();
}
return {
closeFunction,
};
}
}
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;
}