diff --git a/packages/nodes-base/nodes/EmailSend.node.ts b/packages/nodes-base/nodes/EmailSend.node.ts index f2ca08ad46..7e203d8890 100644 --- a/packages/nodes-base/nodes/EmailSend.node.ts +++ b/packages/nodes-base/nodes/EmailSend.node.ts @@ -1,6 +1,6 @@ import { BINARY_ENCODING, - IExecuteSingleFunctions, + IExecuteFunctions } from 'n8n-core'; import { IDataObject, @@ -124,84 +124,95 @@ export class EmailSend implements INodeType { }; - async executeSingle(this: IExecuteSingleFunctions): Promise { - const item = this.getInputData(); + async execute(this: IExecuteFunctions): Promise { + const items = this.getInputData(); - const fromEmail = this.getNodeParameter('fromEmail') as string; - const toEmail = this.getNodeParameter('toEmail') as string; - const ccEmail = this.getNodeParameter('ccEmail') as string; - const bccEmail = this.getNodeParameter('bccEmail') as string; - const subject = this.getNodeParameter('subject') as string; - const text = this.getNodeParameter('text') as string; - const html = this.getNodeParameter('html') as string; - const attachmentPropertyString = this.getNodeParameter('attachments') as string; - const options = this.getNodeParameter('options', {}) as IDataObject; + const returnData: INodeExecutionData[] = []; + const length = items.length as unknown as number; + let item: INodeExecutionData; - const credentials = this.getCredentials('smtp'); + for (let itemIndex = 0; itemIndex < length; itemIndex++) { + + item = items[itemIndex]; + console.log(item,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, {}) as IDataObject; - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } + const credentials = 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) { - // @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, - }; - - if (attachmentPropertyString && item.binary) { - const attachments = []; - const attachmentProperties: string[] = attachmentPropertyString.split(',').map((propertyName) => { - return propertyName.trim(); - }); - - for (const propertyName of attachmentProperties) { - if (!item.binary.hasOwnProperty(propertyName)) { - continue; - } - attachments.push({ - filename: item.binary[propertyName].fileName || 'unknown', - content: Buffer.from(item.binary[propertyName].data, BINARY_ENCODING), - }); + if (credentials === undefined) { + throw new Error('No credentials got returned!'); } - if (attachments.length) { + const connectionOptions: SMTPTransport.Options = { + host: credentials.host as string, + port: credentials.port as number, + secure: credentials.secure as boolean, + }; + + if(credentials.user || credentials.password) { // @ts-ignore - mailOptions.attachments = attachments; + 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, + }; + + if (attachmentPropertyString && item.binary) { + const attachments = []; + const attachmentProperties: string[] = attachmentPropertyString.split(',').map((propertyName) => { + return propertyName.trim(); + }); + + for (const propertyName of attachmentProperties) { + if (!item.binary.hasOwnProperty(propertyName)) { + continue; + } + attachments.push({ + filename: item.binary[propertyName].fileName || 'unknown', + content: Buffer.from(item.binary[propertyName].data, BINARY_ENCODING), + }); + } + + if (attachments.length) { + // @ts-ignore + mailOptions.attachments = attachments; + } + } + + // Send the email + const info = await transporter.sendMail(mailOptions); + + returnData.push({ json: info }); } - - // Send the email - const info = await transporter.sendMail(mailOptions); - - return { json: info }; + + return this.prepareOutputData(returnData); } }