first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"node": "n8n-nodes-base.emailSend",
|
||||
"nodeVersion": "1.0",
|
||||
"codexVersion": "1.0",
|
||||
"categories": ["Communication", "HITL", "Core Nodes"],
|
||||
"subcategories": {
|
||||
"HITL": ["Human in the Loop"]
|
||||
},
|
||||
"resources": {
|
||||
"credentialDocumentation": [
|
||||
{
|
||||
"url": "https://docs.n8n.io/integrations/builtin/credentials/sendemail/"
|
||||
}
|
||||
],
|
||||
"primaryDocumentation": [
|
||||
{
|
||||
"url": "https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.sendemail/"
|
||||
}
|
||||
],
|
||||
"generic": [
|
||||
{
|
||||
"label": "2021: The Year to Automate the New You with n8n",
|
||||
"icon": "☀️",
|
||||
"url": "https://n8n.io/blog/2021-the-year-to-automate-the-new-you-with-n8n/"
|
||||
},
|
||||
{
|
||||
"label": "Build your own virtual assistant with n8n: A step by step guide",
|
||||
"icon": "👦",
|
||||
"url": "https://n8n.io/blog/build-your-own-virtual-assistant-with-n8n-a-step-by-step-guide/"
|
||||
}
|
||||
]
|
||||
},
|
||||
"alias": ["SMTP", "email", "human", "form", "wait", "hitl", "approval"]
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import type { INodeTypeBaseDescription, IVersionedNodeType } from 'n8n-workflow';
|
||||
import { VersionedNodeType } from 'n8n-workflow';
|
||||
|
||||
import { EmailSendV1 } from './v1/EmailSendV1.node';
|
||||
import { EmailSendV2 } from './v2/EmailSendV2.node';
|
||||
|
||||
export class EmailSend extends VersionedNodeType {
|
||||
constructor() {
|
||||
const baseDescription: INodeTypeBaseDescription = {
|
||||
displayName: 'Send Email',
|
||||
name: 'emailSend',
|
||||
icon: 'fa:envelope',
|
||||
group: ['output'],
|
||||
defaultVersion: 2.1,
|
||||
description: 'Sends an email using SMTP protocol',
|
||||
};
|
||||
|
||||
const nodeVersions: IVersionedNodeType['nodeVersions'] = {
|
||||
1: new EmailSendV1(baseDescription),
|
||||
2: new EmailSendV2(baseDescription),
|
||||
2.1: new EmailSendV2(baseDescription),
|
||||
};
|
||||
|
||||
super(nodeVersions, baseDescription);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"accepted": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"ehlo": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"envelope": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"from": {
|
||||
"type": "string"
|
||||
},
|
||||
"to": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"envelopeTime": {
|
||||
"type": "integer"
|
||||
},
|
||||
"messageId": {
|
||||
"type": "string"
|
||||
},
|
||||
"messageSize": {
|
||||
"type": "integer"
|
||||
},
|
||||
"messageTime": {
|
||||
"type": "integer"
|
||||
},
|
||||
"response": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"version": 7
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"approved": {
|
||||
"type": "boolean"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"version": 1
|
||||
}
|
||||
@@ -0,0 +1,520 @@
|
||||
import type { IExecuteFunctions, IBinaryData } from 'n8n-workflow';
|
||||
|
||||
import { EmailSendV1 } from '../../v1/EmailSendV1.node';
|
||||
import { prepareBinariesDataList } from '../../../../utils/binary';
|
||||
|
||||
const transporter = { sendMail: jest.fn() };
|
||||
|
||||
jest.mock('nodemailer', () => ({
|
||||
createTransport: jest.fn(() => transporter),
|
||||
}));
|
||||
|
||||
describe('Test EmailSendV1', () => {
|
||||
let emailSendV1: EmailSendV1;
|
||||
let mockExecuteFunctions: jest.Mocked<IExecuteFunctions>;
|
||||
|
||||
beforeEach(() => {
|
||||
emailSendV1 = new EmailSendV1({
|
||||
description: {
|
||||
displayName: 'Send Email',
|
||||
name: 'emailSend',
|
||||
icon: 'fa:envelope',
|
||||
group: ['output'],
|
||||
description: 'Sends an Email',
|
||||
version: 1,
|
||||
defaults: {
|
||||
name: 'Send Email',
|
||||
color: '#00bb88',
|
||||
},
|
||||
},
|
||||
} as any);
|
||||
|
||||
mockExecuteFunctions = {
|
||||
getInputData: jest.fn(),
|
||||
getCredentials: jest.fn(),
|
||||
getNodeParameter: jest.fn(),
|
||||
helpers: {
|
||||
assertBinaryData: jest.fn(),
|
||||
getBinaryDataBuffer: jest.fn(),
|
||||
},
|
||||
continueOnFail: jest.fn(() => false),
|
||||
} as unknown as jest.Mocked<IExecuteFunctions>;
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('comma-separated attachment strings', () => {
|
||||
it('should process comma-separated attachment names with spaces', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
|
||||
file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
|
||||
file3: { data: 'data3', mimeType: 'text/plain', fileName: 'file3.txt' } as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce('file1, file2, file3')
|
||||
.mockReturnValueOnce({});
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await emailSendV1.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [
|
||||
expect.objectContaining({ filename: 'file1.txt' }),
|
||||
expect.objectContaining({ filename: 'file2.txt' }),
|
||||
expect.objectContaining({ filename: 'file3.txt' }),
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should process comma-separated attachment names without spaces', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
|
||||
file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce('file1,file2')
|
||||
.mockReturnValueOnce({});
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await emailSendV1.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [
|
||||
expect.objectContaining({ filename: 'file1.txt' }),
|
||||
expect.objectContaining({ filename: 'file2.txt' }),
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should trim extra spaces from attachment names', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
|
||||
file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce(' file1 , file2 ')
|
||||
.mockReturnValueOnce({});
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await emailSendV1.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [
|
||||
expect.objectContaining({ filename: 'file1.txt' }),
|
||||
expect.objectContaining({ filename: 'file2.txt' }),
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should process single attachment name', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
singleFile: {
|
||||
data: 'data1',
|
||||
mimeType: 'text/plain',
|
||||
fileName: 'single.txt',
|
||||
} as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce('singleFile')
|
||||
.mockReturnValueOnce({});
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await emailSendV1.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [expect.objectContaining({ filename: 'single.txt' })],
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('prepareBinariesDataList helper function', () => {
|
||||
it('should process string attachment names correctly', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
|
||||
file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce('file1, file2')
|
||||
.mockReturnValueOnce({});
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
const result = prepareBinariesDataList('file1, file2');
|
||||
expect(result).toEqual(['file1', 'file2']);
|
||||
|
||||
await emailSendV1.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: expect.arrayContaining([
|
||||
expect.objectContaining({ filename: 'file1.txt' }),
|
||||
expect.objectContaining({ filename: 'file2.txt' }),
|
||||
]),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should wrap IBinaryData object in array', () => {
|
||||
const input = { data: 'data1', mimeType: 'text/plain', fileName: 'file.txt' };
|
||||
const result = prepareBinariesDataList(input);
|
||||
expect(result).toEqual([input]);
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should return IBinaryData array unchanged', () => {
|
||||
const input = [
|
||||
{ data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' },
|
||||
{ data: 'data2', mimeType: 'image/png', fileName: 'file2.png' },
|
||||
];
|
||||
const result = prepareBinariesDataList(input);
|
||||
expect(result).toEqual(input);
|
||||
expect(result).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should return string array unchanged', () => {
|
||||
const input = ['file1', 'file2', 'file3'];
|
||||
const result = prepareBinariesDataList(input);
|
||||
expect(result).toEqual(['file1', 'file2', 'file3']);
|
||||
});
|
||||
|
||||
it('should process IBinaryData object as attachment', async () => {
|
||||
const binaryDataObject = { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' };
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce(binaryDataObject)
|
||||
.mockReturnValueOnce({});
|
||||
|
||||
// Mock helpers to handle IBinaryData objects directly
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
// If propertyName is already an IBinaryData object, return it
|
||||
if (typeof propertyName === 'object') {
|
||||
return propertyName;
|
||||
}
|
||||
// Otherwise look it up in binary data
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
// If propertyName is already an IBinaryData object, use it directly
|
||||
const binaryData =
|
||||
typeof propertyName === 'object'
|
||||
? propertyName
|
||||
: items[itemIndex].binary![propertyName];
|
||||
return Buffer.from(binaryData.data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await emailSendV1.execute.call(mockExecuteFunctions);
|
||||
|
||||
const result = prepareBinariesDataList(binaryDataObject);
|
||||
expect(result).toEqual([binaryDataObject]);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [expect.objectContaining({ filename: 'file1.txt' })],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should process IBinaryData array as attachments', async () => {
|
||||
const binaryDataArray = [
|
||||
{ data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' },
|
||||
{ data: 'data2', mimeType: 'image/png', fileName: 'file2.png' },
|
||||
];
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce(binaryDataArray)
|
||||
.mockReturnValueOnce({});
|
||||
|
||||
// Mock helpers to handle IBinaryData objects directly
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
// If propertyName is already an IBinaryData object, return it
|
||||
if (typeof propertyName === 'object') {
|
||||
return propertyName;
|
||||
}
|
||||
// Otherwise look it up in binary data
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
// If propertyName is already an IBinaryData object, use it directly
|
||||
const binaryData =
|
||||
typeof propertyName === 'object'
|
||||
? propertyName
|
||||
: items[itemIndex].binary![propertyName];
|
||||
return Buffer.from(binaryData.data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await emailSendV1.execute.call(mockExecuteFunctions);
|
||||
|
||||
const result = prepareBinariesDataList(binaryDataArray);
|
||||
expect(result).toEqual(binaryDataArray);
|
||||
expect(result).toHaveLength(2);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [
|
||||
expect.objectContaining({ filename: 'file1.txt' }),
|
||||
expect.objectContaining({ filename: 'file2.png' }),
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('emails without attachments', () => {
|
||||
it('should send email when attachment string is empty', async () => {
|
||||
const items = [{ json: { data: 'test' } }];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
secure: false,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('Test text')
|
||||
.mockReturnValueOnce('<p>Test HTML</p>')
|
||||
.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce({});
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await emailSendV1.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.not.objectContaining({
|
||||
attachments: expect.anything(),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,485 @@
|
||||
import type { IExecuteFunctions, IBinaryData } from 'n8n-workflow';
|
||||
|
||||
import * as sendOperation from '../../v2/send.operation';
|
||||
import { prepareBinariesDataList } from '../../../../utils/binary';
|
||||
|
||||
const transporter = { sendMail: jest.fn() };
|
||||
|
||||
jest.mock('../../v2/utils', () => {
|
||||
const originalModule = jest.requireActual('../../v2/utils');
|
||||
return {
|
||||
...originalModule,
|
||||
configureTransport: jest.fn(() => transporter),
|
||||
};
|
||||
});
|
||||
|
||||
describe('Test EmailSendV2, send operation', () => {
|
||||
let mockExecuteFunctions: jest.Mocked<IExecuteFunctions>;
|
||||
|
||||
beforeEach(() => {
|
||||
mockExecuteFunctions = {
|
||||
getInputData: jest.fn(),
|
||||
getNode: jest.fn(),
|
||||
getInstanceId: jest.fn(),
|
||||
getCredentials: jest.fn(),
|
||||
getNodeParameter: jest.fn(),
|
||||
helpers: {
|
||||
assertBinaryData: jest.fn(),
|
||||
getBinaryDataBuffer: jest.fn(),
|
||||
},
|
||||
} as unknown as jest.Mocked<IExecuteFunctions>;
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('comma-separated attachment strings', () => {
|
||||
it('should process comma-separated attachment names with spaces', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
|
||||
file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
|
||||
file3: { data: 'data3', mimeType: 'text/plain', fileName: 'file3.txt' } as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
|
||||
mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('html')
|
||||
.mockReturnValueOnce({ attachments: 'file1, file2, file3', appendAttribution: false })
|
||||
.mockReturnValueOnce('<p>Test HTML</p>');
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await sendOperation.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [
|
||||
expect.objectContaining({ filename: 'file1.txt', cid: 'file1' }),
|
||||
expect.objectContaining({ filename: 'file2.txt', cid: 'file2' }),
|
||||
expect.objectContaining({ filename: 'file3.txt', cid: 'file3' }),
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should process comma-separated attachment names without spaces', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
|
||||
file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
|
||||
mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('html')
|
||||
.mockReturnValueOnce({ attachments: 'file1,file2', appendAttribution: false })
|
||||
.mockReturnValueOnce('<p>Test HTML</p>');
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await sendOperation.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [
|
||||
expect.objectContaining({ filename: 'file1.txt', cid: 'file1' }),
|
||||
expect.objectContaining({ filename: 'file2.txt', cid: 'file2' }),
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should trim extra spaces from attachment names', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
|
||||
file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
|
||||
mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('html')
|
||||
.mockReturnValueOnce({ attachments: ' file1 , file2 ', appendAttribution: false })
|
||||
.mockReturnValueOnce('<p>Test HTML</p>');
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await sendOperation.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [
|
||||
expect.objectContaining({ filename: 'file1.txt', cid: 'file1' }),
|
||||
expect.objectContaining({ filename: 'file2.txt', cid: 'file2' }),
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should process single attachment name', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
singleFile: {
|
||||
data: 'data1',
|
||||
mimeType: 'text/plain',
|
||||
fileName: 'single.txt',
|
||||
} as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
|
||||
mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('html')
|
||||
.mockReturnValueOnce({ attachments: 'singleFile', appendAttribution: false })
|
||||
.mockReturnValueOnce('<p>Test HTML</p>');
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await sendOperation.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [expect.objectContaining({ filename: 'single.txt', cid: 'singleFile' })],
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('prepareBinariesDataList helper function', () => {
|
||||
it('should process string attachment names correctly', async () => {
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {
|
||||
file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
|
||||
file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
|
||||
} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
|
||||
mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('html')
|
||||
.mockReturnValueOnce({ attachments: 'file1, file2', appendAttribution: false })
|
||||
.mockReturnValueOnce('<p>Test HTML</p>');
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string) => {
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string) => {
|
||||
return Buffer.from(items[itemIndex].binary![propertyName].data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
const result = prepareBinariesDataList('file1, file2');
|
||||
expect(result).toEqual(['file1', 'file2']);
|
||||
|
||||
await sendOperation.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: expect.arrayContaining([
|
||||
expect.objectContaining({ cid: 'file1' }),
|
||||
expect.objectContaining({ cid: 'file2' }),
|
||||
]),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should wrap IBinaryData object in array', () => {
|
||||
const input = { data: 'data1', mimeType: 'text/plain', fileName: 'file.txt' };
|
||||
const result = prepareBinariesDataList(input);
|
||||
expect(result).toEqual([input]);
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should return IBinaryData array unchanged', () => {
|
||||
const input = [
|
||||
{ data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' },
|
||||
{ data: 'data2', mimeType: 'image/png', fileName: 'file2.png' },
|
||||
];
|
||||
const result = prepareBinariesDataList(input);
|
||||
expect(result).toEqual(input);
|
||||
expect(result).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should return string array unchanged', () => {
|
||||
const input = ['file1', 'file2', 'file3'];
|
||||
const result = prepareBinariesDataList(input);
|
||||
expect(result).toEqual(['file1', 'file2', 'file3']);
|
||||
});
|
||||
|
||||
it('should process IBinaryData object as attachment', async () => {
|
||||
const binaryDataObject = { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' };
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
|
||||
mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('html')
|
||||
.mockReturnValueOnce({ attachments: binaryDataObject, appendAttribution: false })
|
||||
.mockReturnValueOnce('<p>Test HTML</p>');
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
if (typeof propertyName === 'object') {
|
||||
return propertyName;
|
||||
}
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
const binaryData =
|
||||
typeof propertyName === 'object'
|
||||
? propertyName
|
||||
: items[itemIndex].binary![propertyName];
|
||||
return Buffer.from(binaryData.data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await sendOperation.execute.call(mockExecuteFunctions);
|
||||
|
||||
const result = prepareBinariesDataList(binaryDataObject);
|
||||
expect(result).toEqual([binaryDataObject]);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [expect.objectContaining({ filename: 'file1.txt', cid: binaryDataObject })],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should process IBinaryData array as attachments', async () => {
|
||||
const binaryDataArray = [
|
||||
{ data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' },
|
||||
{ data: 'data2', mimeType: 'image/png', fileName: 'file2.png' },
|
||||
];
|
||||
const items = [
|
||||
{
|
||||
json: { data: 'test' },
|
||||
binary: {} as Record<string, IBinaryData>,
|
||||
},
|
||||
];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
|
||||
mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('html')
|
||||
.mockReturnValueOnce({ attachments: binaryDataArray, appendAttribution: false })
|
||||
.mockReturnValueOnce('<p>Test HTML</p>');
|
||||
|
||||
(mockExecuteFunctions.helpers.assertBinaryData as jest.Mock).mockImplementation(
|
||||
(itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
if (typeof propertyName === 'object') {
|
||||
return propertyName;
|
||||
}
|
||||
return items[itemIndex].binary![propertyName];
|
||||
},
|
||||
);
|
||||
|
||||
(mockExecuteFunctions.helpers.getBinaryDataBuffer as jest.Mock).mockImplementation(
|
||||
async (itemIndex: number, propertyName: string | IBinaryData) => {
|
||||
const binaryData =
|
||||
typeof propertyName === 'object'
|
||||
? propertyName
|
||||
: items[itemIndex].binary![propertyName];
|
||||
return Buffer.from(binaryData.data);
|
||||
},
|
||||
);
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await sendOperation.execute.call(mockExecuteFunctions);
|
||||
|
||||
const result = prepareBinariesDataList(binaryDataArray);
|
||||
expect(result).toEqual(binaryDataArray);
|
||||
expect(result).toHaveLength(2);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
attachments: [
|
||||
expect.objectContaining({ filename: 'file1.txt' }),
|
||||
expect.objectContaining({ filename: 'file2.png' }),
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('emails without attachments', () => {
|
||||
it('should send email when no attachments specified', async () => {
|
||||
const items = [{ json: { data: 'test' } }];
|
||||
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
|
||||
mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({
|
||||
host: 'smtp.example.com',
|
||||
port: 587,
|
||||
});
|
||||
|
||||
mockExecuteFunctions.getNodeParameter
|
||||
.mockReturnValueOnce('from@example.com')
|
||||
.mockReturnValueOnce('to@example.com')
|
||||
.mockReturnValueOnce('Test Subject')
|
||||
.mockReturnValueOnce('html')
|
||||
.mockReturnValueOnce({ appendAttribution: false })
|
||||
.mockReturnValueOnce('<p>Test HTML</p>');
|
||||
|
||||
transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||
|
||||
await sendOperation.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith(
|
||||
expect.not.objectContaining({
|
||||
attachments: expect.anything(),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
import type { MockProxy } from 'jest-mock-extended';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { SEND_AND_WAIT_OPERATION, type IExecuteFunctions } from 'n8n-workflow';
|
||||
|
||||
import { EmailSendV2, versionDescription } from '../../v2/EmailSendV2.node';
|
||||
import * as utils from '../../v2/utils';
|
||||
|
||||
const transporter = { sendMail: jest.fn() };
|
||||
|
||||
jest.mock('../../v2/utils', () => {
|
||||
const originalModule = jest.requireActual('../../v2/utils');
|
||||
return {
|
||||
...originalModule,
|
||||
configureTransport: jest.fn(() => transporter),
|
||||
};
|
||||
});
|
||||
|
||||
describe('Test EmailSendV2, email => sendAndWait', () => {
|
||||
let emailSendV2: EmailSendV2;
|
||||
let mockExecuteFunctions: MockProxy<IExecuteFunctions>;
|
||||
|
||||
beforeEach(() => {
|
||||
emailSendV2 = new EmailSendV2(versionDescription);
|
||||
mockExecuteFunctions = mock<IExecuteFunctions>();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should send message and put execution to wait', async () => {
|
||||
const items = [{ json: { data: 'test' } }];
|
||||
//node
|
||||
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce(SEND_AND_WAIT_OPERATION);
|
||||
|
||||
//operation
|
||||
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('from@mail.com');
|
||||
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('to@mail.com');
|
||||
mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
|
||||
mockExecuteFunctions.getCredentials.mockResolvedValue({});
|
||||
mockExecuteFunctions.putExecutionToWait.mockImplementation();
|
||||
mockExecuteFunctions.getInputData.mockReturnValue(items);
|
||||
|
||||
//getSendAndWaitConfig
|
||||
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('my message');
|
||||
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('my subject');
|
||||
mockExecuteFunctions.getSignedResumeUrl.mockReturnValue(
|
||||
'http://localhost/waiting-webhook/nodeID?approved=true&signature=abc',
|
||||
);
|
||||
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce({}); // approvalOptions
|
||||
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce({}); // options
|
||||
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce('approval');
|
||||
|
||||
// configureWaitTillDate
|
||||
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce({}); //options.limitWaitTime.values
|
||||
|
||||
const result = await emailSendV2.execute.call(mockExecuteFunctions);
|
||||
|
||||
expect(result).toEqual([items]);
|
||||
expect(utils.configureTransport).toHaveBeenCalledTimes(1);
|
||||
expect(mockExecuteFunctions.putExecutionToWait).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(transporter.sendMail).toHaveBeenCalledWith({
|
||||
from: 'from@mail.com',
|
||||
html: expect.stringContaining(
|
||||
'href="http://localhost/waiting-webhook/nodeID?approved=true&signature=abc"',
|
||||
),
|
||||
subject: 'my subject',
|
||||
to: 'to@mail.com',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,242 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeBaseDescription,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeConnectionTypes } from 'n8n-workflow';
|
||||
import { createTransport } from 'nodemailer';
|
||||
import type SMTPTransport from 'nodemailer/lib/smtp-transport';
|
||||
import { prepareBinariesDataList } from '../../../utils/binary';
|
||||
|
||||
const versionDescription: INodeTypeDescription = {
|
||||
displayName: 'Send Email',
|
||||
name: 'emailSend',
|
||||
icon: 'fa:envelope',
|
||||
group: ['output'],
|
||||
version: 1,
|
||||
description: 'Sends an Email',
|
||||
defaults: {
|
||||
name: 'Send Email',
|
||||
color: '#00bb88',
|
||||
},
|
||||
inputs: [NodeConnectionTypes.Main],
|
||||
outputs: [NodeConnectionTypes.Main],
|
||||
credentials: [
|
||||
{
|
||||
name: 'smtp',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
// TODO: Add choice for text as text or html (maybe also from name)
|
||||
{
|
||||
displayName: 'From Email',
|
||||
name: 'fromEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
required: true,
|
||||
placeholder: 'admin@example.com',
|
||||
description: 'Email address of the sender optional with name',
|
||||
},
|
||||
{
|
||||
displayName: 'To Email',
|
||||
name: 'toEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
required: true,
|
||||
placeholder: 'info@example.com',
|
||||
description: 'Email address of the recipient',
|
||||
},
|
||||
{
|
||||
displayName: 'CC Email',
|
||||
name: 'ccEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'cc@example.com',
|
||||
description: 'Email address of CC recipient',
|
||||
},
|
||||
{
|
||||
displayName: 'BCC Email',
|
||||
name: 'bccEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'bcc@example.com',
|
||||
description: 'Email address of BCC recipient',
|
||||
},
|
||||
{
|
||||
displayName: 'Subject',
|
||||
name: 'subject',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'My subject line',
|
||||
description: 'Subject line of the email',
|
||||
},
|
||||
{
|
||||
displayName: 'Text',
|
||||
name: 'text',
|
||||
type: 'string',
|
||||
typeOptions: {
|
||||
rows: 5,
|
||||
},
|
||||
default: '',
|
||||
description: 'Plain text message of email',
|
||||
},
|
||||
{
|
||||
displayName: 'HTML',
|
||||
name: 'html',
|
||||
type: 'string',
|
||||
typeOptions: {
|
||||
rows: 5,
|
||||
},
|
||||
default: '',
|
||||
description: 'HTML text message of email',
|
||||
},
|
||||
{
|
||||
displayName: 'Attachments',
|
||||
name: 'attachments',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description:
|
||||
'Name of the binary properties that contain data to add to email as attachment. Multiple ones can be comma-separated.',
|
||||
},
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
placeholder: 'Add option',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Ignore SSL Issues (Insecure)',
|
||||
name: 'allowUnauthorizedCerts',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description: 'Whether to connect even if SSL certificate validation is not possible',
|
||||
},
|
||||
{
|
||||
displayName: 'Reply To',
|
||||
name: 'replyTo',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'info@example.com',
|
||||
description: 'The email address to send the reply to',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export class EmailSendV1 implements INodeType {
|
||||
description: INodeTypeDescription;
|
||||
|
||||
constructor(baseDescription: INodeTypeBaseDescription) {
|
||||
this.description = {
|
||||
...baseDescription,
|
||||
...versionDescription,
|
||||
};
|
||||
}
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
const length = items.length;
|
||||
let item: INodeExecutionData;
|
||||
|
||||
for (let itemIndex = 0; itemIndex < length; itemIndex++) {
|
||||
try {
|
||||
item = items[itemIndex];
|
||||
|
||||
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, {});
|
||||
|
||||
const credentials = await this.getCredentials('smtp');
|
||||
|
||||
const connectionOptions: SMTPTransport.Options = {
|
||||
host: credentials.host as string,
|
||||
port: credentials.port as number,
|
||||
secure: credentials.secure as boolean,
|
||||
};
|
||||
|
||||
if (credentials.user || credentials.password) {
|
||||
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: IDataObject = {
|
||||
from: fromEmail,
|
||||
to: toEmail,
|
||||
cc: ccEmail,
|
||||
bcc: bccEmail,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
replyTo: options.replyTo as string | undefined,
|
||||
};
|
||||
|
||||
if (attachmentPropertyString && item.binary) {
|
||||
const attachments = [];
|
||||
const attachmentProperties = prepareBinariesDataList(attachmentPropertyString);
|
||||
|
||||
for (const propertyName of attachmentProperties) {
|
||||
const binaryData = this.helpers.assertBinaryData(itemIndex, propertyName);
|
||||
attachments.push({
|
||||
filename: binaryData.fileName || 'unknown',
|
||||
content: await this.helpers.getBinaryDataBuffer(itemIndex, propertyName),
|
||||
});
|
||||
}
|
||||
|
||||
if (attachments.length) {
|
||||
mailOptions.attachments = attachments;
|
||||
}
|
||||
}
|
||||
|
||||
// Send the email
|
||||
const info = await transporter.sendMail(mailOptions);
|
||||
|
||||
returnData.push({
|
||||
json: info as unknown as IDataObject,
|
||||
pairedItem: {
|
||||
item: itemIndex,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: {
|
||||
error: error.message,
|
||||
},
|
||||
pairedItem: {
|
||||
item: itemIndex,
|
||||
},
|
||||
});
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return [returnData];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeBaseDescription,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeConnectionTypes, SEND_AND_WAIT_OPERATION } from 'n8n-workflow';
|
||||
|
||||
import * as send from './send.operation';
|
||||
import * as sendAndWait from './sendAndWait.operation';
|
||||
import { smtpConnectionTest } from './utils';
|
||||
import { sendAndWaitWebhooksDescription } from '../../../utils/sendAndWait/descriptions';
|
||||
import {
|
||||
SEND_AND_WAIT_WAITING_TOOLTIP,
|
||||
sendAndWaitWebhook,
|
||||
} from '../../../utils/sendAndWait/utils';
|
||||
|
||||
export const versionDescription: INodeTypeDescription = {
|
||||
displayName: 'Send Email',
|
||||
name: 'emailSend',
|
||||
icon: 'fa:envelope',
|
||||
group: ['output'],
|
||||
version: [2, 2.1],
|
||||
description: 'Sends an email using SMTP protocol',
|
||||
defaults: {
|
||||
name: 'Send Email',
|
||||
color: '#00bb88',
|
||||
},
|
||||
inputs: [NodeConnectionTypes.Main],
|
||||
outputs: [NodeConnectionTypes.Main],
|
||||
usableAsTool: true,
|
||||
credentials: [
|
||||
{
|
||||
name: 'smtp',
|
||||
required: true,
|
||||
testedBy: 'smtpConnectionTest',
|
||||
},
|
||||
],
|
||||
waitingNodeTooltip: SEND_AND_WAIT_WAITING_TOOLTIP,
|
||||
webhooks: sendAndWaitWebhooksDescription,
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'hidden',
|
||||
noDataExpression: true,
|
||||
default: 'email',
|
||||
options: [
|
||||
{
|
||||
name: 'Email',
|
||||
value: 'email',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
default: 'send',
|
||||
options: [
|
||||
{
|
||||
name: 'Send',
|
||||
value: 'send',
|
||||
action: 'Send an Email',
|
||||
},
|
||||
{
|
||||
name: 'Send and Wait for Response',
|
||||
value: SEND_AND_WAIT_OPERATION,
|
||||
action: 'Send message and wait for response',
|
||||
},
|
||||
],
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['email'],
|
||||
},
|
||||
},
|
||||
},
|
||||
...send.description,
|
||||
...sendAndWait.description,
|
||||
],
|
||||
};
|
||||
|
||||
export class EmailSendV2 implements INodeType {
|
||||
description: INodeTypeDescription;
|
||||
|
||||
constructor(baseDescription: INodeTypeBaseDescription) {
|
||||
this.description = {
|
||||
...baseDescription,
|
||||
...versionDescription,
|
||||
};
|
||||
}
|
||||
|
||||
methods = {
|
||||
credentialTest: { smtpConnectionTest },
|
||||
};
|
||||
|
||||
webhook = sendAndWaitWebhook;
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
let returnData: INodeExecutionData[][] = [];
|
||||
const operation = this.getNodeParameter('operation', 0);
|
||||
|
||||
if (operation === SEND_AND_WAIT_OPERATION) {
|
||||
returnData = await sendAndWait.execute.call(this);
|
||||
}
|
||||
|
||||
if (operation === 'send') {
|
||||
returnData = await send.execute.call(this);
|
||||
}
|
||||
|
||||
return returnData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
export const fromEmailProperty: INodeProperties = {
|
||||
displayName: 'From Email',
|
||||
name: 'fromEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
required: true,
|
||||
placeholder: 'admin@example.com',
|
||||
description:
|
||||
'Email address of the sender. You can also specify a name: Nathan Doe <nate@n8n.io>.',
|
||||
};
|
||||
|
||||
export const toEmailProperty: INodeProperties = {
|
||||
displayName: 'To Email',
|
||||
name: 'toEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
required: true,
|
||||
placeholder: 'info@example.com',
|
||||
description:
|
||||
'Email address of the recipient. You can also specify a name: Nathan Doe <nate@n8n.io>.',
|
||||
};
|
||||
@@ -0,0 +1,281 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
JsonObject,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeApiError } from 'n8n-workflow';
|
||||
|
||||
import { createUtmCampaignLink, updateDisplayOptions } from '@utils/utilities';
|
||||
|
||||
import { fromEmailProperty, toEmailProperty } from './descriptions';
|
||||
import { configureTransport, type EmailSendOptions } from './utils';
|
||||
import { appendAttributionOption } from '../../../utils/descriptions';
|
||||
import { prepareBinariesDataList } from '../../../utils/binary';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
// TODO: Add choice for text as text or html (maybe also from name)
|
||||
fromEmailProperty,
|
||||
toEmailProperty,
|
||||
|
||||
{
|
||||
displayName: 'Subject',
|
||||
name: 'subject',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'My subject line',
|
||||
description: 'Subject line of the email',
|
||||
},
|
||||
{
|
||||
displayName: 'Email Format',
|
||||
name: 'emailFormat',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Text',
|
||||
value: 'text',
|
||||
description: 'Send email as plain text',
|
||||
},
|
||||
{
|
||||
name: 'HTML',
|
||||
value: 'html',
|
||||
description: 'Send email as HTML',
|
||||
},
|
||||
{
|
||||
name: 'Both',
|
||||
value: 'both',
|
||||
description: "Send both formats, recipient's client selects version to display",
|
||||
},
|
||||
],
|
||||
default: 'html',
|
||||
displayOptions: {
|
||||
hide: {
|
||||
'@version': [2],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Email Format',
|
||||
name: 'emailFormat',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Text',
|
||||
value: 'text',
|
||||
},
|
||||
{
|
||||
name: 'HTML',
|
||||
value: 'html',
|
||||
},
|
||||
{
|
||||
name: 'Both',
|
||||
value: 'both',
|
||||
},
|
||||
],
|
||||
default: 'text',
|
||||
displayOptions: {
|
||||
show: {
|
||||
'@version': [2],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Text',
|
||||
name: 'text',
|
||||
type: 'string',
|
||||
typeOptions: {
|
||||
rows: 5,
|
||||
},
|
||||
default: '',
|
||||
description: 'Plain text message of email',
|
||||
displayOptions: {
|
||||
show: {
|
||||
emailFormat: ['text', 'both'],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'HTML',
|
||||
name: 'html',
|
||||
type: 'string',
|
||||
typeOptions: {
|
||||
rows: 5,
|
||||
},
|
||||
default: '',
|
||||
description: 'HTML text message of email',
|
||||
displayOptions: {
|
||||
show: {
|
||||
emailFormat: ['html', 'both'],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
placeholder: 'Add option',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
...appendAttributionOption,
|
||||
description:
|
||||
'Whether to include the phrase “This email was sent automatically with n8n” to the end of the email',
|
||||
},
|
||||
{
|
||||
displayName: 'Attachments',
|
||||
name: 'attachments',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description:
|
||||
'Name of the binary properties that contain data to add to email as attachment. Multiple ones can be comma-separated. Reference embedded images or other content within the body of an email message, e.g. <img src="cid:image_1">',
|
||||
},
|
||||
{
|
||||
displayName: 'CC Email',
|
||||
name: 'ccEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'cc@example.com',
|
||||
description: 'Email address of CC recipient',
|
||||
},
|
||||
{
|
||||
displayName: 'BCC Email',
|
||||
name: 'bccEmail',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'bcc@example.com',
|
||||
description: 'Email address of BCC recipient',
|
||||
},
|
||||
{
|
||||
displayName: 'Ignore SSL Issues (Insecure)',
|
||||
name: 'allowUnauthorizedCerts',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description: 'Whether to connect even if SSL certificate validation is not possible',
|
||||
},
|
||||
{
|
||||
displayName: 'Reply To',
|
||||
name: 'replyTo',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'info@example.com',
|
||||
description: 'The email address to send the reply to',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['email'],
|
||||
operation: ['send'],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const nodeVersion = this.getNode().typeVersion;
|
||||
const instanceId = this.getInstanceId();
|
||||
const credentials = await this.getCredentials('smtp');
|
||||
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
let item: INodeExecutionData;
|
||||
|
||||
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
||||
try {
|
||||
item = items[itemIndex];
|
||||
|
||||
const fromEmail = this.getNodeParameter('fromEmail', itemIndex) as string;
|
||||
const toEmail = this.getNodeParameter('toEmail', itemIndex) as string;
|
||||
const subject = this.getNodeParameter('subject', itemIndex) as string;
|
||||
const emailFormat = this.getNodeParameter('emailFormat', itemIndex) as string;
|
||||
const options = this.getNodeParameter('options', itemIndex, {}) as EmailSendOptions;
|
||||
|
||||
const transporter = configureTransport(credentials, options);
|
||||
|
||||
const mailOptions: IDataObject = {
|
||||
from: fromEmail,
|
||||
to: toEmail,
|
||||
cc: options.ccEmail,
|
||||
bcc: options.bccEmail,
|
||||
subject,
|
||||
replyTo: options.replyTo,
|
||||
};
|
||||
|
||||
if (emailFormat === 'text' || emailFormat === 'both') {
|
||||
mailOptions.text = this.getNodeParameter('text', itemIndex, '');
|
||||
}
|
||||
|
||||
if (emailFormat === 'html' || emailFormat === 'both') {
|
||||
mailOptions.html = this.getNodeParameter('html', itemIndex, '');
|
||||
}
|
||||
|
||||
let appendAttribution = options.appendAttribution;
|
||||
if (appendAttribution === undefined) {
|
||||
appendAttribution = nodeVersion >= 2.1;
|
||||
}
|
||||
|
||||
if (appendAttribution) {
|
||||
const attributionText = 'This email was sent automatically with ';
|
||||
const link = createUtmCampaignLink('n8n-nodes-base.emailSend', instanceId);
|
||||
if (emailFormat === 'html' || (emailFormat === 'both' && mailOptions.html)) {
|
||||
mailOptions.html = `
|
||||
${mailOptions.html}
|
||||
<br>
|
||||
<br>
|
||||
---
|
||||
<br>
|
||||
<em>${attributionText}<a href="${link}" target="_blank">n8n</a></em>
|
||||
`;
|
||||
} else {
|
||||
mailOptions.text = `${mailOptions.text}\n\n---\n${attributionText}n8n\n${'https://n8n.io'}`;
|
||||
}
|
||||
}
|
||||
|
||||
if (options.attachments && item.binary) {
|
||||
const attachments = [];
|
||||
const attachmentProperties = prepareBinariesDataList(options.attachments);
|
||||
|
||||
for (const propertyName of attachmentProperties) {
|
||||
const binaryData = this.helpers.assertBinaryData(itemIndex, propertyName);
|
||||
attachments.push({
|
||||
filename: binaryData.fileName || 'unknown',
|
||||
content: await this.helpers.getBinaryDataBuffer(itemIndex, propertyName),
|
||||
cid: propertyName,
|
||||
});
|
||||
}
|
||||
|
||||
if (attachments.length) {
|
||||
mailOptions.attachments = attachments;
|
||||
}
|
||||
}
|
||||
|
||||
const info = await transporter.sendMail(mailOptions);
|
||||
|
||||
returnData.push({
|
||||
json: info as unknown as IDataObject,
|
||||
pairedItem: {
|
||||
item: itemIndex,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({
|
||||
json: {
|
||||
error: error.message,
|
||||
},
|
||||
pairedItem: {
|
||||
item: itemIndex,
|
||||
},
|
||||
});
|
||||
continue;
|
||||
}
|
||||
delete error.cert;
|
||||
throw new NodeApiError(this.getNode(), error as JsonObject);
|
||||
}
|
||||
}
|
||||
|
||||
return [returnData];
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { fromEmailProperty, toEmailProperty } from './descriptions';
|
||||
import { configureTransport } from './utils';
|
||||
import { configureWaitTillDate } from '../../../utils/sendAndWait/configureWaitTillDate.util';
|
||||
import {
|
||||
createEmailBodyWithN8nAttribution,
|
||||
createEmailBodyWithoutN8nAttribution,
|
||||
} from '../../../utils/sendAndWait/email-templates';
|
||||
import {
|
||||
createButton,
|
||||
getSendAndWaitConfig,
|
||||
getSendAndWaitProperties,
|
||||
} from '../../../utils/sendAndWait/utils';
|
||||
|
||||
export const description: INodeProperties[] = getSendAndWaitProperties(
|
||||
[fromEmailProperty, toEmailProperty],
|
||||
'email',
|
||||
);
|
||||
|
||||
export async function execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const fromEmail = this.getNodeParameter('fromEmail', 0) as string;
|
||||
const toEmail = this.getNodeParameter('toEmail', 0) as string;
|
||||
|
||||
const config = getSendAndWaitConfig(this);
|
||||
const buttons: string[] = [];
|
||||
for (const option of config.options) {
|
||||
buttons.push(createButton(option.url, option.label, option.style));
|
||||
}
|
||||
|
||||
let htmlBody: string;
|
||||
|
||||
if (config.appendAttribution !== false) {
|
||||
const instanceId = this.getInstanceId();
|
||||
htmlBody = createEmailBodyWithN8nAttribution(config.message, buttons.join('\n'), instanceId);
|
||||
} else {
|
||||
htmlBody = createEmailBodyWithoutN8nAttribution(config.message, buttons.join('\n'));
|
||||
}
|
||||
|
||||
const mailOptions: IDataObject = {
|
||||
from: fromEmail,
|
||||
to: toEmail,
|
||||
subject: config.title,
|
||||
html: htmlBody,
|
||||
};
|
||||
|
||||
const credentials = await this.getCredentials('smtp');
|
||||
const transporter = configureTransport(credentials, {});
|
||||
|
||||
await transporter.sendMail(mailOptions);
|
||||
|
||||
const waitTill = configureWaitTillDate(this);
|
||||
|
||||
await this.putExecutionToWait(waitTill);
|
||||
return [this.getInputData()];
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
ICredentialsDecrypted,
|
||||
ICredentialTestFunctions,
|
||||
INodeCredentialTestResult,
|
||||
} from 'n8n-workflow';
|
||||
import { createTransport } from 'nodemailer';
|
||||
import type SMTPTransport from 'nodemailer/lib/smtp-transport';
|
||||
|
||||
export type EmailSendOptions = {
|
||||
appendAttribution?: boolean;
|
||||
allowUnauthorizedCerts?: boolean;
|
||||
attachments?: string;
|
||||
ccEmail?: string;
|
||||
bccEmail?: string;
|
||||
replyTo?: string;
|
||||
};
|
||||
|
||||
export function configureTransport(credentials: IDataObject, options: EmailSendOptions) {
|
||||
const connectionOptions: SMTPTransport.Options = {
|
||||
host: credentials.host as string,
|
||||
port: credentials.port as number,
|
||||
secure: credentials.secure as boolean,
|
||||
};
|
||||
|
||||
if (credentials.secure === false) {
|
||||
connectionOptions.ignoreTLS = credentials.disableStartTls as boolean;
|
||||
}
|
||||
|
||||
if (typeof credentials.hostName === 'string' && credentials.hostName) {
|
||||
connectionOptions.name = credentials.hostName;
|
||||
}
|
||||
|
||||
if (credentials.user || credentials.password) {
|
||||
connectionOptions.auth = {
|
||||
user: credentials.user as string,
|
||||
pass: credentials.password as string,
|
||||
};
|
||||
}
|
||||
|
||||
if (options.allowUnauthorizedCerts === true) {
|
||||
connectionOptions.tls = {
|
||||
rejectUnauthorized: false,
|
||||
};
|
||||
}
|
||||
|
||||
return createTransport(connectionOptions);
|
||||
}
|
||||
|
||||
export async function smtpConnectionTest(
|
||||
this: ICredentialTestFunctions,
|
||||
credential: ICredentialsDecrypted,
|
||||
): Promise<INodeCredentialTestResult> {
|
||||
const credentials = credential.data!;
|
||||
const transporter = configureTransport(credentials, {});
|
||||
try {
|
||||
await transporter.verify();
|
||||
return {
|
||||
status: 'OK',
|
||||
message: 'Connection successful!',
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 'Error',
|
||||
message: error.message,
|
||||
};
|
||||
} finally {
|
||||
transporter.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user