import * as nodemailer from "nodemailer"; // SMTP configuration from environment variables const smtpConfig = { host: process.env.SMTP_HOST || "smtp.example.com", port: parseInt(process.env.SMTP_PORT || "587"), secure: process.env.SMTP_PORT === "465", // true for 465, false for other ports auth: { user: process.env.SMTP_USER || "", pass: process.env.SMTP_PASSWORD || "", }, }; const fromAddress = process.env.SMTP_FROM || "noreply@example.com"; // Create reusable transporter let transporter: nodemailer.Transporter | null = null; function getTransporter(): nodemailer.Transporter { if (!transporter) { transporter = nodemailer.createTransport(smtpConfig); } return transporter; } // Email templates interface MagicLinkEmailParams { email: string; url: string; token: string; } export async function sendMagicLinkEmail({ email, url, }: MagicLinkEmailParams): Promise { const transport = getTransporter(); const html = ` Sign in to Pulse

Pulse

Sign in to your account

Click the button below to sign in to Pulse. This link will expire in 5 minutes.

Sign in to Pulse

If you didn't request this email, you can safely ignore it.


If the button doesn't work, copy and paste this link into your browser:
${url}

`; const text = ` Sign in to Pulse Click the link below to sign in to your account. This link will expire in 5 minutes. ${url} If you didn't request this email, you can safely ignore it. `; await transport.sendMail({ from: fromAddress, to: email, subject: "Sign in to Pulse", text, html, }); } interface InvitationEmailParams { email: string; inviterName: string; url: string; } export async function sendInvitationEmail({ email, inviterName, url, }: InvitationEmailParams): Promise { const transport = getTransporter(); const html = ` You're invited to Pulse

Pulse

You're invited!

${inviterName} has invited you to join Pulse.

Click the button below to accept the invitation and set up your account.

Accept Invitation

If you weren't expecting this invitation, you can safely ignore this email.


If the button doesn't work, copy and paste this link into your browser:
${url}

`; const text = ` You're invited to Pulse! ${inviterName} has invited you to join Pulse. Click the link below to accept the invitation and set up your account: ${url} If you weren't expecting this invitation, you can safely ignore this email. `; await transport.sendMail({ from: fromAddress, to: email, subject: "You're invited to Pulse", text, html, }); } // Verify SMTP connection export async function verifyEmailConnection(): Promise { try { const transport = getTransporter(); await transport.verify(); return true; } catch (error) { console.error("SMTP connection verification failed:", error); return false; } }