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