backend/src/services/mail.services.js
Fred Boniface f512f7db3f Refactoring mail code
Signed-off-by: Fred Boniface <fred@fjla.uk>
2023-04-05 11:27:01 +01:00

35 lines
1017 B
JavaScript

const log = require('../utils/log.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
}
})
async function send(message){ // message is an object containing strings for: *to, cc, bcc, *subject, *txt, html (* denotes required)
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")
return false;
}
log.out(`mailServices.send: SMTP Response: ${res.response}`)
return true;
}
module.exports = {
send
}