const nodemailer = require('nodemailer'); // Build a transport from the resolved SMTP settings, or null if not configured. function buildTransport(smtp) { if (!smtp.host) return null; const port = Number(smtp.port) || 587; return nodemailer.createTransport({ host: smtp.host, port, secure: smtp.secure === true || smtp.secure === 'true' || port === 465, auth: smtp.user ? { user: smtp.user, pass: smtp.pass } : undefined, }); } // smtp: { host, port, secure, user, pass, from } async function sendPasswordReset(smtp, to, link) { const transport = buildTransport(smtp); if (!transport) throw new Error('SMTP is not configured.'); await transport.sendMail({ from: smtp.from || smtp.user, to, subject: 'Printhive password reset', text: `Reset your Printhive password using this link (valid for 1 hour):\n\n${link}\n\nIf you didn't request this, you can ignore this email.`, html: `

Reset your Printhive password using this link (valid for 1 hour):

${link}

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

`, }); } module.exports = { buildTransport, sendPasswordReset };