retropilot-server/src/server/controllers/mailing.js

52 lines
1.0 KiB
JavaScript
Raw Normal View History

2022-01-12 08:02:30 -07:00
import nodemailer from 'nodemailer';
import log4js from 'log4js';
2021-05-23 08:40:55 -06:00
const logger = log4js.getLogger('default');
2022-01-07 18:35:55 -07:00
const transporter = nodemailer.createTransport(
{
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
2022-01-07 18:35:55 -07:00
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
2021-05-23 08:40:55 -06:00
},
2022-01-07 18:35:55 -07:00
logger: true,
2022-01-08 13:43:57 -07:00
debug: false,
2022-01-07 18:35:55 -07:00
},
{ from: process.env.SMTP_FROM },
2021-05-23 08:40:55 -06:00
);
async function sendEmailVerification(token, email) {
if (!process.env.CAN_SEND_MAIL) {
2022-01-08 17:35:40 -07:00
return logger.warn(`Mailing disabled. ${email} - ${token}`);
}
2022-01-07 18:35:55 -07:00
let message,
error,
info;
try {
message = {
from: process.env.SMTP_FROM,
2022-01-07 18:35:55 -07:00
to: email.trim(),
subject: 'RetroPilot Registration Token',
2022-01-08 13:43:57 -07:00
text: `Your Email Registration Token Is: "${token}"`,
2022-01-07 18:35:55 -07:00
};
2022-01-08 17:35:40 -07:00
info = await transporter.sendMail(message);
2022-01-08 13:43:57 -07:00
} catch (exception) {
2022-01-08 17:35:40 -07:00
error = exception;
2022-01-07 18:35:55 -07:00
}
if (error) {
logger.warn(`Email to ${email} FAILED ${error} - ${token}`);
return false;
}
return info;
2021-05-23 08:40:55 -06:00
}
2022-01-12 08:02:30 -07:00
export default {
sendEmailVerification,
2022-01-07 18:35:55 -07:00
};