import { transporter } from "@/lib/nodemailer"; const escapeHtml = (value: string) => value .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); export const sendPasswordResetEmail = async ( { email, name, resetUrl }: { email: string; name?: string | null; resetUrl: string } ) => { try { if (!process.env.NODEMAILER_EMAIL || !process.env.NODEMAILER_PASSWORD) { throw new Error('Email credentials not configured'); } const firstName = name?.trim().split(' ')[0] || 'there'; const escapedFirstName = escapeHtml(firstName); const escapedResetUrl = escapeHtml(encodeURI(resetUrl)); const html = `

Reset your password

Hi ${escapedFirstName},

We received a request to reset your Openstock password. Use the button below to choose a new one.

Reset password

If you did not request this, you can safely ignore this email.

`; const info = await transporter.sendMail({ from: `"Openstock" <${process.env.NODEMAILER_EMAIL}>`, to: email, subject: 'Reset your Openstock password', text: `Reset your password: ${encodeURI(resetUrl)}`, html, }); console.log('Password reset email sent successfully:', info.messageId); return info; } catch (error) { console.error('Failed to send password reset email:', error); throw error; } };