import nodemailer from 'nodemailer' interface EmailOptions { to: string | string[] subject: string text?: string html?: string from?: string replyTo?: string } interface SendEmailResult { success: boolean messageId?: string error?: string } class SMTPService { private transporter: any constructor() { // Configure nodemailer to use local Maddy SMTP server this.transporter = nodemailer.createTransport({ host: 'localhost', port: 25, secure: false, // Use STARTTLS tls: { rejectUnauthorized: false // Accept self-signed certificates }, // Maddy accepts mail on port 25 without authentication for localhost requireTLS: false }) } async sendEmail(options: EmailOptions): Promise { try { const mailOptions = { from: options.from || 'Biblical Guide ', to: Array.isArray(options.to) ? options.to.join(', ') : options.to, subject: options.subject, text: options.text, html: options.html, replyTo: options.replyTo } const info = await this.transporter.sendMail(mailOptions) return { success: true, messageId: info.messageId } } catch (error) { console.error('SMTP send error:', error) return { success: false, error: error instanceof Error ? error.message : 'Unknown error' } } } async sendContactForm(data: { name: string email: string subject: string message: string }): Promise { const html = `

New Contact Form Submission

Name: ${data.name}

Email: ${data.email}

Subject: ${data.subject}

Message:

${data.message.replace(/\n/g, '
')}
` const text = ` New Contact Form Submission Name: ${data.name} Email: ${data.email} Subject: ${data.subject} Message: ${data.message} ` return this.sendEmail({ to: 'contact@biblical-guide.com', subject: `Contact Form: ${data.subject}`, html, text, replyTo: data.email }) } async testConnection(): Promise<{ success: boolean; error?: string }> { try { await this.transporter.verify() return { success: true } } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Connection failed' } } } } export const smtpService = new SMTPService()