56 lines
1.6 KiB
JavaScript
56 lines
1.6 KiB
JavaScript
const nodemailer = require('nodemailer');
|
|
const defaultEmailConfig = require('./config').defaultEmailConfig
|
|
|
|
class EmailSender {
|
|
/**
|
|
* Konstruktor initialisiert Transport-Konfiguration.
|
|
* @param {object} config - Konfiguration für den SMTP-Transport.
|
|
*/
|
|
constructor(config = undefined) {
|
|
|
|
if (!config) {
|
|
config = defaultEmailConfig
|
|
}
|
|
|
|
this.transporter = nodemailer.createTransport({
|
|
host: config.host,
|
|
port: config.port,
|
|
secure: config.secure, // true für 465, false für andere Ports
|
|
auth: {
|
|
user: config.user,
|
|
pass: config.pass
|
|
}
|
|
});
|
|
this.from = config.from;
|
|
}
|
|
|
|
/**
|
|
* Sendet eine E-Mail.
|
|
* @param {object} options - Details zur E-Mail.
|
|
* @param {string} options.to - Empfängeradresse.
|
|
* @param {string} options.subject - Betreff der E-Mail.
|
|
* @param {string} options.text - Nur-Text-Version.
|
|
* @param {string} [options.html] - HTML-Version (optional).
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async sendMail({ to, subject, text, html }) {
|
|
try {
|
|
const info = await this.transporter.sendMail({
|
|
from: this.from,
|
|
to,
|
|
subject,
|
|
text,
|
|
html
|
|
});
|
|
|
|
console.log('E-Mail gesendet:', info.messageId);
|
|
} catch (error) {
|
|
console.error('Fehler beim Senden der E-Mail:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = EmailSender;
|