2021-04-12 03:24:17 +00:00
|
|
|
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({
|
2024-02-07 16:23:01 +00:00
|
|
|
host: process.env.EMAIL_HOST,
|
|
|
|
port: process.env.EMAIL_PORT,
|
2021-04-12 03:24:17 +00:00
|
|
|
auth: {
|
2024-02-07 16:23:01 +00:00
|
|
|
user: process.env.EMAIL_FROM,
|
|
|
|
pass: process.env.EMAIL_PASS
|
2021-04-12 03:24:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|