Новые правки
This commit is contained in:
parent
9bee21ad45
commit
1eedeb9fb9
1 changed files with 24 additions and 9 deletions
|
|
@ -8,7 +8,7 @@ const SMTP_HOST = isProduction
|
||||||
? (import.meta.env.SMTP_HOST || 'smtp.resend.com')
|
? (import.meta.env.SMTP_HOST || 'smtp.resend.com')
|
||||||
: 'localhost';
|
: 'localhost';
|
||||||
const SMTP_PORT = isProduction
|
const SMTP_PORT = isProduction
|
||||||
? (import.meta.env.SMTP_PORT || '465')
|
? (import.meta.env.SMTP_PORT || '587')
|
||||||
: '1025';
|
: '1025';
|
||||||
const SMTP_AUTH_USER = isProduction ? (import.meta.env.SMTP_AUTH_USER || '') : '';
|
const SMTP_AUTH_USER = isProduction ? (import.meta.env.SMTP_AUTH_USER || '') : '';
|
||||||
const SMTP_AUTH_PASS = isProduction ? (import.meta.env.SMTP_AUTH_PASS || '') : '';
|
const SMTP_AUTH_PASS = isProduction ? (import.meta.env.SMTP_AUTH_PASS || '') : '';
|
||||||
|
|
@ -27,11 +27,14 @@ console.log('[EMAIL] SMTP config:', { host: SMTP_HOST, port: SMTP_PORT, auth: !!
|
||||||
|
|
||||||
function getTransporter() {
|
function getTransporter() {
|
||||||
if (!transporter) {
|
if (!transporter) {
|
||||||
|
const useTLS = SMTP_PORT === '465';
|
||||||
transporter = nodemailer.createTransport({
|
transporter = nodemailer.createTransport({
|
||||||
host: SMTP_HOST,
|
host: SMTP_HOST,
|
||||||
port: parseInt(SMTP_PORT),
|
port: parseInt(SMTP_PORT),
|
||||||
secure: isProduction,
|
secure: useTLS,
|
||||||
requireTLS: isProduction,
|
requireTLS: !useTLS,
|
||||||
|
connectionTimeout: 10000,
|
||||||
|
greetingTimeout: 10000,
|
||||||
auth: isProduction && SMTP_AUTH_USER ? {
|
auth: isProduction && SMTP_AUTH_USER ? {
|
||||||
user: SMTP_AUTH_USER,
|
user: SMTP_AUTH_USER,
|
||||||
pass: SMTP_AUTH_PASS,
|
pass: SMTP_AUTH_PASS,
|
||||||
|
|
@ -41,17 +44,29 @@ function getTransporter() {
|
||||||
return transporter;
|
return transporter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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> {
|
export async function sendEmail(options: EmailOptions): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
console.log('Sending email to:', options.to);
|
console.log('Sending email to:', options.to);
|
||||||
console.log('SMTP config:', { host: SMTP_HOST, port: SMTP_PORT });
|
console.log('SMTP config:', { host: SMTP_HOST, port: SMTP_PORT });
|
||||||
|
|
||||||
const info = await getTransporter().sendMail({
|
const info = await withTimeout(
|
||||||
|
getTransporter().sendMail({
|
||||||
from: `${FROM_NAME} <${FROM_EMAIL}>`,
|
from: `${FROM_NAME} <${FROM_EMAIL}>`,
|
||||||
to: options.to,
|
to: options.to,
|
||||||
subject: options.subject,
|
subject: options.subject,
|
||||||
html: options.html,
|
html: options.html,
|
||||||
});
|
}),
|
||||||
|
15000
|
||||||
|
);
|
||||||
|
|
||||||
console.log('Email sent:', info.messageId);
|
console.log('Email sent:', info.messageId);
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue