41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
import nodemailer from 'nodemailer';
|
|
import config from '../config.js';
|
|
|
|
let transporter = null;
|
|
|
|
if (config.email.host) {
|
|
transporter = nodemailer.createTransport({
|
|
host: config.email.host,
|
|
port: config.email.port,
|
|
secure: config.email.secure,
|
|
auth: config.email.user
|
|
? { user: config.email.user, pass: config.email.pass }
|
|
: undefined,
|
|
});
|
|
}
|
|
|
|
export async function sendVerificationEmail(to, link) {
|
|
const subject = 'Verify your Fertig Classic Games account';
|
|
const text = `Welcome! Confirm your email by visiting:\n\n${link}\n\nThis link expires in ${config.email.verificationTtlHours} hour(s).`;
|
|
const html = `<p>Welcome!</p><p>Confirm your email by clicking the link below:</p><p><a href="${link}">${link}</a></p><p>This link expires in ${config.email.verificationTtlHours} hour(s).</p>`;
|
|
|
|
if (!transporter) {
|
|
console.log(`\n[mailer:dev] Verification link for ${to}:\n ${link}\n`);
|
|
return { delivered: false, devLogged: true };
|
|
}
|
|
|
|
try {
|
|
await transporter.sendMail({
|
|
from: config.email.from,
|
|
to,
|
|
subject,
|
|
text,
|
|
html,
|
|
});
|
|
return { delivered: true, devLogged: false };
|
|
} catch (err) {
|
|
console.error(`[mailer] Failed to send verification email to ${to}:`, err);
|
|
return { delivered: false, devLogged: false, error: err.message };
|
|
}
|
|
}
|