import nodemailer from 'nodemailer'; // Email configuration // Required environment variables: // - SMTP_HOST: Your SMTP server host (e.g. smtp.gmail.com) // - SMTP_PORT: Your SMTP server port (e.g. 587 for TLS, 465 for SSL) // - SMTP_USER: Your SMTP server username/email // - SMTP_PASSWORD: Your SMTP server password or app password // - EMAIL_FROM: The email address that will appear as sender const smtpConfig = { host: process.env.SMTP_HOST, 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, }, }; // For development, if SMTP credentials aren't provided, log messages instead of sending const devMode = process.env.NODE_ENV !== 'production' && (!process.env.SMTP_HOST || !process.env.SMTP_USER || !process.env.SMTP_PASSWORD); // Create a reusable transporter object let transporter: nodemailer.Transporter; // Initialize the transporter export const initializeEmailTransporter = () => { if (devMode) { console.log('Email dev mode active: emails will be logged instead of sent'); // Use Nodemailer's testing account for development return nodemailer.createTransport({ jsonTransport: true // This outputs the email to the console instead of sending it }); } if (!transporter) { transporter = nodemailer.createTransport(smtpConfig); } return transporter; }; // Send location sharing email export const sendLocationSharingEmail = async ({ to, senderName, locationUrl, expiryTime, }: { to: string; senderName: string; locationUrl: string; expiryTime?: Date; }) => { const emailTransporter = initializeEmailTransporter(); const expiryMessage = expiryTime ? `This location share will expire on ${expiryTime.toLocaleString()}.` : 'This location share does not expire.'; const mailOptions = { from: process.env.EMAIL_FROM || `"Location Tracker" <${process.env.SMTP_USER || 'noreply@locationtracker.com'}>`, to, subject: `${senderName} shared their location with you`, text: ` ${senderName} has shared their real-time location with you. Click the link below to view their current location: ${locationUrl} ${expiryMessage} This is an automated message, please do not reply. `, html: `

Location Shared with You

${senderName} has shared their real-time location with you.

View Location

${expiryMessage}


This is an automated message from Real-Time Location Tracker. Please do not reply.

`, }; try { if (devMode) { // In dev mode, log the email details instead of sending console.log('DEV MODE: Email would be sent with the following details:'); console.log('To:', to); console.log('Subject:', mailOptions.subject); console.log('Location URL:', locationUrl); return { success: true, messageId: 'dev-mode-email', devMode: true }; } const info = await emailTransporter.sendMail(mailOptions); return { success: true, messageId: info.messageId }; } catch (error) { console.error('Error sending email:', error); throw new Error('Failed to send email. Please check your SMTP settings.'); } };