Новые правки в компоненты
This commit is contained in:
parent
71da9d3143
commit
6e935a9099
2 changed files with 17 additions and 79 deletions
|
|
@ -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<boolean> {
|
||||
async function sendViaResend(options: EmailOptions): Promise<boolean> {
|
||||
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<boolean> {
|
|||
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<T>(promise: Promise<T>, ms: number): Promise<T> {
|
||||
return Promise.race([
|
||||
promise,
|
||||
new Promise<T>((_, reject) =>
|
||||
setTimeout(() => reject(new Error('Email send timeout')), ms)
|
||||
)
|
||||
]);
|
||||
}
|
||||
|
||||
export async function sendEmail(options: EmailOptions): Promise<boolean> {
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue