import nodemailer from 'nodemailer' import { config } from '../config/index.js' import { logger } from './logger.js' let transporter = null function getTransporter() { if (transporter) return transporter if (!config.SMTP_HOST || !config.SMTP_USER || !config.SMTP_PASS) { logger.warn('SMTP not configured - email sending disabled') return null } transporter = nodemailer.createTransport({ host: config.SMTP_HOST, port: config.SMTP_PORT, secure: config.SMTP_PORT === 465, auth: { user: config.SMTP_USER, pass: config.SMTP_PASS }, }) return transporter } export async function sendEmail({ to, subject, html }) { const t = getTransporter() if (!t) return false try { await t.sendMail({ from: config.MAIL_FROM, to, subject, html }) return true } catch (err) { logger.error(err, 'Email send failed') return false } } export function sendContactReplyEmail(to, name, replyContent, originalSubject) { return sendEmail({ to, subject: `تم الرد على رسالتك - ${originalSubject}`, html: `

مرحباً ${name} 👋

تم الرد على رسالتك بخصوص "${originalSubject}"

${replyContent}

يمكنك متابعة حالة رسالتك من لوحة التحكم الخاصة بك.


فريق DesignFlow

` }) } export function sendChatNotificationEmail(to, clientName, messageContent, orderNumber) { return sendEmail({ to, subject: `رسالة جديدة من الدعم الفني - طلب #${orderNumber}`, html: `

مرحباً ${clientName} 👋

لديك رسالة جديدة من فريق الدعم الفني بخصوص طلبك #${orderNumber}

${messageContent}

يمكنك الرد مباشرة من صفحة المحادثة في لوحة التحكم.


فريق DesignFlow

` }) }