Updated mailer to use Mailtrap API
This commit is contained in:
@@ -11,4 +11,6 @@ SMTP_USER=6af2bf58132d12
|
||||
SMTP_PASS=1253b2fe0fc47a
|
||||
SMTP_FROM=Playzer <no-reply@playzer.app>
|
||||
BETTER_AUTH_SECRET=R1MfeeeekXSNdE65hOhhr0Mt0KLwczYr
|
||||
BETTER_AUTH_URL=http://localhost:3000
|
||||
BETTER_AUTH_URL=http://localhost:3000
|
||||
MAILTRAP_API_TOKEN=2c040e2e577dc48909d3206dd2d47fc6
|
||||
MAIL_SANDBOX=true
|
||||
1
apps/backend/.gitignore
vendored
1
apps/backend/.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
# deps
|
||||
node_modules/
|
||||
prisma.config.prod.ts
|
||||
@@ -21,6 +21,7 @@
|
||||
"better-auth": "^1.6.4",
|
||||
"dotenv": "^17.4.1",
|
||||
"hono": "4.12.10",
|
||||
"mailtrap": "^4.5.1",
|
||||
"nodemailer": "^8.0.5",
|
||||
"pg": "^8.20.0",
|
||||
"pino": "10.3.1",
|
||||
|
||||
@@ -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