Новые правки

This commit is contained in:
Web-serfer 2026-05-04 22:18:43 +05:00
parent 8ae05f40c4
commit 423b436140

View file

@ -1,4 +1,3 @@
import nodemailer from 'nodemailer';
import type { EmailOptions } from '../globalInterfaces';
const isDev = import.meta.env.DEV;
@ -6,12 +5,7 @@ const hasProductionFlag = import.meta.env.PROD === 'true';
const forceProduction = import.meta.env.PB_POCKETBASE_URL?.includes('avt-back') || false;
const isProduction = hasProductionFlag || forceProduction || !isDev;
const RESEND_API_KEY = isProduction ? (import.meta.env.RESEND_API_KEY || '') : '';
const SMTP_HOST = isProduction ? (import.meta.env.SMTP_HOST || '') : '';
const SMTP_PORT = isProduction ? (import.meta.env.SMTP_PORT || '465') : '1025';
const SMTP_AUTH_USER = isProduction ? (import.meta.env.SMTP_AUTH_USER || '') : '';
const SMTP_AUTH_PASS = isProduction ? (import.meta.env.SMTP_AUTH_PASS || '') : '';
const SMTP_BZ_API_KEY = isProduction ? (import.meta.env.SMTP_BZ_API_KEY || '') : '';
const FROM_EMAIL = isProduction
? (import.meta.env.FROM_EMAIL || 'noreply@localhost')
@ -23,31 +17,20 @@ const SITE_URL = isProduction
console.log('[EMAIL] isDev:', isDev);
console.log('[EMAIL] isProduction:', isProduction);
console.log('[EMAIL] SMTP:', SMTP_HOST, ':', SMTP_PORT);
function getTransporter() {
return nodemailer.createTransport({
host: SMTP_HOST,
port: parseInt(SMTP_PORT),
secure: SMTP_PORT === '465',
connectionTimeout: 15000,
auth: SMTP_AUTH_USER ? {
user: SMTP_AUTH_USER,
pass: SMTP_AUTH_PASS,
} : undefined,
});
}
async function sendViaResend(options: EmailOptions): Promise<boolean> {
async function sendViaSmtpBz(options: EmailOptions): Promise<boolean> {
try {
const response = await fetch('https://api.resend.com/emails', {
console.log('[SMTP.BZ] Sending to:', options.to);
const response = await fetch('https://api.smtp.bz/v1/smtp/send', {
method: 'POST',
headers: {
'Authorization': `Bearer ${RESEND_API_KEY}`,
'Authorization': SMTP_BZ_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
from: `${FROM_NAME} <${FROM_EMAIL}>`,
from: FROM_EMAIL,
name: FROM_NAME,
to: options.to,
subject: options.subject,
html: options.html,
@ -57,39 +40,23 @@ async function sendViaResend(options: EmailOptions): Promise<boolean> {
const data = await response.json();
if (response.ok) {
console.log('[RESEND] Email sent:', data.id);
console.log('[SMTP.BZ] Email sent:', data.id || 'ok');
return true;
} else {
console.error('[RESEND] Error:', data);
console.error('[SMTP.BZ] Error:', data);
return false;
}
} catch (error) {
console.error('[RESEND] Error:', error);
console.error('[SMTP.BZ] Error:', error);
return false;
}
}
export async function sendEmail(options: EmailOptions): Promise<boolean> {
if (SMTP_HOST && SMTP_AUTH_PASS) {
try {
const info = await getTransporter().sendMail({
from: `${FROM_NAME} <${FROM_EMAIL}>`,
to: options.to,
subject: options.subject,
html: options.html,
});
console.log('[SMTP] Email sent:', info.messageId);
return true;
} catch (error) {
console.error('[SMTP] Email send error:', error);
return false;
}
if (SMTP_BZ_API_KEY) {
return sendViaSmtpBz(options);
}
if (isProduction && RESEND_API_KEY) {
return sendViaResend(options);
}
console.log('[DEV] Email not sent:', options.to);
return false;
}