import nodemailer from 'nodemailer' class Mailer { constructor () { this._transport = null this._isTest = false this._isDisabled = false } async init (options = null, mode = null) { let transportOptions = options if (mode === null) this._isDisabled = true const isEmpty = !options?.host || !options?.port || !options?.user || !options?.host if (isEmpty || !options) { const account = await nodemailer.createTestAccount() transportOptions = { host: 'smtp.ethereal.email', port: 587, secure: false, auth: { user: account.user, pass: account.pass } } this._isTest = true } this._transport = nodemailer.createTransport(transportOptions) } async sendMail (to, subject, message, html = null) { try { if (!this._transport) throw new Error('Call Mailer.init()') const mail = { from: 'sender@example.com', // DEVINFO Hier die entsprechende Absenderadresse einfügen to, subject, text: message } if (this._isDisabled) { mail.from = 'sender@example.com' mail.to = 'receiver@example.com' mail.subject = 'Test Message Subject' mail.text = 'Test Message Body' } const info = await this._transport.sendMail(mail) console.log('LOG Message:', info) if (this._isTest) { console.log('LOG Test Account URL:', nodemailer.getTestMessageUrl(info)) } } catch (error) { console.log('ERROR:', error.name) } } } export default Mailer