@davoodmehraban89 / LifeMate / commits / bffc3a8
Fork 0

LifeMate — Personal, Family & Learning Companion

feat: support SMTP transactional email in staging

davoodmehraban89 committed Oct 5, 2026 at 07:53 UTC bffc3a8ec67a817b475317dd38c0de838849765f
1 file changed +37 -13
backend/src/server.js
+37 -13
@@ -3,6 +3,7 @@ import express from 'express';
3 import argon2 from 'argon2';
4 import jwt from 'jsonwebtoken';
5 import pg from 'pg';
6 +import nodemailer from 'nodemailer';
7
8 const { Pool } = pg;
9 const app = express();
@@ -21,20 +22,43 @@ const testMode = process.env.NODE_ENV === 'test';
22
23 async function sendTransactionalEmail({ to, subject, text }) {
24 if (testMode) return { delivered: false, testMode: true };
24 - const apiKey = process.env.RESEND_API_KEY;
25 +
26 const from = process.env.EMAIL_FROM;
26 - if (!apiKey || !from) return { delivered: false, reason: 'provider_not_configured' };
27 -
28 - const response = await fetch('https://api.resend.com/emails', {
29 - method: 'POST',
30 - headers: {
31 - Authorization: `Bearer ${apiKey}`,
32 - 'Content-Type': 'application/json',
33 - },
34 - body: JSON.stringify({ from, to: [to], subject, text }),
35 - });
36 - if (!response.ok) throw new Error(`email_delivery_failed:${response.status}`);
37 - return { delivered: true };
27 + if (!from) return { delivered: false, reason: 'provider_not_configured' };
28 +
29 + const apiKey = process.env.RESEND_API_KEY;
30 + if (apiKey) {
31 + const response = await fetch('https://api.resend.com/emails', {
32 + method: 'POST',
33 + headers: {
34 + Authorization: `Bearer ${apiKey}`,
35 + 'Content-Type': 'application/json',
36 + },
37 + body: JSON.stringify({ from, to: [to], subject, text }),
38 + });
39 + if (!response.ok) throw new Error(`email_delivery_failed:${response.status}`);
40 + return { delivered: true, provider: 'resend' };
41 + }
42 +
43 + const smtpHost = process.env.SMTP_HOST;
44 + const smtpPort = Number(process.env.SMTP_PORT ?? 0);
45 + if (smtpHost && smtpPort > 0) {
46 + const transporter = nodemailer.createTransport({
47 + host: smtpHost,
48 + port: smtpPort,
49 + secure: process.env.SMTP_SECURE === 'true',
50 + auth: process.env.SMTP_USER
51 + ? {
52 + user: process.env.SMTP_USER,
53 + pass: process.env.SMTP_PASSWORD,
54 + }
55 + : undefined,
56 + });
57 + await transporter.sendMail({ from, to, subject, text });
58 + return { delivered: true, provider: 'smtp' };
59 + }
60 +
61 + return { delivered: false, reason: 'provider_not_configured' };
62 }
63
64 const issueAccess = (userId, sessionId) =>