From 748d7f0323a28f0c976813a27cf72b8d744bdc48 Mon Sep 17 00:00:00 2001 From: arsam Date: Fri, 18 Sep 2026 23:19:15 +0330 Subject: [PATCH] Import source: app/backend/src/lib/email Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality. --- app/backend/src/lib/email.ts | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 app/backend/src/lib/email.ts diff --git a/app/backend/src/lib/email.ts b/app/backend/src/lib/email.ts new file mode 100644 index 0000000..3d94f19 --- /dev/null +++ b/app/backend/src/lib/email.ts @@ -0,0 +1,43 @@ +// Email stub — sends via SMTP when SMTP_HOST is configured; otherwise logs to console. +// Drop-in for production: set SMTP_HOST/PORT/USER/PASS/FROM in .env.prod. + +interface MailOptions { + to: string; + subject: string; + html: string; +} + +async function sendEmail(opts: MailOptions): Promise { + const host = process.env.SMTP_HOST; + if (!host) { + // SMTP not configured — log so the operator can see what would have been sent. + console.info('[email:stub] Would send to', opts.to, '|', opts.subject); + return; + } + + // Lazy-load nodemailer only when SMTP is configured to keep the cold-start fast. + const nodemailer = await import('nodemailer').catch(() => null); + if (!nodemailer) { + console.warn('[email] nodemailer not installed — install with: cd backend && npm install nodemailer @types/nodemailer'); + return; + } + + const transport = nodemailer.createTransport({ + host, + port: Number(process.env.SMTP_PORT ?? 587), + secure: Number(process.env.SMTP_PORT ?? 587) === 465, + auth: { + user: process.env.SMTP_USER, + pass: process.env.SMTP_PASS, + }, + }); + + await transport.sendMail({ + from: process.env.SMTP_FROM ?? 'no-reply@teknav.ir', + to: opts.to, + subject: opts.subject, + html: opts.html, + }); +} + +export { sendEmail };