sms-mail_MainMail.js

const nodemailer = require('nodemailer');
const conf = require("../config");
/**
 * @namespace Mail_SMS
 * @memberOf Myintranet
 */

/**
 * @memberOf Myintranet.Mail_SMS
 */
class MainMail {
    /**
     *
     * @param user
     * @param subject
     * @param mission
     * @param consultant
     * @param employeur
     * @param db
     */
    constructor(user, subject = "Bienvenue à My-intranet", mission, consultant, employeur,db,client,prevOrCra) {
        this.url = conf.siteUrl;
        this.user = user;
        this.mission = mission;
        this.employeur = employeur;
        this.consultant = consultant;
        this.db = db;
        this.subject = subject;
        this.client = client
        this.prevOrCra = prevOrCra

        this.transporter = nodemailer.createTransport({
            host: conf.mail.smtp,
            port: conf.mail.port,
            secure: conf.mail.ssl, // true for 465, false for other ports
            auth: {
                user: conf.mail.username, // generated ethereal user
                pass: conf.mail.password, // generated ethereal password
            }
        });
    }

    /**
     * @returns {*}
     */
    getTo() {
        return this.user.login;
    }
    /**
     * @abstract
     * @returns {string}
     */
    getMailHtml(){
        return "The text to send in the sms"
    }
    /**
     * @abstract
     * @returns {string}
     */
    getHeaderHtml() {
        return `<div  style="text-align: center; width: 100%;">
            <img width="400px" style="width: 400px" src="cid:LOGOMYINTRANET" alt="LogoMyIntranet">
            <h3>${this.subject}</h3>
        </div>`;
    }

    /**
     *
     * @returns {*}
     */
    getMailOption() {
        return this.mailOptions;
    }

    /**
     *
     * @returns {Promise<unknown>}
     */
    async send() {
        this.mailOptions = {
            from: conf.mail.username,
            to: this.getTo(),
            subject:this.subject,
            html: this.getMailHtml(),
            attachments: [{
                filename: 'logo.png',
                path: __dirname + '/imgs/MIlogo.png',
                cid: 'LOGOMYINTRANET' //same cid value as in the html img src
            }]
        };

        return new Promise((resolve, reject) => {
            this.transporter.sendMail(this.getMailOption(), (err, infos) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(infos);
                }
            });
        });
    }
}


module.exports = MainMail;