const nodemailer = require('nodemailer'); require('dotenv').config(); const transporter = nodemailer.createTransport({ host: process.env.SMTP_HOST || 'smtp.office365.com', port: parseInt(process.env.SMTP_PORT || '587', 10), secure: process.env.SMTP_SECURE === 'true', // true for 465, false for other ports (587 uses STARTTLS) auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS, }, tls: { ciphers: 'SSLv3', // sometimes required for Office365 rejectUnauthorized: false } }); /** * Sends an email using the configured SMTP server. * @param {string} to - Recipient email address * @param {string} subject - Email subject * @param {string} text - Plain text body * @param {string} html - HTML body (optional) * @returns {Promise} Info object from nodemailer */ const sendMail = async (to, subject, text, html = '') => { try { const info = await transporter.sendMail({ from: process.env.SMTP_FROM, to, subject, text, html }); console.log(`Email sent to ${to}: ${info.messageId}`); return info; } catch (error) { console.error('Error sending email:', error); throw error; } }; const sendActivationEmail = async (email, token, baseUrl) => { const activationLink = `${baseUrl}/activate?token=${token}`; const subject = "Activate your Titan 3D Account"; const html = `

Welcome to Titan 3D!

Please click the link below to activate your account:

${activationLink}

If you did not request this, please ignore this email.

`; const text = `Welcome to Titan 3D! Please navigate to the following link to activate your account: ${activationLink}`; return sendMail(email, subject, text, html); }; const sendPasswordResetEmail = async (email, token, baseUrl) => { const resetLink = `${baseUrl}/reset-password?token=${token}`; const subject = "Reset your Titan 3D Password"; const html = `

Password Reset Request

Please click the link below to securely reset your password:

${resetLink}

If you did not request this, please ignore this email.

`; const text = `Please navigate to the following link to reset your password: ${resetLink}`; return sendMail(email, subject, text, html); }; module.exports = { sendMail, sendActivationEmail, sendPasswordResetEmail, transporter };