sendEmailNN.js 1.64 KB
const APIKEY = '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'

let emailOption = {
    "options": {
        "auth": {
            "api_key": APIKEY
        }
    },
    "from": "no-reply@sourcecode.co.th",
    "subject": "Test send mail",
    "html": "./conf/emailTemplate.txt"
}

sendEmail = function (emailTo) {
    const nodemailer = require('nodemailer');
    const sendgridTransport = require('nodemailer-sendgrid-transport');

    /* ----------------------------- constant value ----------------------------- */
    const sendEmail = emailOption

    // Create a SendGrid transporter using your API key-
    const transporter = nodemailer.createTransport(
        sendgridTransport(sendEmail.options)
        // {
        //     host: 'smtp.office365.com',
        //     port: 587,
        //     secure: false, // TLS requires secureConnection to be false
        //     auth: {
        //         user: 'nattaponw@sourcecode.co.th',
        //         pass: 'Dkigiupo035'
        //     }
        // }
    );

    // Email content
    let html = '<b>Awesome sauce</b>'
    // html = html.replace('$organizationName', orgName)

    const mailOptions = {
        from: 'sc.nattaponw@gmail.com', // Sender email address
        to: emailTo, // Recipient email address (you can use an array for multiple recipients)
        subject: 'test Subject', // Email subject
        html: html
    };

    // Send the email
    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            console.error('Error:', error);
        } else {
            console.log('Email sent:', info);
        }
    });

}

sendEmail('mil2inda@gmail.com')