// don't forget: 'npm install nodemailer'
var nodemailer = require('nodemailer');

// transporter: houses data for sending email
let transporter = nodemailer.createTransport({
    service: 'gmail',
    secure: false, // as not using SSL
    port: 25, //stmp port
    auth: {
        user: 'njstesting@gmail.com',
        pass: 'fifPow-zydmoj-cyfhe2' // fill in with correct password
    },
    tls: {
        rejectUnauthorized: false
    }
});

let twoFactorAuth = {
    subject: 'Verify your email address',
    message: 'Demo email <br>that verifies <span style="color: red">new</span> user via two factor authentication...'
}

const emails = [{
        subject: 'Demo Email 1 Subject',
        message: 'Demo email 1 message...'
    },
    {
        subject: 'Demo Email 2 Subject',
        message: 'Demo email 2 message...'
    },
    {
        subject: 'Demo Email 3 Subject',
        message: 'Demo email 3 message...'
    }
];

let sendEmail = (addr, subj, msg) => {
    let helperOptions = {
        from: '"Nick Smith" <njstesting@gmail.com',
        to: addr,
        subject: subj,
        html: msg
       // text: msg
    };

    transporter.sendMail(helperOptions, (err, info) => {
        if (err) {
            return console.log('Error: ', err);
        }
        console.log('Message sent...', 'Info: ', info);
    });
}

const startAutomation = (emails, receiver) => {
    emails.forEach((email, i) => {
        let intTime = 10000; // in ms (1ms = 1000s)

        setTimeout(sendEmail, ((i + 1) * intTime), receiver, email.subject, email.message);
    });
}

//sendEmail('nick.smith.tsa@gmail.com', twoFactorAuth.subject, twoFactorAuth.message);
//startAutomation(emails, 'nick.smith.tsa@gmail.com');

exports.sendEmail = sendEmail;
exports.twoFactorAuth = twoFactorAuth;