backend/src/services/mail.services.js

74 lines
2.1 KiB
JavaScript
Raw Normal View History

const log = require('../utils/log.utils')
const fs = require('fs/promises')
2023-04-04 11:45:32 +01:00
const mail = require('nodemailer'); //>> Probs wrong
2023-04-01 15:09:29 +01:00
const fromAdrr = 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
2023-04-04 17:17:12 +01:00
let transporter = mail.createTransport({
2023-04-01 15:09:29 +01:00
host: smtpHost,
port: smtpPort,
2023-04-04 17:17:12 +01:00
secure: false, // Must be false for STARTTLS on port 587
2023-04-01 15:09:29 +01:00
auth: {
user: smtpUser,
pass: smtpPass
}
2023-04-04 17:17:12 +01:00
})
2023-04-01 15:09:29 +01:00
async function sendTest(to, cc, bcc) {
log.out(`mailServices.sendTest: Sending test message to: ${to}`, "info")
2023-04-04 17:17:12 +01:00
let tHtml = fs.readFile('mail-templates/register.html', 'utf-8');
let tTxt = fs.readFile('mail-templates/register.txt', 'utf-8');
// Send test mail message
try {
var res = await transporter.sendMail({
2023-04-01 15:09:29 +01:00
from: fromAdrr,
to: to,
cc: cc,
bcc: bcc,
2023-04-04 11:45:32 +01:00
subject: "Test Message from OwlBoard",
2023-04-04 17:17:12 +01:00
text: (await tTxt).replace(/>>ACCESSCODE<</g, "TEST-MESSAGE-ONLY"),
html: (await tHtml).replace(/>>ACCESSCODE<</g, "TEST-MESSAGE-ONLY")
2023-04-01 15:09:29 +01:00
});
} catch(err) {
log.out(err, "warn")
var res = "failed"
}
2023-04-04 11:45:32 +01:00
return res;
}
2023-04-04 17:17:12 +01:00
async function sendRegister(to, accesscode) {
log.out(`mailServices.sendRegister: Sending registration message to: ${to}`, "info")
let tHtml = fs.readFile('mail-templates/register.html', 'utf-8');
let tTxt = fs.readFile('mail-templates/register.txt', 'utf-8');
// Send test mail message
try {
var res = transporter.sendMail({
from: fromAdrr,
to: to,
cc: cc,
bcc: bcc,
subject: "OwlBoard - Complete Registration",
text: (await tTxt).replace(/>>ACCESSCODE<</g, accesscode),
html: (await tHtml).replace(/>>ACCESSCODE<</g, accesscode)
});
return await res;
} catch(err) {
log.out("mailServices.sendRegister: Mail send failed")
log.out(err, "warn")
return "failed"
}
}
async function sendAlert() {
return;
2023-04-01 15:09:29 +01:00
}
module.exports = {
sendTest,
sendRegister,
sendAlert
}