Update files

This commit is contained in:
glpshchn 2025-12-14 17:52:58 +03:00
parent 9ebdd81c48
commit b59c5afe51
1 changed files with 63 additions and 27 deletions

View File

@ -2,8 +2,9 @@ const AWS = require('aws-sdk');
const nodemailer = require('nodemailer'); const nodemailer = require('nodemailer');
const config = require('../config'); const config = require('../config');
// Инициализация AWS SES // Инициализация AWS SES / SESv2
let sesClient = null; let sesClient = null;
let sesv2Client = null;
let transporter = null; let transporter = null;
const initializeEmailService = () => { const initializeEmailService = () => {
@ -29,16 +30,19 @@ const initializeEmailService = () => {
region: awsRegion region: awsRegion
}; };
// Для Yandex Cloud Postbox нужен кастомный endpoint // Для Yandex Cloud Postbox нужен кастомный endpoint и SESv2 API
if (endpointUrl) { if (endpointUrl) {
sesConfig.endpoint = endpointUrl; sesConfig.endpoint = endpointUrl;
console.log(`[Email] Используется Yandex Cloud Postbox с endpoint: ${endpointUrl}`); console.log(`[Email] Используется Yandex Cloud Postbox с endpoint: ${endpointUrl}`);
// Yandex Cloud Postbox использует SESv2 API
sesv2Client = new AWS.SESv2(sesConfig);
} else if (!validAWSRegions.includes(awsRegion)) { } else if (!validAWSRegions.includes(awsRegion)) {
console.warn(`[Email] Невалидный регион AWS SES: ${awsRegion}. Используется us-east-1`); console.warn(`[Email] Невалидный регион AWS SES: ${awsRegion}. Используется us-east-1`);
sesConfig.region = 'us-east-1'; sesConfig.region = 'us-east-1';
}
sesClient = new AWS.SES(sesConfig); sesClient = new AWS.SES(sesConfig);
} else {
sesClient = new AWS.SES(sesConfig);
}
} else if (emailProvider === 'yandex' || emailProvider === 'smtp') { } else if (emailProvider === 'yandex' || emailProvider === 'smtp') {
const emailConfig = config.email?.[emailProvider] || config.email?.smtp || {}; const emailConfig = config.email?.[emailProvider] || config.email?.smtp || {};
@ -90,8 +94,39 @@ const sendEmail = async (to, subject, html, text) => {
const emailProvider = process.env.EMAIL_PROVIDER || 'aws'; const emailProvider = process.env.EMAIL_PROVIDER || 'aws';
const fromEmail = process.env.EMAIL_FROM || config.email?.from || 'noreply@nakama.guru'; const fromEmail = process.env.EMAIL_FROM || config.email?.from || 'noreply@nakama.guru';
if (emailProvider === 'aws' && sesClient) { // Использовать SESv2 для Yandex Cloud Postbox, SES для обычного AWS
// Отправка через AWS SES if (emailProvider === 'aws' && (sesClient || sesv2Client)) {
if (sesv2Client) {
// Отправка через AWS SESv2 (Yandex Cloud Postbox)
const params = {
FromEmailAddress: fromEmail,
Destination: {
ToAddresses: [to]
},
Content: {
Simple: {
Subject: {
Data: subject,
Charset: 'UTF-8'
},
Body: {
Html: {
Data: html,
Charset: 'UTF-8'
},
Text: {
Data: text || html.replace(/<[^>]*>/g, ''),
Charset: 'UTF-8'
}
}
}
}
};
const result = await sesv2Client.sendEmail(params).promise();
return { success: true, messageId: result.MessageId };
} else if (sesClient) {
// Отправка через AWS SES (обычный AWS)
const params = { const params = {
Source: fromEmail, Source: fromEmail,
Destination: { Destination: {
@ -117,6 +152,7 @@ const sendEmail = async (to, subject, html, text) => {
const result = await sesClient.sendEmail(params).promise(); const result = await sesClient.sendEmail(params).promise();
return { success: true, messageId: result.MessageId }; return { success: true, messageId: result.MessageId };
}
} else if (transporter) { } else if (transporter) {
// Отправка через SMTP (Yandex, Gmail и т.д.) // Отправка через SMTP (Yandex, Gmail и т.д.)
const info = await transporter.sendMail({ const info = await transporter.sendMail({