Source: utils/mailUtils.js

/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2025 Rainbownapkin and the TTN Community

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.*/

//Config
const config = require('../../config.json');

//NPM imports
const nodeMailer = require("nodemailer");

//Setup mail transport
/**
 * nodemailer transport object, generated from options specific in our config file
 */
const transporter = nodeMailer.createTransport({
    host: config.mail.host,
    port: config.mail.port,
    secure: config.mail.secure,
    auth: {
        user: config.mail.address,
        pass: config.mail.pass
    }
});

/**
 * Sends an email as tokebot to the requested user w/ the requested body and signature
 * @param {String} to - String containing the email address to send to
 * @param {String} subject - Subject line of the email to send
 * @param {String} body - Body contents, either HTML or Plaintext
 * @param {Boolean} htmlBody - Whether or not Body contents should be sent as HTML or Plaintext
 * @returns {Object} Sent mail info
 */
module.exports.mailem = async function(to, subject, body, htmlBody = false){
    //Create mail object
    const mailObj = {
        from: `"Tokebot🤖💨"<${config.mail.address}>`,
        to,
        subject
    };

    //If we're sending HTML
    if(htmlBody){
        //set body as html
        mailObj.html = body;
    //If we're sending plaintext
    }else{
        //Set body as plaintext
        mailObj.text = body
    }

    //Send mail based on mail object
    const sentMail = await transporter.sendMail(mailObj);

    //return the mail info
    return sentMail;
}

/**
 * Sends address verification email
 * @param {Mongoose.Document} requestDB - DB Document Object for the current email change request token
 * @param {Mongoose.Document} userDB - DB Document Object for the user we're verifying email against
 * @param {String} newEmail - New email address to send to
 */
module.exports.sendAddressVerification = async function(requestDB, userDB, newEmail){
    //Send the reset url via email
    await module.exports.mailem(
        newEmail,
        `Email Change Request - ${userDB.user}`,
        `<h1>Email Change Request</h1>
        <p>A request to change the email associated with the ${config.instanceName} account '${userDB.user}' to this address has been requested.<br>
        <a href="${requestDB.getChangeURL()}">Click here</a> to confirm this change.</p>
        <sup>If you received this email without request, feel free to ignore and delete it! -Tokebot</sup>`,
        true
    );

    //If the user has a pre-existing email address
    if(userDB.email != null && userDB.email != ""){
        await module.exports.mailem(
            userDB.email,
            `Email Change Request - ${userDB.user}`,
            `<h1>Email Change Request Notification</h1>
            <p>A request to change the email associated with the ${config.instanceName} account '${userDB.user}' to another address has been requested.<br>
            <sup>If you received this email without request, you should <strong>immediately</strong> change your password and contact the server adminsitrator! -Tokebot</sup>`,
            true
        );
    }
}