/
TimurIsm
/
OpenEyes
Обзор
Документация
Войти
/
TimurIsm
/
OpenEyes
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
back/utils/emailService.js
156 строк
5 KB
TimurIsm
first_commit
16 май 2026, 15:34
16 май 2026, 15:34
4d3f198
Код
Авторство
О чём код?
const nodemailer = require("nodemailer"); const {getVerificationEmailTemplate, getWelcomeEmailTemplate} = require('../emailTemplates/verificationTemplates'); const {getPasswordResetEmailTemplate, getPasswordResetConfirmationEmailTemplate} = require('../emailTemplates/passwordTemplates'); const {getAccountDeletionEmailTemplate, getAccountDeletionConfirmationEmailTemplate} = require('../emailTemplates/deletionTemplates'); const {getPasswordChangeRequestEmailTemplate} = require('../emailTemplates/changePasswordTemplates'); const createTransporter = () => { return nodemailer.createTransport({ host: 'smtp.mail.ru', port: 465, secure: true, auth: { user: 'openeyes.web@mail.ru', pass: process.env.MAILRU_PASSWORD } }); }; const sendEmail = async (to, subject, htmlContent, textContent = '') => { try { const transporter = createTransporter(); const info = await transporter.sendMail({ from: '"OpenEyes" <openeyes.web@mail.ru>', to, subject, html: htmlContent, text: textContent || htmlContent.replace(/<[^>]*>/g, '') }); console.log(`Email отправлен на ${to}: ${info.messageId}`); return info; } catch (error) { console.error(`Ошибка отправки email на ${to}:`, error); throw new Error(`Не удалось отправить письмо: ${error.message}`); } }; const sendVerificationEmail = async (email, verificationToken, userId) => { try { const verificationUrl = `${process.env.FRONTEND_URL}/verify-email?token=${verificationToken}&userId=${userId}`; const template = getVerificationEmailTemplate(verificationUrl); return await sendEmail( email, template.subject, template.html, template.text ); } catch (error) { throw new Error(`Не удалось отправить письмо подтверждения: ${error.message}`); } }; const sendWelcomeEmail = async (email) => { try { const template = getWelcomeEmailTemplate(); return await sendEmail( email, template.subject, template.html, template.text ); } catch (error) { throw new Error(`Не удалось отправить приветственное письмо: ${error.message}`); } }; const sendPasswordResetEmail = async (email, resetToken, userId) => { try { const resetUrl = `${process.env.FRONTEND_URL}/reset-password?token=${resetToken}&userId=${userId}`; const template = getPasswordResetEmailTemplate(resetUrl); return await sendEmail( email, template.subject, template.html, template.text ); } catch (error) { throw new Error(`Не удалось отправить письмо для сброса пароля: ${error.message}`); } }; const sendPasswordResetConfirmationEmail = async (email) => { try { const template = getPasswordResetConfirmationEmailTemplate(); return await sendEmail( email, template.subject, template.html, template.text ); } catch (error) { throw new Error(`Не удалось отправить письмо подтверждения сброса пароля: ${error.message}`); } }; const sendAccountDeletionEmail = async (email, deletionToken, userId) => { try { const deletionUrl = `${process.env.FRONTEND_URL}/confirm-account-deletion?token=${deletionToken}&userId=${userId}`; const template = getAccountDeletionEmailTemplate(deletionUrl); return await sendEmail( email, template.subject, template.html, template.text ); } catch (error) { throw new Error(`Не удалось отправить письмо для удаления аккаунта: ${error.message}`); } }; const sendAccountDeletionConfirmationEmail = async (email) => { try { const template = getAccountDeletionConfirmationEmailTemplate(); return await sendEmail( email, template.subject, template.html, template.text ); } catch (error) { throw new Error(`Не удалось отправить письмо подтверждения удаления аккаунта: ${error.message}`); } }; const sendPasswordChangeEmail = async (email, resetToken, userId, userName = '') => { try { const resetUrl = `${process.env.FRONTEND_URL}/reset-password?token=${resetToken}&userId=${userId}&source=change`; const template = getPasswordChangeRequestEmailTemplate(resetUrl, userName); return await sendEmail( email, template.subject, template.html, template.text ); } catch (error) { throw new Error(`Не удалось отправить письмо для изменения пароля: ${error.message}`); } }; module.exports = { sendVerificationEmail, sendWelcomeEmail, sendPasswordResetEmail, sendPasswordResetConfirmationEmail, sendAccountDeletionEmail, sendAccountDeletionConfirmationEmail, sendPasswordChangeEmail };