const nodemailer = require('nodemailer'); const {Service} = require("@hapipal/schmervice"); // Fonction pour créer un transporteur SMTP réutilisable module.exports = class EmailService extends Service { createTransporter() { // Crée un transporteur SMTP réutilisable return nodemailer.createTransport({ host: process.env.EMAIL_HOST, port: process.env.EMAIL_PORT, auth: { user: process.env.EMAIL_FROM, pass: process.env.EMAIL_PASS } }); } // Fonction pour envoyer un e-mail async sendEmail(email, firstName, lastName, subject, html) { // Crée un transporteur SMTP réutilisable const transporter = this.createTransporter(); // Paramètres de l'e-mail const mailOptions = { from: process.env.EMAIL_FROM, to: email, subject: subject, html: html }; // Envoyer l'e-mail try { const info = await transporter.sendMail(mailOptions); console.log('Message sent: %s', info.messageId); console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info)); // For testing with Ethereal return info; } catch (error) { console.error('Error sending email:', error); throw error; // Propagate the error } } async sendEmailWithAttachment(email, firstName, lastName, subject, html, attachment) { // Crée un transporteur SMTP réutilisable const transporter = this.createTransporter(); // Paramètres de l'e-mail const mailOptions = { from: process.env.EMAIL_FROM, to: email, subject: subject, html: html, attachments: [ { filename: 'movies.csv', content: attachment } ] }; // Envoyer l'e-mail try { const info = await transporter.sendMail(mailOptions); console.log('Message sent: %s', info.messageId); console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info)); // For testing with Ethereal return info; } catch (error) { console.error('Error sending email:', error); throw error; // Propagate the error } } }