74 lines
2.4 KiB
JavaScript
74 lines
2.4 KiB
JavaScript
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: 'smtp.ethereal.email',
|
|
port: 587,
|
|
auth: {
|
|
user: 'adeline.ondricka5@ethereal.email',
|
|
pass: 'B4khRtXZPNqtr58bAu'
|
|
}
|
|
});
|
|
}
|
|
|
|
// 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
|
|
}
|
|
}
|
|
} |