57 lines
2.0 KiB
JavaScript
57 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
const nodemailer = require('nodemailer');
|
|
const config = require('./config');
|
|
|
|
function ensureConfigured() {
|
|
if (!config.emailHost) {
|
|
const err = new Error('SMTP не настроен (EMAIL_HOST пуст)');
|
|
err.status = 503;
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
function sendMail({ to, subject, text }) {
|
|
ensureConfigured();
|
|
const transporter = nodemailer.createTransport({
|
|
host: config.emailHost,
|
|
port: config.emailPort,
|
|
secure: config.emailSecure,
|
|
auth: config.emailUser ? { user: config.emailUser, pass: config.emailPassword } : undefined
|
|
});
|
|
return transporter
|
|
.sendMail({
|
|
from: config.mailFrom || config.emailUser,
|
|
to,
|
|
subject,
|
|
text
|
|
})
|
|
.finally(() => transporter.close());
|
|
}
|
|
|
|
function sendVerificationCode(email, code) {
|
|
console.log(`[register] код для ${email}: ${code}`);
|
|
return sendMail({
|
|
to: email,
|
|
subject: 'Код подтверждения регистрации',
|
|
text: `Ваш код подтверждения регистрации: ${code}\n\nКод действителен 10 минут.`
|
|
});
|
|
}
|
|
|
|
function sendApproved(email, { login, password, fio }) {
|
|
return sendMail({
|
|
to: email,
|
|
subject: 'Регистрация одобрена',
|
|
text: `Здравствуйте, ${fio}!\n\nВаша заявка на регистрацию одобрена.\n\nВаши данные для входа:\nЛогин: ${login}\nПароль: ${password}\n\nРекомендуем сменить пароль при первом входе.`
|
|
});
|
|
}
|
|
|
|
function sendDenied(email, { fio, reason }) {
|
|
return sendMail({
|
|
to: email,
|
|
subject: 'Регистрация отклонена',
|
|
text: `Здравствуйте${fio ? ', ' + fio : ''}!\n\nВаша заявка на регистрацию отклонена.\n${reason ? 'Причина: ' + reason + '\n' : ''}\nЕсли вы считаете это ошибкой, обратитесь к администратору.`
|
|
});
|
|
}
|
|
|
|
module.exports = { sendVerificationCode, sendApproved, sendDenied }; |