Новый правки
This commit is contained in:
parent
1eedeb9fb9
commit
2453421cdf
1 changed files with 52 additions and 8 deletions
|
|
@ -2,7 +2,9 @@ import nodemailer from 'nodemailer';
|
|||
import type { EmailOptions } from '../globalInterfaces';
|
||||
|
||||
const isDev = import.meta.env.DEV;
|
||||
const isProduction = import.meta.env.PROD === 'true' || !isDev;
|
||||
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.resend.com')
|
||||
|
|
@ -12,6 +14,7 @@ const SMTP_PORT = isProduction
|
|||
: '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 = import.meta.env.RESEND_API_KEY || SMTP_AUTH_PASS;
|
||||
const FROM_EMAIL = isProduction
|
||||
? (import.meta.env.FROM_EMAIL || 'onboarding@resend.onlinemail.me')
|
||||
: 'noreply@localhost';
|
||||
|
|
@ -22,8 +25,12 @@ const SITE_URL = isProduction
|
|||
|
||||
let transporter: nodemailer.Transporter | null = null;
|
||||
|
||||
console.log('[EMAIL] isDev:', isDev, 'isProduction:', isProduction);
|
||||
console.log('[EMAIL] SMTP config:', { host: SMTP_HOST, port: SMTP_PORT, auth: !!SMTP_AUTH_USER });
|
||||
console.log('[EMAIL] isDev:', isDev);
|
||||
console.log('[EMAIL] hasProductionFlag:', hasProductionFlag);
|
||||
console.log('[EMAIL] forceProduction:', forceProduction);
|
||||
console.log('[EMAIL] isProduction:', isProduction);
|
||||
console.log('[EMAIL] RESEND_API_KEY present:', !!RESEND_API_KEY);
|
||||
console.log('[EMAIL] Use RESEND_API:', isProduction && !!RESEND_API_KEY);
|
||||
|
||||
function getTransporter() {
|
||||
if (!transporter) {
|
||||
|
|
@ -44,6 +51,39 @@ function getTransporter() {
|
|||
return transporter;
|
||||
}
|
||||
|
||||
async function sendViaResendApi(options: EmailOptions): Promise<boolean> {
|
||||
try {
|
||||
console.log('[RESEND_API] Sending to:', options.to);
|
||||
|
||||
const response = await fetch('https://api.resend.com/emails', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${RESEND_API_KEY}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
from: `${FROM_NAME} <${FROM_EMAIL}>`,
|
||||
to: options.to,
|
||||
subject: options.subject,
|
||||
html: options.html,
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
console.log('[RESEND_API] Email sent:', data.id);
|
||||
return true;
|
||||
} else {
|
||||
console.error('[RESEND_API] Error:', data);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[RESEND_API] Error:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
|
||||
return Promise.race([
|
||||
promise,
|
||||
|
|
@ -54,10 +94,14 @@ function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
|
|||
}
|
||||
|
||||
export async function sendEmail(options: EmailOptions): Promise<boolean> {
|
||||
try {
|
||||
console.log('Sending email to:', options.to);
|
||||
console.log('SMTP config:', { host: SMTP_HOST, port: SMTP_PORT });
|
||||
if (isProduction && RESEND_API_KEY) {
|
||||
return sendViaResendApi(options);
|
||||
}
|
||||
|
||||
console.log('[NODEMAILER] Sending to:', options.to);
|
||||
console.log('[NODEMAILER] SMTP config:', { host: SMTP_HOST, port: SMTP_PORT });
|
||||
|
||||
try {
|
||||
const info = await withTimeout(
|
||||
getTransporter().sendMail({
|
||||
from: `${FROM_NAME} <${FROM_EMAIL}>`,
|
||||
|
|
@ -68,10 +112,10 @@ export async function sendEmail(options: EmailOptions): Promise<boolean> {
|
|||
15000
|
||||
);
|
||||
|
||||
console.log('Email sent:', info.messageId);
|
||||
console.log('[NODEMAILER] Email sent:', info.messageId);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Email send error:', error);
|
||||
console.error('[NODEMAILER] Email send error:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue