Add SMTP server auth (#300)

* Add SMTP server auth

* Add changeset
This commit is contained in:
Krzysztof Wolski 2023-03-16 19:07:24 +01:00 committed by GitHub
parent c952624dd9
commit a44aaf00a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 32 deletions

View file

@ -0,0 +1,5 @@
---
"saleor-app-emails-and-messages": patch
---
Fixed SMTP auth data not being properly passed to the sending service

View file

@ -22,6 +22,7 @@ export const getDefaultEmptyConfiguration = (): MjmlConfiguration => {
smtpHost: "", smtpHost: "",
smtpPort: "", smtpPort: "",
smtpUser: "", smtpUser: "",
smtpPassword: "",
encryption: "NONE", encryption: "NONE",
events: getDefaultEventsConfiguration(), events: getDefaultEventsConfiguration(),
}; };

View file

@ -2,34 +2,6 @@ import { z } from "zod";
import { messageEventTypes } from "../../event-handlers/message-event-types"; import { messageEventTypes } from "../../event-handlers/message-event-types";
import { smtpEncryptionTypes } from "./mjml-config"; import { smtpEncryptionTypes } from "./mjml-config";
export const mjmlConfigInputSchema = z.object({
configurations: z.array(
z.object({
active: z.boolean(),
configurationName: z.string(),
senderName: z.string(),
senderEmail: z.string().email(),
smtpHost: z.string(),
smtpPort: z.string(),
smtpUser: z.string().min(0),
useTls: z.boolean(),
useSsl: z.boolean(),
templateInvoiceSentSubject: z.string(),
templateInvoiceSentTemplate: z.string(),
templateOrderCancelledSubject: z.string(),
templateOrderCancelledTemplate: z.string(),
templateOrderConfirmedSubject: z.string(),
templateOrderConfirmedTemplate: z.string(),
templateOrderFullyPaidSubject: z.string(),
templateOrderFullyPaidTemplate: z.string(),
templateOrderCreatedSubject: z.string(),
templateOrderCreatedTemplate: z.string(),
templateOrderFulfilledSubject: z.string(),
templateOrderFulfilledTemplate: z.string(),
})
),
});
export const mjmlConfigurationEventObjectSchema = z.object({ export const mjmlConfigurationEventObjectSchema = z.object({
active: z.boolean(), active: z.boolean(),
eventType: z.enum(messageEventTypes), eventType: z.enum(messageEventTypes),
@ -45,6 +17,7 @@ export const mjmlConfigurationBaseObjectSchema = z.object({
smtpHost: z.string().min(1), smtpHost: z.string().min(1),
smtpPort: z.string(), smtpPort: z.string(),
smtpUser: z.string(), smtpUser: z.string(),
smtpPassword: z.string(),
encryption: z.enum(smtpEncryptionTypes), encryption: z.enum(smtpEncryptionTypes),
}); });

View file

@ -20,6 +20,7 @@ export interface MjmlConfiguration {
smtpHost: string; smtpHost: string;
smtpPort: string; smtpPort: string;
smtpUser: string; smtpUser: string;
smtpPassword: string;
encryption: SmtpEncryptionType; encryption: SmtpEncryptionType;
events: MjmlEventConfiguration[]; events: MjmlEventConfiguration[];
} }

View file

@ -258,6 +258,21 @@ export const MjmlConfigurationForm = (props: Props) => {
)} )}
/> />
<Controller
name="smtpPassword"
control={control}
render={({ field: { onChange, value }, fieldState: { error } }) => (
<TextField
label="SMTP server password"
value={value}
helperText={error?.message}
error={!!error}
onChange={onChange}
{...CommonFieldProps}
/>
)}
/>
<Controller <Controller
control={control} control={control}
name="encryption" name="encryption"

View file

@ -5,10 +5,14 @@ const logger = pinoLogger.child({
fn: "sendEmailWithSmtp", fn: "sendEmailWithSmtp",
}); });
interface SendMailArgs { export interface SendMailArgs {
smtpSettings: { smtpSettings: {
host: string; host: string;
port: number; port: number;
auth?: {
user: string;
pass: string | undefined;
};
}; };
mailData: { mailData: {
from: string; from: string;

View file

@ -1,7 +1,7 @@
import { logger as pinoLogger } from "../../lib/logger"; import { logger as pinoLogger } from "../../lib/logger";
import { compileMjml } from "./compile-mjml"; import { compileMjml } from "./compile-mjml";
import { compileHandlebarsTemplate } from "./compile-handlebars-template"; import { compileHandlebarsTemplate } from "./compile-handlebars-template";
import { sendEmailWithSmtp } from "./send-email-with-smtp"; import { sendEmailWithSmtp, SendMailArgs } from "./send-email-with-smtp";
import { MessageEventTypes } from "../event-handlers/message-event-types"; import { MessageEventTypes } from "../event-handlers/message-event-types";
import { htmlToPlaintext } from "./html-to-plaintext"; import { htmlToPlaintext } from "./html-to-plaintext";
import { MjmlConfiguration } from "./configuration/mjml-config"; import { MjmlConfiguration } from "./configuration/mjml-config";
@ -128,7 +128,7 @@ export const sendMjml = async ({
}; };
} }
const { response, errors: smtpErrors } = await sendEmailWithSmtp({ const sendEmailSettings: SendMailArgs = {
mailData: { mailData: {
text: emailBodyPlaintext, text: emailBodyPlaintext,
html: emailBodyHtml, html: emailBodyHtml,
@ -140,7 +140,16 @@ export const sendMjml = async ({
host: mjmlConfiguration.smtpHost, host: mjmlConfiguration.smtpHost,
port: parseInt(mjmlConfiguration.smtpPort, 10), port: parseInt(mjmlConfiguration.smtpPort, 10),
}, },
}); };
if (mjmlConfiguration.smtpUser) {
sendEmailSettings.smtpSettings.auth = {
user: mjmlConfiguration.smtpUser,
pass: mjmlConfiguration.smtpPassword,
};
}
const { response, errors: smtpErrors } = await sendEmailWithSmtp(sendEmailSettings);
if (smtpErrors?.length) { if (smtpErrors?.length) {
return { errors: smtpErrors }; return { errors: smtpErrors };