const nodemailer = require('nodemailer');

class Mail {

    constructor() {
        this.transporter = nodemailer.createTransport({
            host: 'localhost',
            port: 25,
            secure: false
        });

        this.mailOptions = {
            from: 'noreply@server.net',
            to: '',
            subject: '',
            text: ''
        };
    }

    SetMail(mail) {
        this.mailOptions = {
            ...this.mailOptions,
            to: mail.to,
            subject: mail.subject,
            text: mail.text
        };
    }

    async SendMail() {
        try {
            const info = await this.transporter.sendMail(this.mailOptions);
            console.log(`Mail Został wysłany: ${info.response}`);
            return true;
        }
        catch(e) {
            console.error(`Nie udało się wysłać wiadomości email: ${e}`);
            return false;
        }
    }
}

module.exports = new Mail();