Updated mailer to use Mailtrap API

This commit is contained in:
Jose Selesan
2026-04-17 15:23:51 -03:00
parent b3f1b72da4
commit a1ff9f2831
5 changed files with 20 additions and 18 deletions

View File

@@ -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;
}