55 lines
2.2 KiB
TypeScript
55 lines
2.2 KiB
TypeScript
import nodemailer from "nodemailer";
|
|
|
|
// Configure transport based on environment
|
|
const transporter = nodemailer.createTransport({
|
|
host: process.env.SMTP_HOST || "localhost",
|
|
port: parseInt(process.env.SMTP_PORT || "1025"),
|
|
secure: process.env.SMTP_SECURE === "true",
|
|
auth: process.env.SMTP_USER && process.env.SMTP_PASS
|
|
? {
|
|
user: process.env.SMTP_USER,
|
|
pass: process.env.SMTP_PASS,
|
|
}
|
|
: undefined, // MailHog doesn't need auth in dev
|
|
});
|
|
|
|
export async function sendVerificationEmail(
|
|
to: string,
|
|
name: string,
|
|
token: string,
|
|
): Promise<boolean> {
|
|
const baseUrl = process.env.APP_URL || `http://localhost:${process.env.PORT || 3000}`;
|
|
const verifyUrl = `${baseUrl}/verify?token=${token}`;
|
|
|
|
const mailOptions: Record<string, unknown> = {
|
|
from: process.env.SMTP_FROM || `"GammaDesk" <noreply@gammanexus.io>`,
|
|
to,
|
|
subject: "Verify your GammaDesk account",
|
|
text: `GammaDesk - Verify Your Account\n\nHello ${name},\n\nThanks for signing up! Please verify your email by visiting:\n\n${verifyUrl}\n\nThis link expires in 24 hours. If you didn't create this account, you can safely ignore this email.`,
|
|
html: `
|
|
<div style="font-family: system-ui, -apple-system, sans-serif; max-width: 480px; margin: 0 auto; padding: 24px;">
|
|
<h1 style="color: #333; margin-bottom: 16px;">Welcome to GammaDesk, ${name}!</h1>
|
|
<p style="color: #666; line-height: 1.6; margin-bottom: 24px;">
|
|
Thanks for signing up. Please verify your email address by clicking the button below.
|
|
</p>
|
|
<a href="${verifyUrl}" style="display: inline-block; padding: 12px 24px; background: #22c55e; color: white; text-decoration: none; border-radius: 8px; font-weight: 600;">
|
|
Verify Email Address
|
|
</a>
|
|
<p style="color: #999; font-size: 12px; margin-top: 24px;">
|
|
This link will expire in 24 hours.<br/>
|
|
If you didn't create this account, you can safely ignore this email.
|
|
</p>
|
|
</div>
|
|
`,
|
|
};
|
|
|
|
try {
|
|
await transporter.sendMail(mailOptions);
|
|
console.log(`[Email] Verification email sent to ${to}`);
|
|
return true;
|
|
} catch (error) {
|
|
console.error("[Email] Failed to send verification email:", error);
|
|
return false;
|
|
}
|
|
}
|