RealBlocks / server /src /services /email.ts
incognitolm's picture
Initial
ce2d6ca verified
Raw
History Blame Contribute Delete
2.17 kB
import nodemailer from 'nodemailer';
import { config } from '../config';
let transporter: nodemailer.Transporter | null = null;
function getTransporter(): nodemailer.Transporter {
if (!transporter) {
if (!config.smtp.host || !config.smtp.user) {
console.warn('SMTP not configured. Email sending disabled.');
return null as any;
}
transporter = nodemailer.createTransport({
host: config.smtp.host,
port: config.smtp.port,
secure: config.smtp.secure,
auth: {
user: config.smtp.user,
pass: config.smtp.pass,
},
});
}
return transporter;
}
export async function sendVerificationEmail(to: string, token: string): Promise<void> {
const transport = getTransporter();
if (!transport) {
console.log(`[DEV] Verification email to ${to}: ${token}`);
return;
}
const verificationUrl = `${config.corsOrigin}/verify-email?token=${token}`;
await transport.sendMail({
from: config.smtp.from,
to,
subject: 'Verify your RealBlocks account',
html: `
<h1>Welcome to RealBlocks!</h1>
<p>Click the link below to verify your email address:</p>
<a href="${verificationUrl}" style="display:inline-block;padding:12px 24px;background:#4f46e5;color:#fff;text-decoration:none;border-radius:6px;">Verify Email</a>
<p>This link expires in 24 hours.</p>
`,
});
}
export async function sendPasswordResetEmail(to: string, token: string): Promise<void> {
const transport = getTransporter();
if (!transport) {
console.log(`[DEV] Password reset email to ${to}: ${token}`);
return;
}
const resetUrl = `${config.corsOrigin}/reset-password?token=${token}`;
await transport.sendMail({
from: config.smtp.from,
to,
subject: 'Reset your RealBlocks password',
html: `
<h1>Password Reset Request</h1>
<p>Click the link below to reset your password:</p>
<a href="${resetUrl}" style="display:inline-block;padding:12px 24px;background:#4f46e5;color:#fff;text-decoration:none;border-radius:6px;">Reset Password</a>
<p>This link expires in 1 hour. If you didn't request this, ignore this email.</p>
`,
});
}