Новые правки
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')
|
||||
: 'localhost';
|
||||
const SMTP_PORT = isProduction
|
||||
? (import.meta.env.SMTP_PORT || '465')
|
||||
? (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 || '') : '';
|
||||
|
|
@ -27,11 +27,14 @@ console.log('[EMAIL] SMTP config:', { host: SMTP_HOST, port: SMTP_PORT, auth: !!
|
|||
|
||||
function getTransporter() {
|
||||
if (!transporter) {
|
||||
const useTLS = SMTP_PORT === '465';
|
||||
transporter = nodemailer.createTransport({
|
||||
host: SMTP_HOST,
|
||||
port: parseInt(SMTP_PORT),
|
||||
secure: isProduction,
|
||||
requireTLS: isProduction,
|
||||
secure: useTLS,
|
||||
requireTLS: !useTLS,
|
||||
connectionTimeout: 10000,
|
||||
greetingTimeout: 10000,
|
||||
auth: isProduction && SMTP_AUTH_USER ? {
|
||||
user: SMTP_AUTH_USER,
|
||||
pass: SMTP_AUTH_PASS,
|
||||
|
|
@ -41,17 +44,29 @@ function getTransporter() {
|
|||
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> {
|
||||
try {
|
||||
console.log('Sending email to:', options.to);
|
||||
console.log('SMTP config:', { host: SMTP_HOST, port: SMTP_PORT });
|
||||
|
||||
const info = await getTransporter().sendMail({
|
||||
from: `${FROM_NAME} <${FROM_EMAIL}>`,
|
||||
to: options.to,
|
||||
subject: options.subject,
|
||||
html: options.html,
|
||||
});
|
||||
const info = await withTimeout(
|
||||
getTransporter().sendMail({
|
||||
from: `${FROM_NAME} <${FROM_EMAIL}>`,
|
||||
to: options.to,
|
||||
subject: options.subject,
|
||||
html: options.html,
|
||||
}),
|
||||
15000
|
||||
);
|
||||
|
||||
console.log('Email sent:', info.messageId);
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue