Updated mailer to use Mailtrap API
This commit is contained in:
@@ -1,29 +1,24 @@
|
||||
import nodemailer from 'nodemailer';
|
||||
import { MailtrapTransport } from 'mailtrap';
|
||||
|
||||
const TOKEN = Bun.env.MAILTRAP_API_TOKEN;
|
||||
|
||||
let cachedTransporter: ReturnType<typeof nodemailer.createTransport> | null = null;
|
||||
|
||||
function getTransporter() {
|
||||
if (cachedTransporter) return cachedTransporter;
|
||||
|
||||
const smtpHost = Bun.env.SMTP_HOST;
|
||||
const smtpPort = Number(Bun.env.SMTP_PORT ?? 587);
|
||||
const smtpUser = Bun.env.SMTP_USER;
|
||||
const smtpPass = Bun.env.SMTP_PASS;
|
||||
|
||||
if (!smtpHost || !smtpUser || !smtpPass) {
|
||||
throw new Error('Missing SMTP env vars. Set SMTP_HOST, SMTP_PORT, SMTP_USER and SMTP_PASS.');
|
||||
if (!TOKEN) {
|
||||
throw new Error('Missing MAILTRAP_API_TOKEN env var.');
|
||||
}
|
||||
|
||||
cachedTransporter = nodemailer.createTransport({
|
||||
host: smtpHost,
|
||||
port: smtpPort,
|
||||
secure: smtpPort === 465,
|
||||
auth: {
|
||||
user: smtpUser,
|
||||
pass: smtpPass,
|
||||
},
|
||||
});
|
||||
|
||||
cachedTransporter = nodemailer.createTransport(
|
||||
MailtrapTransport({
|
||||
token: TOKEN,
|
||||
sandbox: Bun.env.MAIL_SANDBOX === 'true',
|
||||
testInboxId: 1114587
|
||||
})
|
||||
);
|
||||
return cachedTransporter;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user