מה רמת הקוד ומה הוא עושה :import { Router } from 'express';
import { MailService } from '../services/mailRegister';

const router = Router();
const mailService = new MailService();

router.post('/', async (req, res) => {
    try {
        const name = "Gili Zaydorf"; // You can replace this with req.body.name if you want to get it from the request body
        const email = "tichnut59@8547900.org.il";
        const subject = "ברוך הבא למערכת!";
        const text = `שלום ${name},\nנרשמת בהצלחה למערכת שלנו! ברוך הבא!`;

        await mailService.sendMail({ to: email, subject, text,name });

        res.status(200).json({ message: 'Registration successful, email sent!' });
    } catch (error) {
        console.error('Error sending registration email:', error);
        res.status(500).json({ error: 'Failed to send email' });
    }
});

export default router;                                                                                                                                                                      and function : import fs from 'fs';
import path from 'path';
const nodemailer = require('nodemailer');

export interface MailOptions {
    name: string;
    to: string;
    subject: string;
    text: string;
    html?: string;
}

export class MailService {
    private outputDir: string;

    constructor(outputDir = path.resolve(process.cwd(), 'emails_out')) {
        this.outputDir = outputDir;
        if (!fs.existsSync(this.outputDir)) {
            fs.mkdirSync(this.outputDir, { recursive: true });
        }
    }


    public async sendMail(options: MailOptions): Promise<void> {
        const htmlContent = options.html ?? `
      <html>
      <body style="font-family: Guttman Yad-Brush, sans-serif; color: #0000CC;font-size: 30px;background-color: #f5f5f5db;">
        <h1 style="color: #b6b6b6c5;">Hello ${options.name}!!!</h1>
        <p>נרשמת בהצלחה למערכת שלנו! ברוך הבא!</p>
        <p>אנחנו שמחים שבחרת להצטרף אלינו.</p>
        <p>אם יש לך שאלות, אל תהסס לפנות אלינו.</p>
    
        </body>
    </html>
    `;

        if (process.env.NODE_ENV === 'production') {
            console.log("Current NODE_ENV:", process.env.NODE_ENV);
            const transporter = nodemailer.createTransport({
                service: 'gmail',
                auth: {
                    user: process.env.GMAIL_USER,
                    pass: process.env.GMAIL_APP_PASSWORD,
                },
            });

            const mailOptions = {
                from: process.env.GMAIL_APP_PASSWORD,
                to: options.to,
                subject: options.subject,
                text: options.text,
                html: htmlContent,
            };

            try {
                const info = await transporter.sendMail(mailOptions);
                console.log(`Email sent: ${info.response}`);
            } catch (error) {
                console.error('Error sending email:', error);
            }
        }

        else {
            console.log("!Current NODE_ENV:", process.env.NODE_ENV);
            const now = new Date();

            const hours = String(now.getHours()).padStart(2, '0');
            const minutes = String(now.getMinutes()).padStart(2, '0');
            const seconds = String(now.getSeconds()).padStart(2, '0');
            const timePart = `${hours}-${minutes}-${seconds}`;

            const day = String(now.getDate()).padStart(2, '0');
            const month = String(now.getMonth() + 1).padStart(2, '0');
            const year = now.getFullYear();
            const datePart = `${day}_${month}_${year}`;

            const safeTo = options.to
            const safeSubject = options.subject.replace(/[^a-zA-Z0-9-_]/g, '');

            const filename = `${timePart} ${datePart} ${safeTo}_${safeSubject}.html`;
            const filepath = path.join(this.outputDir, filename);

            await fs.promises.writeFile(filepath, htmlContent, { encoding: 'utf-8' });
            console.log(`Email saved as HTML file:\n ${filepath}`);
        }
    }
}