From 8dfb919aa039fc8c2b437a03ff241b8937920bdb Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Tue, 25 Oct 2022 13:23:14 +0200 Subject: [PATCH] feat: Implement send email action --- .../src/apps/smtp/actions/send-email/index.ts | 105 ++++++++++++++++++ .../src/apps/smtp/auth/verify-credentials.ts | 14 +-- .../src/apps/smtp/common/transporter.ts | 16 +++ 3 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 packages/backend/src/apps/smtp/actions/send-email/index.ts create mode 100644 packages/backend/src/apps/smtp/common/transporter.ts diff --git a/packages/backend/src/apps/smtp/actions/send-email/index.ts b/packages/backend/src/apps/smtp/actions/send-email/index.ts new file mode 100644 index 00000000..c46e8c3c --- /dev/null +++ b/packages/backend/src/apps/smtp/actions/send-email/index.ts @@ -0,0 +1,105 @@ +import { IJSONObject } from '@automatisch/types'; +import defineAction from '../../../../helpers/define-action'; +import transporter from '../../common/transporter'; + +export default defineAction({ + name: 'Send Email', + key: 'sendEmail', + description: 'Send an email', + substeps: [ + { + key: 'chooseConnection', + name: 'Choose connection', + }, + { + key: 'chooseAction', + name: 'Set up action', + arguments: [ + { + label: 'From name', + key: 'fromName', + type: 'string', + required: false, + description: 'Display name of the sender.', + variables: true, + }, + { + label: 'From email', + key: 'fromEmail', + type: 'string', + required: true, + description: 'Email address of the sender.', + variables: true, + }, + { + label: 'Reply to', + key: 'replyTo', + type: 'string', + required: false, + description: + 'Email address to reply to. Defaults to the from email address.', + variables: true, + }, + { + label: 'To', + key: 'to', + type: 'string', + required: true, + description: + 'Comma seperated list of email addresses to send the email to.', + variables: true, + }, + { + label: 'Cc', + key: 'cc', + type: 'string', + required: false, + description: 'Comma seperated list of email addresses.', + variables: true, + }, + { + label: 'Bcc', + key: 'bcc', + type: 'string', + required: false, + description: 'Comma seperated list of email addresses.', + variables: true, + }, + { + label: 'Subject', + key: 'subject', + type: 'string', + required: true, + description: 'Subject of the email.', + variables: true, + }, + { + label: 'Body', + key: 'body', + type: 'string', + required: true, + description: 'Body of the email.', + variables: true, + }, + ], + }, + { + key: 'testStep', + name: 'Test action', + }, + ], + + async run($) { + const info = await transporter($).sendMail({ + from: `${$.step.parameters.fromName} <${$.step.parameters.fromEmail}>`, + to: ($.step.parameters.to as string).split(','), + replyTo: $.step.parameters.replyTo as string, + cc: ($.step.parameters.cc as string).split(','), + bcc: ($.step.parameters.bcc as string).split(','), + subject: $.step.parameters.subject as string, + text: $.step.parameters.body as string, + }); + + $.setActionItem({ raw: info as IJSONObject }); + }, +}); diff --git a/packages/backend/src/apps/smtp/auth/verify-credentials.ts b/packages/backend/src/apps/smtp/auth/verify-credentials.ts index 22189967..73891a7f 100644 --- a/packages/backend/src/apps/smtp/auth/verify-credentials.ts +++ b/packages/backend/src/apps/smtp/auth/verify-credentials.ts @@ -1,18 +1,8 @@ import { IGlobalVariable } from '@automatisch/types'; -import nodemailer, { TransportOptions } from 'nodemailer'; +import transporter from '../common/transporter'; const verifyCredentials = async ($: IGlobalVariable) => { - const client = nodemailer.createTransport({ - host: $.auth.data.host, - port: $.auth.data.port, - secure: $.auth.data.useTls, - auth: { - user: $.auth.data.username, - pass: $.auth.data.password, - }, - } as TransportOptions); - - await client.verify(); + await transporter($).verify(); await $.auth.set({ screenName: $.auth.data.username, diff --git a/packages/backend/src/apps/smtp/common/transporter.ts b/packages/backend/src/apps/smtp/common/transporter.ts new file mode 100644 index 00000000..2a975415 --- /dev/null +++ b/packages/backend/src/apps/smtp/common/transporter.ts @@ -0,0 +1,16 @@ +import { IGlobalVariable } from '@automatisch/types'; +import nodemailer, { TransportOptions } from 'nodemailer'; + +const transporter = ($: IGlobalVariable) => { + return nodemailer.createTransport({ + host: $.auth.data.host, + port: $.auth.data.port, + secure: $.auth.data.useTls, + auth: { + user: $.auth.data.username, + pass: $.auth.data.password, + }, + } as TransportOptions); +}; + +export default transporter;