From 6e935a90998ea7ef2f1c42b04c54d5bf387cf33a Mon Sep 17 00:00:00 2001 From: Web-serfer Date: Mon, 4 May 2026 20:45:42 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=B2=20=D0=BA=D0=BE=D0=BC=D0=BF?= =?UTF-8?q?=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/lib/email.ts | 77 +++++--------------------- frontend/src/pages/api/consultation.ts | 19 ++----- 2 files changed, 17 insertions(+), 79 deletions(-) diff --git a/frontend/src/lib/email.ts b/frontend/src/lib/email.ts index fb8fc56..a18825d 100644 --- a/frontend/src/lib/email.ts +++ b/frontend/src/lib/email.ts @@ -1,4 +1,3 @@ -import nodemailer from 'nodemailer'; import type { EmailOptions } from '../globalInterfaces'; const isDev = import.meta.env.DEV; @@ -6,14 +5,8 @@ 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 SMTP_HOST = isProduction - ? (import.meta.env.SMTP_HOST || 'smtp.gmail.com') - : 'localhost'; -const SMTP_PORT = isProduction - ? (import.meta.env.SMTP_PORT || '587') - : '1025'; -const SMTP_AUTH_USER = isProduction ? (import.meta.env.SMTP_AUTH_USER || '') : ''; -const SMTP_AUTH_PASS = isProduction ? (import.meta.env.SMTP_AUTH_PASS || '') : ''; +const RESEND_API_KEY = isProduction ? (import.meta.env.RESEND_API_KEY || '') : ''; + const FROM_EMAIL = isProduction ? (import.meta.env.FROM_EMAIL || 'noreply@localhost') : 'noreply@localhost'; @@ -22,34 +15,13 @@ const SITE_URL = isProduction ? (import.meta.env.SITE_URL || 'https://avtourist-surgut.ru') : 'http://localhost:4321'; -let transporter: nodemailer.Transporter | null = null; - console.log('[EMAIL] isDev:', isDev); console.log('[EMAIL] isProduction:', isProduction); -console.log('[EMAIL] SMTP:', SMTP_HOST, ':', SMTP_PORT); +console.log('[EMAIL] Using Resend:', isProduction && !!RESEND_API_KEY); -function getTransporter() { - if (!transporter) { - const useTLS = SMTP_PORT === '465'; - transporter = nodemailer.createTransport({ - host: SMTP_HOST, - port: parseInt(SMTP_PORT), - secure: useTLS, - requireTLS: !useTLS, - connectionTimeout: 10000, - greetingTimeout: 10000, - auth: isProduction && SMTP_AUTH_USER ? { - user: SMTP_AUTH_USER, - pass: SMTP_AUTH_PASS, - } : undefined, - }); - } - return transporter; -} - -async function sendViaResendApi(options: EmailOptions): Promise { +async function sendViaResend(options: EmailOptions): Promise { try { - console.log('[RESEND_API] Sending to:', options.to); + console.log('[RESEND] Sending to:', options.to); const response = await fetch('https://api.resend.com/emails', { method: 'POST', @@ -68,48 +40,25 @@ async function sendViaResendApi(options: EmailOptions): Promise { const data = await response.json(); if (response.ok) { - console.log('[RESEND_API] Email sent:', data.id); + console.log('[RESEND] Email sent:', data.id); return true; } else { - console.error('[RESEND_API] Error:', data); + console.error('[RESEND] Error:', data); return false; } } catch (error) { - console.error('[RESEND_API] Error:', error); + console.error('[RESEND] Error:', error); return false; } } -function withTimeout(promise: Promise, ms: number): Promise { - return Promise.race([ - promise, - new Promise((_, reject) => - setTimeout(() => reject(new Error('Email send timeout')), ms) - ) - ]); -} - export async function sendEmail(options: EmailOptions): Promise { - console.log('[NODEMAILER] Sending to:', options.to); - console.log('[NODEMAILER] SMTP config:', { host: SMTP_HOST, port: SMTP_PORT, user: SMTP_AUTH_USER }); - - try { - const info = await withTimeout( - getTransporter().sendMail({ - from: `${FROM_NAME} <${FROM_EMAIL}>`, - to: options.to, - subject: options.subject, - html: options.html, - }), - 15000 - ); - - console.log('[NODEMAILER] Email sent:', info.messageId); - return true; - } catch (error) { - console.error('[NODEMAILER] Email send error:', error); - return false; + if (isProduction && RESEND_API_KEY) { + return sendViaResend(options); } + + console.log('[DEV] Email not sent (no Resend key):', options.to); + return false; } export function getSiteUrl(): string { diff --git a/frontend/src/pages/api/consultation.ts b/frontend/src/pages/api/consultation.ts index ec00bbf..7cbdaed 100644 --- a/frontend/src/pages/api/consultation.ts +++ b/frontend/src/pages/api/consultation.ts @@ -1,17 +1,8 @@ import type { APIRoute } from 'astro'; -import nodemailer from 'nodemailer'; +import { sendEmail } from '../../../lib/email'; -const PB_POCKETBASE_URL = import.meta.env.PB_POCKETBASE_URL; // || 'http://localhost:8090'; -const SMTP_HOST = import.meta.env.SMTP_HOST; // || 'localhost'; -const SMTP_PORT = import.meta.env.SMTP_PORT; // || '1025'; -const NOTIFY_EMAIL = import.meta.env.NOTIFY_EMAIL; // || 'info@avtourist.ru'; - -const transporter = nodemailer.createTransport({ - host: SMTP_HOST, - port: parseInt(SMTP_PORT), - secure: false, - ignoreTLS: true, -}); +const PB_POCKETBASE_URL = import.meta.env.PB_POCKETBASE_URL; +const NOTIFY_EMAIL = import.meta.env.NOTIFY_EMAIL || 'redibedi2019@gmail.com'; const RATE_LIMIT_WINDOW = 60 * 1000; const MAX_REQUESTS = 3; @@ -140,13 +131,11 @@ export const POST: APIRoute = async ({ request }) => { `.trim(); try { - await transporter.sendMail({ - from: 'avtourist@surgut.ru', + await sendEmail({ to: NOTIFY_EMAIL, subject: `Новая заявка от ${name}`, html: emailHtml, }); - console.log('Email notification sent'); } catch (emailError) { console.error('Email send error:', emailError); }