2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2020-02-13 07:43:59 -08:00
|
|
|
IDataObject,
|
2021-05-07 16:46:50 -07:00
|
|
|
ILoadOptionsFunctions,
|
2020-02-13 07:43:59 -08:00
|
|
|
INodeExecutionData,
|
2021-05-07 16:46:50 -07:00
|
|
|
INodePropertyOptions,
|
2020-02-13 07:43:59 -08:00
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2020-02-13 07:43:59 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2021-05-07 16:46:50 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IMessage } from './GenericFunctions';
|
|
|
|
import { mailjetApiRequest, validateJSON } from './GenericFunctions';
|
2021-05-07 16:46:50 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { emailFields, emailOperations } from './EmailDescription';
|
2021-05-07 16:46:50 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { smsFields, smsOperations } from './SmsDescription';
|
2020-02-13 07:43:59 -08:00
|
|
|
export class Mailjet implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Mailjet',
|
|
|
|
name: 'mailjet',
|
2021-05-07 16:46:50 -07:00
|
|
|
icon: 'file:mailjet.svg',
|
2020-02-13 07:43:59 -08:00
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Mailjet API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Mailjet',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'mailjetEmailApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['email'],
|
2020-02-13 07:43:59 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'mailjetSmsApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['sms'],
|
2020-02-13 07:43:59 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-15 11:39:54 -07:00
|
|
|
noDataExpression: true,
|
2020-02-13 07:43:59 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Email',
|
|
|
|
value: 'email',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SMS',
|
|
|
|
value: 'sms',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'email',
|
|
|
|
},
|
|
|
|
...emailOperations,
|
|
|
|
...emailFields,
|
|
|
|
...smsOperations,
|
|
|
|
...smsFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-05-07 16:46:50 -07:00
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the available custom fields to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getTemplates(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const { Data: templates } = await mailjetApiRequest.call(this, 'GET', '/v3/REST/template');
|
|
|
|
for (const template of templates) {
|
|
|
|
returnData.push({
|
|
|
|
name: template.Name,
|
|
|
|
value: template.ID,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-02-13 07:43:59 -08:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-02-13 07:43:59 -08:00
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-02-13 07:43:59 -08:00
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'email') {
|
|
|
|
//https://dev.mailjet.com/email/guides/send-api-v31/#send-a-basic-email
|
|
|
|
if (operation === 'send') {
|
|
|
|
const fromEmail = this.getNodeParameter('fromEmail', i) as string;
|
|
|
|
const htmlBody = this.getNodeParameter('html', i) as string;
|
|
|
|
const textBody = this.getNodeParameter('text', i) as string;
|
|
|
|
const subject = this.getNodeParameter('subject', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-12-02 12:54:28 -08:00
|
|
|
const toEmail = (this.getNodeParameter('toEmail', i) as string).split(',');
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i);
|
2020-02-13 07:43:59 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IMessage = {
|
|
|
|
From: {
|
|
|
|
Email: fromEmail,
|
|
|
|
},
|
|
|
|
Subject: subject,
|
|
|
|
To: [],
|
|
|
|
Cc: [],
|
|
|
|
Bcc: [],
|
|
|
|
Variables: {},
|
|
|
|
};
|
2021-05-07 16:46:50 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const email of toEmail) {
|
|
|
|
body.To?.push({
|
2021-05-07 16:46:50 -07:00
|
|
|
Email: email,
|
2020-02-13 07:43:59 -08:00
|
|
|
});
|
|
|
|
}
|
2022-03-20 12:13:18 -07:00
|
|
|
|
|
|
|
if (jsonParameters) {
|
|
|
|
const variablesJson = this.getNodeParameter('variablesJson', i) as string;
|
|
|
|
const parsedJson = validateJSON(variablesJson);
|
|
|
|
if (parsedJson === undefined) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
2022-12-29 03:20:43 -08:00
|
|
|
"Parameter 'Variables (JSON)' has a invalid JSON",
|
2022-08-17 08:50:24 -07:00
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2022-03-20 12:13:18 -07:00
|
|
|
}
|
|
|
|
body.Variables = parsedJson;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const variables =
|
|
|
|
((this.getNodeParameter('variablesUi', i) as IDataObject)
|
|
|
|
.variablesValues as IDataObject[]) || [];
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const variable of variables) {
|
|
|
|
body.Variables![variable.name as string] = variable.value;
|
|
|
|
}
|
2020-02-13 07:43:59 -08:00
|
|
|
}
|
2022-03-20 12:13:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (htmlBody) {
|
|
|
|
body.HTMLPart = htmlBody;
|
|
|
|
}
|
|
|
|
if (textBody) {
|
|
|
|
body.TextPart = textBody;
|
|
|
|
}
|
|
|
|
if (additionalFields.bccEmail) {
|
2022-12-02 12:54:28 -08:00
|
|
|
const bccEmail = (additionalFields.bccEmail as string).split(',');
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const email of bccEmail) {
|
|
|
|
body.Bcc!.push({
|
|
|
|
Email: email,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (additionalFields.ccAddresses) {
|
2022-12-02 12:54:28 -08:00
|
|
|
const ccEmail = (additionalFields.ccAddresses as string).split(',');
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const email of ccEmail) {
|
|
|
|
body.Cc!.push({
|
|
|
|
Email: email,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (additionalFields.trackOpens) {
|
|
|
|
body.TrackOpens = additionalFields.trackOpens as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.replyTo) {
|
|
|
|
const replyTo = additionalFields.replyTo as string;
|
2022-12-02 12:54:28 -08:00
|
|
|
body.ReplyTo = {
|
2021-07-19 23:58:54 -07:00
|
|
|
Email: replyTo,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (additionalFields.trackClicks) {
|
|
|
|
body.TrackClicks = additionalFields.trackClicks as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.fromName) {
|
|
|
|
body.From!.Name = additionalFields.fromName as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.templateLanguage) {
|
|
|
|
body.TemplateLanguage = additionalFields.templateLanguage as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.priority) {
|
|
|
|
body.Priority = additionalFields.priority as number;
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await mailjetApiRequest.call(this, 'POST', '/v3.1/send', {
|
|
|
|
Messages: [body],
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.Messages;
|
|
|
|
}
|
|
|
|
//https://dev.mailjet.com/email/guides/send-api-v31/#use-a-template
|
|
|
|
if (operation === 'sendTemplate') {
|
|
|
|
const fromEmail = this.getNodeParameter('fromEmail', i) as string;
|
|
|
|
const templateId = parseInt(this.getNodeParameter('templateId', i) as string, 10);
|
|
|
|
const subject = this.getNodeParameter('subject', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-12-02 12:54:28 -08:00
|
|
|
const toEmail = (this.getNodeParameter('toEmail', i) as string).split(',');
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i);
|
2020-02-13 07:43:59 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IMessage = {
|
|
|
|
From: {
|
|
|
|
Email: fromEmail,
|
|
|
|
},
|
|
|
|
Subject: subject,
|
|
|
|
To: [],
|
|
|
|
Cc: [],
|
|
|
|
Bcc: [],
|
|
|
|
Variables: {},
|
|
|
|
TemplateID: templateId,
|
|
|
|
};
|
2021-05-07 16:46:50 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const email of toEmail) {
|
|
|
|
body.To!.push({
|
2021-05-07 16:46:50 -07:00
|
|
|
Email: email,
|
2020-02-13 07:43:59 -08:00
|
|
|
});
|
|
|
|
}
|
2022-03-20 12:13:18 -07:00
|
|
|
|
|
|
|
if (jsonParameters) {
|
|
|
|
const variablesJson = this.getNodeParameter('variablesJson', i) as string;
|
|
|
|
const parsedJson = validateJSON(variablesJson);
|
|
|
|
if (parsedJson === undefined) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
2022-12-29 03:20:43 -08:00
|
|
|
"Parameter 'Variables (JSON)' has a invalid JSON",
|
2022-08-17 08:50:24 -07:00
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2022-03-20 12:13:18 -07:00
|
|
|
}
|
|
|
|
body.Variables = parsedJson;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const variables =
|
|
|
|
((this.getNodeParameter('variablesUi', i) as IDataObject)
|
|
|
|
.variablesValues as IDataObject[]) || [];
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const variable of variables) {
|
|
|
|
body.Variables![variable.name as string] = variable.value;
|
|
|
|
}
|
|
|
|
}
|
2022-05-15 11:39:54 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.bccEmail) {
|
2022-12-02 12:54:28 -08:00
|
|
|
const bccEmail = (additionalFields.bccEmail as string).split(',');
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const email of bccEmail) {
|
|
|
|
body.Bcc!.push({
|
|
|
|
Email: email,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (additionalFields.ccEmail) {
|
2022-12-02 12:54:28 -08:00
|
|
|
const ccEmail = (additionalFields.ccEmail as string).split(',');
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const email of ccEmail) {
|
|
|
|
body.Cc!.push({
|
|
|
|
Email: email,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (additionalFields.replyTo) {
|
|
|
|
const replyTo = additionalFields.replyTo as string;
|
2022-12-02 12:54:28 -08:00
|
|
|
body.ReplyTo = {
|
2021-07-19 23:58:54 -07:00
|
|
|
Email: replyTo,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (additionalFields.trackOpens) {
|
|
|
|
body.TrackOpens = additionalFields.trackOpens as string;
|
2020-02-13 07:43:59 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.trackClicks) {
|
|
|
|
body.TrackClicks = additionalFields.trackClicks as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.fromName) {
|
|
|
|
body.From!.Name = additionalFields.fromName as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.templateLanguage) {
|
|
|
|
body.TemplateLanguage = additionalFields.templateLanguage as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.priority) {
|
|
|
|
body.Priority = additionalFields.priority as number;
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await mailjetApiRequest.call(this, 'POST', '/v3.1/send', {
|
|
|
|
Messages: [body],
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.Messages;
|
2020-02-13 07:43:59 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (resource === 'sms') {
|
|
|
|
//https://dev.mailjet.com/sms/reference/send-message#v4_post_sms-send
|
|
|
|
if (operation === 'send') {
|
|
|
|
const from = this.getNodeParameter('from', i) as string;
|
|
|
|
const to = this.getNodeParameter('to', i) as boolean;
|
|
|
|
const text = this.getNodeParameter('text', i) as string;
|
|
|
|
const body: IDataObject = {
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Text: text,
|
2021-05-07 16:46:50 -07:00
|
|
|
};
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await mailjetApiRequest.call(this, 'POST', '/v4/sms-send', body);
|
2021-05-07 16:46:50 -07:00
|
|
|
}
|
2020-02-13 07:43:59 -08:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-02-13 07:43:59 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
return this.prepareOutputData(returnData);
|
2020-02-13 07:43:59 -08:00
|
|
|
}
|
|
|
|
}
|