diff --git a/.changeset/tough-dingos-rule.md b/.changeset/tough-dingos-rule.md new file mode 100644 index 0000000..da5633e --- /dev/null +++ b/.changeset/tough-dingos-rule.md @@ -0,0 +1,5 @@ +--- +"saleor-app-emails-and-messages": patch +--- + +Fixed issue with SMTP provider not sending emails on some ports. diff --git a/apps/emails-and-messages/src/modules/event-handlers/send-event-messages.ts b/apps/emails-and-messages/src/modules/event-handlers/send-event-messages.ts index 17b2287..4a615ac 100644 --- a/apps/emails-and-messages/src/modules/event-handlers/send-event-messages.ts +++ b/apps/emails-and-messages/src/modules/event-handlers/send-event-messages.ts @@ -46,11 +46,26 @@ export const sendEventMessages = async ({ featureFlagService, }); - const availableSmtpConfigurations = await smtpConfigurationService.getConfigurations({ - active: true, - availableInChannel: channel, + const sendgridConfigurationService = new SendgridConfigurationService({ + metadataManager: new SendgridPrivateMetadataManager( + createSettingsManager(client, authData.appId), + authData.saleorApiUrl + ), + featureFlagService, }); + // Fetch configurations for all providers concurrently + const [availableSmtpConfigurations, availableSendgridConfigurations] = await Promise.all([ + smtpConfigurationService.getConfigurations({ + active: true, + availableInChannel: channel, + }), + sendgridConfigurationService.getConfigurations({ + active: true, + availableInChannel: channel, + }), + ]); + for (const smtpConfiguration of availableSmtpConfigurations) { const smtpStatus = await sendSmtp({ event, @@ -67,19 +82,6 @@ export const sendEventMessages = async ({ logger.debug("Channel has assigned Sendgrid configuration"); - const sendgridConfigurationService = new SendgridConfigurationService({ - metadataManager: new SendgridPrivateMetadataManager( - createSettingsManager(client, authData.appId), - authData.saleorApiUrl - ), - featureFlagService, - }); - - const availableSendgridConfigurations = await sendgridConfigurationService.getConfigurations({ - active: true, - availableInChannel: channel, - }); - for (const sendgridConfiguration of availableSendgridConfigurations) { const sendgridStatus = await sendSendgrid({ event, diff --git a/apps/emails-and-messages/src/modules/sendgrid/send-sendgrid.ts b/apps/emails-and-messages/src/modules/sendgrid/send-sendgrid.ts index 24647a5..90fcae3 100644 --- a/apps/emails-and-messages/src/modules/sendgrid/send-sendgrid.ts +++ b/apps/emails-and-messages/src/modules/sendgrid/send-sendgrid.ts @@ -30,37 +30,19 @@ export const sendSendgrid = async ({ if (!sendgridConfiguration.senderEmail) { logger.debug("Sender email has not been specified, skipping"); - return { - errors: [ - { - message: "Sender email has not been set up", - }, - ], - }; + return; } const eventSettings = sendgridConfiguration.events.find((e) => e.eventType === event); if (!eventSettings) { logger.debug("No active settings for this event, skipping"); - return { - errors: [ - { - message: "No active settings for this event", - }, - ], - }; + return; } if (!eventSettings.active) { logger.debug("Event settings are not active, skipping"); - return { - errors: [ - { - message: "Event settings are not active", - }, - ], - }; + return; } logger.debug("Sending an email using Sendgrid"); diff --git a/apps/emails-and-messages/src/modules/smtp/send-email-with-smtp.ts b/apps/emails-and-messages/src/modules/smtp/send-email-with-smtp.ts index 3696f5b..7368357 100644 --- a/apps/emails-and-messages/src/modules/smtp/send-email-with-smtp.ts +++ b/apps/emails-and-messages/src/modules/smtp/send-email-with-smtp.ts @@ -1,5 +1,6 @@ import nodemailer from "nodemailer"; import { createLogger } from "@saleor/apps-shared"; +import { SmtpEncryptionType } from "./configuration/migrations/mjml-config-schema-v1"; const logger = createLogger({ fn: "sendEmailWithSmtp", @@ -13,6 +14,7 @@ export interface SendMailArgs { user: string; pass: string | undefined; }; + encryption: SmtpEncryptionType; }; mailData: { from: string; @@ -25,11 +27,56 @@ export interface SendMailArgs { export const sendEmailWithSmtp = async ({ smtpSettings, mailData }: SendMailArgs) => { logger.debug("Sending an email with SMTP"); - try { - const transporter = nodemailer.createTransport({ - ...smtpSettings, - }); + let transporter: nodemailer.Transporter; + /* + * https://github.com/nodemailer/nodemailer/issues/1461#issuecomment-1263131029 + * [secure argument] it’s not about security but if the server starts tcp connections over TLS mode or not. + * If it starts connections in cleartext mode, the client can not use TLS until STARTTLS can be established later. + */ + + switch (smtpSettings.encryption) { + case "TLS": + transporter = nodemailer.createTransport({ + tls: { + minVersion: "TLSv1.1", + }, + secure: false, + host: smtpSettings.host, + port: smtpSettings.port, + auth: { + user: smtpSettings.auth?.user, + pass: smtpSettings.auth?.pass, + }, + }); + break; + case "SSL": + transporter = nodemailer.createTransport({ + secure: true, + host: smtpSettings.host, + port: smtpSettings.port, + auth: { + user: smtpSettings.auth?.user, + pass: smtpSettings.auth?.pass, + }, + }); + break; + case "NONE": + transporter = nodemailer.createTransport({ + host: smtpSettings.host, + port: smtpSettings.port, + secure: false, + auth: { + user: smtpSettings.auth?.user, + pass: smtpSettings.auth?.pass, + }, + }); + break; + default: + throw new Error("Unknown encryption type"); + } + + try { const response = await transporter.sendMail({ ...mailData, }); diff --git a/apps/emails-and-messages/src/modules/smtp/send-smtp.ts b/apps/emails-and-messages/src/modules/smtp/send-smtp.ts index 4293b10..35a4800 100644 --- a/apps/emails-and-messages/src/modules/smtp/send-smtp.ts +++ b/apps/emails-and-messages/src/modules/smtp/send-smtp.ts @@ -140,6 +140,7 @@ export const sendSmtp = async ({ smtpSettings: { host: smtpConfiguration.smtpHost, port: parseInt(smtpConfiguration.smtpPort, 10), + encryption: smtpConfiguration.encryption, }, };