Implement dynamic SMTP setting loader, admin product dependency matrix UI, recursive deselect cascade, and backend catalog validation
All checks were successful
Staging Build / build (push) Successful in 2m19s
All checks were successful
Staging Build / build (push) Successful in 2m19s
This commit is contained in:
@@ -1,22 +1,9 @@
|
||||
// utils/mail.ts – simple wrapper around nodemailer
|
||||
import nodemailer from 'nodemailer';
|
||||
|
||||
// Load SMTP configuration from environment variables
|
||||
const smtpConfig = {
|
||||
host: process.env.SMTP_HOST,
|
||||
port: Number(process.env.SMTP_PORT),
|
||||
secure: process.env.SMTP_SECURE === 'true' || process.env.SMTP_SECURE === '1', // true for 465, false for other ports
|
||||
auth: {
|
||||
user: process.env.SMTP_USER,
|
||||
pass: process.env.SMTP_PASS,
|
||||
},
|
||||
};
|
||||
|
||||
// Create a reusable transporter. If any required var is missing, nodemailer will throw on send.
|
||||
const transporter = nodemailer.createTransport(smtpConfig);
|
||||
import { createAdminClient } from '@/lib/supabase/admin';
|
||||
|
||||
/**
|
||||
* Send an email.
|
||||
* Send an email using database-configured SMTP settings (or environment variable fallback).
|
||||
* @param to Recipient address
|
||||
* @param subject Subject line
|
||||
* @param text Plain‑text body
|
||||
@@ -33,12 +20,46 @@ export async function sendMail({
|
||||
text: string;
|
||||
html?: string;
|
||||
}) {
|
||||
// Query custom SMTP settings from database
|
||||
const supabase = createAdminClient();
|
||||
const { data: dbSettings } = await supabase
|
||||
.from('settings')
|
||||
.select('*')
|
||||
.eq('id', 'smtp')
|
||||
.maybeSingle();
|
||||
|
||||
// Resolve config from DB or environment variables
|
||||
const host = dbSettings?.host || process.env.SMTP_HOST;
|
||||
const port = dbSettings?.port ? Number(dbSettings.port) : Number(process.env.SMTP_PORT);
|
||||
const secure = dbSettings
|
||||
? !!dbSettings.secure
|
||||
: (process.env.SMTP_SECURE === 'true' || process.env.SMTP_SECURE === '1');
|
||||
const user = dbSettings?.user || process.env.SMTP_USER;
|
||||
const pass = dbSettings?.pass || process.env.SMTP_PASS;
|
||||
|
||||
if (!host || !user) {
|
||||
throw new Error('SMTP host and user must be configured (either in settings database table or environment variables).');
|
||||
}
|
||||
|
||||
// Create transporter dynamically on send request
|
||||
const transporter = nodemailer.createTransport({
|
||||
host,
|
||||
port,
|
||||
secure,
|
||||
auth: {
|
||||
user,
|
||||
pass,
|
||||
},
|
||||
});
|
||||
|
||||
const info = await transporter.sendMail({
|
||||
from: smtpConfig.auth.user,
|
||||
from: user,
|
||||
to,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
});
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user