Repository: louislam/uptime-kuma
File: server/notification-providers/smtp.js (JavaScript)

Code before:
const { DOWN, UP } = require("../../src/util");

class SMTP extends NotificationProvider {

    name = "smtp";

    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {

        const config = {
            host: notification.smtpHost,
            port: notification.smtpPort,
            secure: notification.smtpSecure,
        };

        // Should fix the issue in https://github.com/louislam/uptime-kuma/issues/26#issuecomment-896373904
        if (notification.smtpUsername || notification.smtpPassword) {
            config.auth = {
                user: notification.smtpUsername,
                pass: notification.smtpPassword,
            };
        }
        // Lets start with default subject
        let subject = msg;

        // Our subject cannot end with whitespace it's often raise spam score
        let customsubject = notification.customsubject.trim()

        // If custom subject is not empty, change subject for notification
        if (customsubject !== "") {

            // Replace "MACROS" with coresponding variable
            let replaceName = new RegExp("{NAME}", "g");
            let replaceHostname = new RegExp("{HOSTNAME}", "g");
            let replaceStatus = new RegExp("{STATUS}", "g");

            // Lets start with dummy values to simplify code
            let monitorName = "Test"
            let monitorHostname = "example.com"
            let serviceStatus = "⚠️ Test";

            if (monitorJSON !== null) {
                monitorName = monitorJSON["name"];
                monitorHostname = monitorJSON["hostname"];
            }

            if (heartbeatJSON !== null) {
                serviceStatus = heartbeatJSON["status"] == DOWN ? "🔴 Down" : "✅ Up";
            }
            
            // Break replace to one by line for better readability
            customsubject = customsubject.replace(replaceStatus, serviceStatus);

Review comment (suggestion):
- I believe that customsubject could be undefined.
- camelCase is preferred here. [Read more](https://github.com/louislam/uptime-kuma/blob/master/CONTRIBUTING.md#name-convention).

Code after:
const { DOWN, UP } = require("../../src/util");

class SMTP extends NotificationProvider {

    name = "smtp";

    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {

        const config = {
            host: notification.smtpHost,
            port: notification.smtpPort,
            secure: notification.smtpSecure,
        };

        // Should fix the issue in https://github.com/louislam/uptime-kuma/issues/26#issuecomment-896373904
        if (notification.smtpUsername || notification.smtpPassword) {
            config.auth = {
                user: notification.smtpUsername,
                pass: notification.smtpPassword,
            };
        }
        // Lets start with default subject and empty string for custom one
        let subject = msg;

        // Change the subject if:
        //     - The msg ends with "Testing" or
        //     - Actual Up/Down Notification
        if ((monitorJSON && heartbeatJSON) || msg.endsWith("Testing")) {
            let customSubject = "";

            // Our subject cannot end with whitespace it's often raise spam score
            // Once I got "Cannot read property 'trim' of undefined", better be safe than sorry
            if (notification.customSubject) {
                customSubject = notification.customSubject.trim();
            }

            // If custom subject is not empty, change subject for notification
            if (customSubject !== "") {

                // Replace "MACROS" with corresponding variable
                let replaceName = new RegExp("{{NAME}}", "g");
                let replaceHostnameOrURL = new RegExp("{{HOSTNAME_OR_URL}}", "g");
                let replaceStatus = new RegExp("{{STATUS}}", "g");

                // Lets start with dummy values to simplify code
                let monitorName = "Test";
                let monitorHostnameOrURL = "testing.hostname";
                let serviceStatus = "⚠️ Test";

                if (monitorJSON !== null) {
                    monitorName = monitorJSON["name"];