Files
myeasycms-v2/packages/mailers/src/index.ts
giancarlo f7ded6f789 Add support for Resend mailer
This update expands the mailer support by adding the Resend mailer as an alternative to NodeMailer and Cloudflare Mailer. After setting the environment variable 'MAILER_PROVIDER' to 'resend', users can utilize the Resend HTTP API for mailing. Necessary instructions and variable settings regarding the usage of Resend mailer have also been added to README.md and related modules.
2024-04-17 18:42:00 +08:00

39 lines
981 B
TypeScript

import { z } from 'zod';
const MAILER_PROVIDER = z
.enum(['nodemailer', 'cloudflare', 'resend'])
.default('nodemailer')
.parse(process.env.MAILER_PROVIDER);
/**
* @description Get the mailer based on the environment variable.
*/
export async function getMailer() {
switch (process.env.MAILER_PROVIDER as typeof MAILER_PROVIDER) {
case 'nodemailer': {
if (process.env.NEXT_RUNTIME !== 'edge') {
const { Nodemailer } = await import('./impl/nodemailer');
return new Nodemailer();
} else {
throw new Error('Nodemailer is not available on the edge runtime side');
}
}
case 'cloudflare': {
const { CloudflareMailer } = await import('./impl/cloudflare');
return new CloudflareMailer();
}
case 'resend': {
const { ResendMailer } = await import('./impl/resend');
return new ResendMailer();
}
default:
throw new Error(`Invalid mailer: ${MAILER_PROVIDER as string}`);
}
}