mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-17 16:27:28 +02:00
⚡️ Add support for using SES API (#573)
This commit is contained in:
parent
d5c3017a8b
commit
8ab67683cf
6 changed files with 810 additions and 18 deletions
|
@ -1,3 +1,5 @@
|
|||
import * as aws from "@aws-sdk/client-ses";
|
||||
import { defaultProvider } from "@aws-sdk/credential-provider-node";
|
||||
import { render } from "@react-email/render";
|
||||
import { createTransport, Transporter } from "nodemailer";
|
||||
import React from "react";
|
||||
|
@ -19,22 +21,47 @@ const env = process.env["NODE" + "_ENV"] || "development";
|
|||
let transport: Transporter;
|
||||
|
||||
const getTransport = () => {
|
||||
if (transport) {
|
||||
// Reuse the transport if it exists
|
||||
return transport;
|
||||
}
|
||||
|
||||
if (env === "test") {
|
||||
transport = createTransport({ port: 4025 });
|
||||
} else {
|
||||
const hasAuth = process.env.SMTP_USER || process.env.SMTP_PWD;
|
||||
transport = createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port: process.env.SMTP_PORT ? parseInt(process.env.SMTP_PORT) : undefined,
|
||||
secure: process.env.SMTP_SECURE === "true",
|
||||
auth: hasAuth
|
||||
? {
|
||||
user: process.env.SMTP_USER,
|
||||
pass: process.env.SMTP_PWD,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
return transport;
|
||||
}
|
||||
|
||||
switch (process.env.EMAIL_PROVIDER) {
|
||||
case "ses":
|
||||
{
|
||||
const ses = new aws.SES({
|
||||
region: process.env["AWS" + "_REGION"],
|
||||
credentialDefaultProvider: defaultProvider,
|
||||
});
|
||||
|
||||
transport = createTransport({
|
||||
SES: { ses, aws },
|
||||
});
|
||||
}
|
||||
break;
|
||||
default: {
|
||||
const hasAuth = process.env.SMTP_USER || process.env.SMTP_PWD;
|
||||
transport = createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port: process.env.SMTP_PORT
|
||||
? parseInt(process.env.SMTP_PORT)
|
||||
: undefined,
|
||||
secure: process.env.SMTP_SECURE === "true",
|
||||
auth: hasAuth
|
||||
? {
|
||||
user: process.env.SMTP_USER,
|
||||
pass: process.env.SMTP_PWD,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return transport;
|
||||
};
|
||||
|
||||
|
@ -53,11 +80,12 @@ export const sendEmail = async <T extends TemplateName>(
|
|||
console.info("SUPPORT_EMAIL not configured - skipping email send");
|
||||
return;
|
||||
}
|
||||
|
||||
const transport = getTransport();
|
||||
const Template = templates[templateName] as TemplateComponent<T>;
|
||||
|
||||
try {
|
||||
return await transport.sendMail({
|
||||
await transport.sendMail({
|
||||
from: {
|
||||
name: "Rallly",
|
||||
address: process.env.SUPPORT_EMAIL,
|
||||
|
@ -67,8 +95,9 @@ export const sendEmail = async <T extends TemplateName>(
|
|||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
html: render(<Template {...(options.props as any)} />),
|
||||
});
|
||||
return;
|
||||
} catch (e) {
|
||||
console.error("Error sending email", templateName);
|
||||
console.error("Error sending email", templateName, e);
|
||||
options.onError?.();
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue