// Node.js/Nodemailer configuration for MailHog const nodemailer = require('nodemailer'); // Development configuration (using MailHog) const createDevTransporter = () => { return nodemailer.createTransporter({ host: 'localhost', port: 1025, secure: false, // TLS not required for MailHog auth: false, // No authentication required tls: { rejectUnauthorized: false }, logger: true, // Enable logging for debugging debug: true // Show debug output }); }; // Production configuration (using real SMTP) const createProdTransporter = () => { return nodemailer.createTransporter({ host: process.env.SMTP_HOST, port: parseInt(process.env.SMTP_PORT || '587'), secure: process.env.SMTP_SECURE === 'true', // true for 465, false for other ports auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS }, tls: { rejectUnauthorized: true } }); }; // Get appropriate transporter based on environment const getTransporter = () => { if (process.env.NODE_ENV === 'production') { return createProdTransporter(); } else { return createDevTransporter(); } }; // Email sending function const sendEmail = async (options) => { try { const transporter = getTransporter(); const mailOptions = { from: options.from || process.env.EMAIL_FROM || 'test@sender.local', to: options.to, subject: options.subject, text: options.text, html: options.html, attachments: options.attachments }; const info = await transporter.sendMail(mailOptions); console.log('Email sent successfully:', { messageId: info.messageId, response: info.response, accepted: info.accepted, rejected: info.rejected }); return info; } catch (error) { console.error('Email sending failed:', error); throw error; } }; // Example usage if (require.main === module) { (async () => { try { await sendEmail({ to: 'test@recipient.local', subject: 'Test Email from Nodemailer', text: 'This is a test email sent via Nodemailer', html: '
This email was sent using Nodemailer with MailHog.
' }); console.log('Test email sent successfully'); } catch (error) { console.error('Failed to send test email:', error); } })(); } module.exports = { sendEmail, createDevTransporter, createProdTransporter, getTransporter };