2022-01-03 13:42:42 -08:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2022-11-08 06:28:21 -08:00
|
|
|
import { IDataObject, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
import { createTransport } from 'nodemailer';
|
2022-04-08 14:32:08 -07:00
|
|
|
import SMTPTransport from 'nodemailer/lib/smtp-transport';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
export class EmailSend implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Send Email',
|
|
|
|
name: 'emailSend',
|
|
|
|
icon: 'fa:envelope',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Sends an Email',
|
|
|
|
defaults: {
|
|
|
|
name: 'Send Email',
|
2019-07-26 02:41:08 -07:00
|
|
|
color: '#00bb88',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'smtp',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
properties: [
|
2020-02-24 08:21:03 -08:00
|
|
|
// TODO: Add choice for text as text or html (maybe also from name)
|
2019-06-23 03:35:23 -07:00
|
|
|
{
|
|
|
|
displayName: 'From Email',
|
|
|
|
name: 'fromEmail',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
required: true,
|
|
|
|
placeholder: 'admin@example.com',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Email address of the sender optional with name',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'To Email',
|
|
|
|
name: 'toEmail',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
required: true,
|
|
|
|
placeholder: 'info@example.com',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Email address of the recipient',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2019-10-08 05:40:47 -07:00
|
|
|
{
|
|
|
|
displayName: 'CC Email',
|
|
|
|
name: 'ccEmail',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: 'cc@example.com',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Email address of CC recipient',
|
2019-10-08 05:40:47 -07:00
|
|
|
},
|
2020-02-24 08:21:03 -08:00
|
|
|
{
|
|
|
|
displayName: 'BCC Email',
|
|
|
|
name: 'bccEmail',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: 'bcc@example.com',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Email address of BCC recipient',
|
2020-02-24 08:21:03 -08:00
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
{
|
|
|
|
displayName: 'Subject',
|
|
|
|
name: 'subject',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: 'My subject line',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Subject line of the email',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Text',
|
|
|
|
name: 'text',
|
|
|
|
type: 'string',
|
|
|
|
typeOptions: {
|
|
|
|
alwaysOpenEditWindow: true,
|
|
|
|
rows: 5,
|
|
|
|
},
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Plain text message of email',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'HTML',
|
|
|
|
name: 'html',
|
|
|
|
type: 'string',
|
|
|
|
typeOptions: {
|
|
|
|
rows: 5,
|
|
|
|
},
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'HTML text message of email',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Attachments',
|
|
|
|
name: 'attachments',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2022-08-01 13:47:55 -07:00
|
|
|
description:
|
|
|
|
'Name of the binary properties that contain data to add to email as attachment. Multiple ones can be comma-separated.',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2019-10-25 12:38:54 -07:00
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Ignore SSL Issues',
|
|
|
|
name: 'allowUnauthorizedCerts',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Whether to connect even if SSL certificate validation is not possible',
|
2019-10-25 12:38:54 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-03-29 02:20:10 -07:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
2021-02-15 22:23:37 -08:00
|
|
|
const items = this.getInputData();
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-02-15 22:23:37 -08:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-02-15 22:23:37 -08:00
|
|
|
let item: INodeExecutionData;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-02-15 22:23:37 -08:00
|
|
|
for (let itemIndex = 0; itemIndex < length; itemIndex++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
item = items[itemIndex];
|
2021-02-15 23:09:10 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const fromEmail = this.getNodeParameter('fromEmail', itemIndex) as string;
|
|
|
|
const toEmail = this.getNodeParameter('toEmail', itemIndex) as string;
|
|
|
|
const ccEmail = this.getNodeParameter('ccEmail', itemIndex) as string;
|
|
|
|
const bccEmail = this.getNodeParameter('bccEmail', itemIndex) as string;
|
|
|
|
const subject = this.getNodeParameter('subject', itemIndex) as string;
|
|
|
|
const text = this.getNodeParameter('text', itemIndex) as string;
|
|
|
|
const html = this.getNodeParameter('html', itemIndex) as string;
|
|
|
|
const attachmentPropertyString = this.getNodeParameter('attachments', itemIndex) as string;
|
|
|
|
const options = this.getNodeParameter('options', itemIndex, {}) as IDataObject;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('smtp');
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const connectionOptions: SMTPTransport.Options = {
|
|
|
|
host: credentials.host as string,
|
|
|
|
port: credentials.port as number,
|
|
|
|
secure: credentials.secure as boolean,
|
2021-02-15 22:23:37 -08:00
|
|
|
};
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (credentials.user || credentials.password) {
|
|
|
|
// @ts-ignore
|
|
|
|
connectionOptions.auth = {
|
|
|
|
user: credentials.user as string,
|
|
|
|
pass: credentials.password as string,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.allowUnauthorizedCerts === true) {
|
|
|
|
connectionOptions.tls = {
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const transporter = createTransport(connectionOptions);
|
|
|
|
|
|
|
|
// setup email data with unicode symbols
|
|
|
|
const mailOptions = {
|
|
|
|
from: fromEmail,
|
|
|
|
to: toEmail,
|
|
|
|
cc: ccEmail,
|
|
|
|
bcc: bccEmail,
|
|
|
|
subject,
|
|
|
|
text,
|
|
|
|
html,
|
2021-02-15 22:23:37 -08:00
|
|
|
};
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (attachmentPropertyString && item.binary) {
|
|
|
|
const attachments = [];
|
2022-08-01 13:47:55 -07:00
|
|
|
const attachmentProperties: string[] = attachmentPropertyString
|
|
|
|
.split(',')
|
|
|
|
.map((propertyName) => {
|
|
|
|
return propertyName.trim();
|
|
|
|
});
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const propertyName of attachmentProperties) {
|
|
|
|
if (!item.binary.hasOwnProperty(propertyName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
attachments.push({
|
|
|
|
filename: item.binary[propertyName].fileName || 'unknown',
|
2022-01-03 13:42:42 -08:00
|
|
|
content: await this.helpers.getBinaryDataBuffer(itemIndex, propertyName),
|
2021-07-19 23:58:54 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachments.length) {
|
|
|
|
// @ts-ignore
|
|
|
|
mailOptions.attachments = attachments;
|
|
|
|
}
|
2021-02-15 22:23:37 -08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Send the email
|
|
|
|
const info = await transporter.sendMail(mailOptions);
|
|
|
|
|
2022-06-03 08:25:07 -07:00
|
|
|
returnData.push({
|
|
|
|
json: info as unknown as IDataObject,
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2021-07-19 23:58:54 -07:00
|
|
|
if (this.continueOnFail()) {
|
2022-06-03 08:25:07 -07:00
|
|
|
returnData.push({
|
|
|
|
json: {
|
|
|
|
error: error.message,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-02-15 22:23:37 -08:00
|
|
|
}
|
2021-03-29 02:20:10 -07:00
|
|
|
|
2021-02-15 22:23:37 -08:00
|
|
|
return this.prepareOutputData(returnData);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|