Files
webshop/shop/utils/mail.ts
DanielS 4402d4ec64
Some checks failed
Staging Build / build (push) Failing after 25s
Add SMTP mail utility, admin settings page, and API routes for SMTP configuration and test email
2026-06-23 00:17:41 +02:00

45 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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);
/**
* Send an email.
* @param to Recipient address
* @param subject Subject line
* @param text Plaintext body
* @param html Optional HTML body
*/
export async function sendMail({
to,
subject,
text,
html,
}: {
to: string;
subject: string;
text: string;
html?: string;
}) {
const info = await transporter.sendMail({
from: smtpConfig.auth.user,
to,
subject,
text,
html,
});
return info;
}