📧 Fix for setting up SMTP transport (#723)
* Fix SMTP transport settings based on encryption * Stop reporting debug message as error * Shave some time on fetching configurations * Add changeset
This commit is contained in:
parent
2d77bca353
commit
bda814b7b3
5 changed files with 78 additions and 41 deletions
5
.changeset/tough-dingos-rule.md
Normal file
5
.changeset/tough-dingos-rule.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"saleor-app-emails-and-messages": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixed issue with SMTP provider not sending emails on some ports.
|
|
@ -46,11 +46,26 @@ export const sendEventMessages = async ({
|
||||||
featureFlagService,
|
featureFlagService,
|
||||||
});
|
});
|
||||||
|
|
||||||
const availableSmtpConfigurations = await smtpConfigurationService.getConfigurations({
|
const sendgridConfigurationService = new SendgridConfigurationService({
|
||||||
active: true,
|
metadataManager: new SendgridPrivateMetadataManager(
|
||||||
availableInChannel: channel,
|
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) {
|
for (const smtpConfiguration of availableSmtpConfigurations) {
|
||||||
const smtpStatus = await sendSmtp({
|
const smtpStatus = await sendSmtp({
|
||||||
event,
|
event,
|
||||||
|
@ -67,19 +82,6 @@ export const sendEventMessages = async ({
|
||||||
|
|
||||||
logger.debug("Channel has assigned Sendgrid configuration");
|
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) {
|
for (const sendgridConfiguration of availableSendgridConfigurations) {
|
||||||
const sendgridStatus = await sendSendgrid({
|
const sendgridStatus = await sendSendgrid({
|
||||||
event,
|
event,
|
||||||
|
|
|
@ -30,37 +30,19 @@ export const sendSendgrid = async ({
|
||||||
|
|
||||||
if (!sendgridConfiguration.senderEmail) {
|
if (!sendgridConfiguration.senderEmail) {
|
||||||
logger.debug("Sender email has not been specified, skipping");
|
logger.debug("Sender email has not been specified, skipping");
|
||||||
return {
|
return;
|
||||||
errors: [
|
|
||||||
{
|
|
||||||
message: "Sender email has not been set up",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const eventSettings = sendgridConfiguration.events.find((e) => e.eventType === event);
|
const eventSettings = sendgridConfiguration.events.find((e) => e.eventType === event);
|
||||||
|
|
||||||
if (!eventSettings) {
|
if (!eventSettings) {
|
||||||
logger.debug("No active settings for this event, skipping");
|
logger.debug("No active settings for this event, skipping");
|
||||||
return {
|
return;
|
||||||
errors: [
|
|
||||||
{
|
|
||||||
message: "No active settings for this event",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!eventSettings.active) {
|
if (!eventSettings.active) {
|
||||||
logger.debug("Event settings are not active, skipping");
|
logger.debug("Event settings are not active, skipping");
|
||||||
return {
|
return;
|
||||||
errors: [
|
|
||||||
{
|
|
||||||
message: "Event settings are not active",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Sending an email using Sendgrid");
|
logger.debug("Sending an email using Sendgrid");
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import nodemailer from "nodemailer";
|
import nodemailer from "nodemailer";
|
||||||
import { createLogger } from "@saleor/apps-shared";
|
import { createLogger } from "@saleor/apps-shared";
|
||||||
|
import { SmtpEncryptionType } from "./configuration/migrations/mjml-config-schema-v1";
|
||||||
|
|
||||||
const logger = createLogger({
|
const logger = createLogger({
|
||||||
fn: "sendEmailWithSmtp",
|
fn: "sendEmailWithSmtp",
|
||||||
|
@ -13,6 +14,7 @@ export interface SendMailArgs {
|
||||||
user: string;
|
user: string;
|
||||||
pass: string | undefined;
|
pass: string | undefined;
|
||||||
};
|
};
|
||||||
|
encryption: SmtpEncryptionType;
|
||||||
};
|
};
|
||||||
mailData: {
|
mailData: {
|
||||||
from: string;
|
from: string;
|
||||||
|
@ -25,11 +27,56 @@ export interface SendMailArgs {
|
||||||
|
|
||||||
export const sendEmailWithSmtp = async ({ smtpSettings, mailData }: SendMailArgs) => {
|
export const sendEmailWithSmtp = async ({ smtpSettings, mailData }: SendMailArgs) => {
|
||||||
logger.debug("Sending an email with SMTP");
|
logger.debug("Sending an email with SMTP");
|
||||||
try {
|
let transporter: nodemailer.Transporter;
|
||||||
const transporter = nodemailer.createTransport({
|
|
||||||
...smtpSettings,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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({
|
const response = await transporter.sendMail({
|
||||||
...mailData,
|
...mailData,
|
||||||
});
|
});
|
||||||
|
|
|
@ -140,6 +140,7 @@ export const sendSmtp = async ({
|
||||||
smtpSettings: {
|
smtpSettings: {
|
||||||
host: smtpConfiguration.smtpHost,
|
host: smtpConfiguration.smtpHost,
|
||||||
port: parseInt(smtpConfiguration.smtpPort, 10),
|
port: parseInt(smtpConfiguration.smtpPort, 10),
|
||||||
|
encryption: smtpConfiguration.encryption,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue