import nodemailer from 'nodemailer'; import { config } from '../config/config'; export class EmailService { private transporter: nodemailer.Transporter; constructor() { this.transporter = nodemailer.createTransport({ host: config.email.host, port: config.email.port, secure: config.email.port === 465, auth: { user: config.email.user, pass: config.email.password, }, }); } async sendVerificationEmail(to: string, token: string) { const verificationLink = `${config.frontendUrl}/verify/${token}`; const content = `

Please click the button below to verify your email address:

Verify Email

Or copy and paste this link into your browser:

${verificationLink}

This link will expire in 24 hours.

`; const html = this.getHtmlTemplate('Verify your email address', content); await this.sendEmail(to, 'Verify your email', html); } async sendPasswordResetEmail(to: string, token: string) { const resetLink = `${config.frontendUrl}/reset-password/${token}`; const content = `

You requested a password reset. Click the button below to reset your password:

Reset Password

Or copy and paste this link into your browser:

${resetLink}

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

`; const html = this.getHtmlTemplate('Reset your password', content); await this.sendEmail(to, 'Reset your password', html); } async sendContactMessageNotification(data: { name: string; email: string; subject?: string; message: string }) { const content = `

You have received a new contact message from ${data.name} (${data.email}).

Subject: ${data.subject || 'No Subject'}


${data.message}

`; const html = this.getHtmlTemplate('New Contact Message', content); // Send to the configured notification emails const recipients = config.email.contactNotificationEmails; await this.sendEmail(recipients.join(','), `New Contact Message: ${data.subject || 'No Subject'}`, html); } private getHtmlTemplate(title: string, content: string): string { const logoUrl = 'https://res.cloudinary.com/dmxoohiwo/image/upload/v1764315105/logo_fpczsd.png'; const currentYear = new Date().getFullYear(); return ` ${title}
Zemenu Logo

${title}

${content}

© ${currentYear} Zemenu. All rights reserved.

This is an automated message, please do not reply to this email.

`; } private async sendEmail(to: string, subject: string, html: string) { if (!config.email.user || !config.email.password) { console.warn('Email credentials not provided. Skipping email sending.'); console.log(`To: ${to}, Subject: ${subject}`); // For development, we might want to log the link return; } try { await this.transporter.sendMail({ from: config.email.from, to, subject, html, }); console.log(`Email sent to ${to}`); } catch (error) { console.error('Error sending email:', error); throw error; } } } export const emailService = new EmailService();