const log = require("../utils/logs.utils");
const mail = require("nodemailer"); //>> Probs wrong

const fromAddr = process.env.OWL_EML_FROM;
const smtpUser = process.env.OWL_EML_USER;
const smtpPass = process.env.OWL_EML_PASS;
const smtpHost = process.env.OWL_EML_HOST;
const smtpPort = process.env.OWL_EML_PORT;

let transporter = mail.createTransport({
  host: smtpHost,
  port: smtpPort,
  secure: false, // Must be false for STARTTLS on port 587
  auth: {
    user: smtpUser,
    pass: smtpPass,
  },
});

/* 'message' is an object containing string values for:
 *to, cc, bcc, *subject, *txt, html.  * denotes required */
async function send(message) {
  log.out("mailServices.send: Message send request received", "info");
  message.from = fromAddr;
  try {
    var res = await transporter.sendMail(message);
  } catch (err) {
    log.out(`mailServices.send: Message send failed: ${err}`, "err");
    return false;
  }
  log.out(`mailServices.send: SMTP Response: ${res.response}`);
  return true;
}

module.exports = {
  send,
};
