📧 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,
|
||||
});
|
||||
|
||||
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,
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
|
|
|
@ -140,6 +140,7 @@ export const sendSmtp = async ({
|
|||
smtpSettings: {
|
||||
host: smtpConfiguration.smtpHost,
|
||||
port: parseInt(smtpConfiguration.smtpPort, 10),
|
||||
encryption: smtpConfiguration.encryption,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue