修复邮件发送: TLS error handler重构 + 错误信息透传
Agent-Logs-Url: https://github.com/qinfendebingshuo/guanghulab/sessions/98f7f760-839a-42c7-b716-fa39b6021391 Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
This commit is contained in:
parent
4a2b1c94c5
commit
edf3cfedd4
|
|
@ -176,15 +176,22 @@ async function sendEmail(to, subject, htmlBody) {
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let settled = false;
|
let settled = false;
|
||||||
const timeoutId = setTimeout(() => {
|
let socket;
|
||||||
if (!settled) {
|
|
||||||
|
const settle = (fn, val) => {
|
||||||
|
if (settled) return;
|
||||||
settled = true;
|
settled = true;
|
||||||
try { socket.destroy(); } catch { /* ignore */ }
|
clearTimeout(timeoutId);
|
||||||
reject(new Error('SMTP超时(30s)'));
|
try { if (socket) socket.destroy(); } catch { /* ignore */ }
|
||||||
}
|
fn(val);
|
||||||
|
};
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(() => {
|
||||||
|
settle(reject, new Error(`SMTP超时(30s) → ${smtpHost}:${smtpPort}`));
|
||||||
}, 30000);
|
}, 30000);
|
||||||
|
|
||||||
const socket = tls.connect(smtpPort, smtpHost, {}, () => {
|
try {
|
||||||
|
socket = tls.connect(smtpPort, smtpHost, {}, () => {
|
||||||
let step = 0;
|
let step = 0;
|
||||||
let buffer = '';
|
let buffer = '';
|
||||||
const from = config.smtp_user;
|
const from = config.smtp_user;
|
||||||
|
|
@ -224,12 +231,7 @@ async function sendEmail(to, subject, htmlBody) {
|
||||||
|
|
||||||
// 检查错误响应 (4xx/5xx)
|
// 检查错误响应 (4xx/5xx)
|
||||||
if (statusCode >= 400) {
|
if (statusCode >= 400) {
|
||||||
if (!settled) {
|
settle(reject, new Error(`SMTP错误(${statusCode}): ${line}`));
|
||||||
settled = true;
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
try { socket.destroy(); } catch { /* ignore */ }
|
|
||||||
reject(new Error(`SMTP错误(${statusCode}): ${line}`));
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -240,32 +242,28 @@ async function sendEmail(to, subject, htmlBody) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 所有命令已发送且收到最终响应
|
// 所有命令已发送且收到最终响应
|
||||||
if (step >= commands.length && !settled) {
|
if (step >= commands.length) {
|
||||||
settled = true;
|
settle(resolve, true);
|
||||||
clearTimeout(timeoutId);
|
|
||||||
try { socket.destroy(); } catch { /* ignore */ }
|
|
||||||
resolve(true);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('error', (err) => {
|
|
||||||
if (!settled) {
|
|
||||||
settled = true;
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 连接级错误 (DNS解析失败/连接拒绝/TLS握手失败)
|
||||||
|
// 必须在connect callback外部注册,否则连接失败时handler不触发
|
||||||
socket.on('error', (err) => {
|
socket.on('error', (err) => {
|
||||||
|
settle(reject, new Error(`SMTP连接失败(${smtpHost}): ${err.message}`));
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('close', () => {
|
||||||
if (!settled) {
|
if (!settled) {
|
||||||
settled = true;
|
settle(reject, new Error(`SMTP连接被关闭(${smtpHost})`));
|
||||||
clearTimeout(timeoutId);
|
|
||||||
reject(err);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} catch (err) {
|
||||||
|
settle(reject, new Error(`SMTP初始化失败: ${err.message}`));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1064,7 +1062,7 @@ async function sendBandwidthAuthEmail(email, code) {
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`[邮件中枢] ❌ 带宽验证码发送失败: ${err.message}`);
|
console.error(`[邮件中枢] ❌ 带宽验证码发送失败: ${err.message}`);
|
||||||
logEmail('bandwidth-auth', email, false, err.message);
|
logEmail('bandwidth-auth', email, false, err.message);
|
||||||
return { sent: 0, failed: 1 };
|
return { sent: 0, failed: 1, error: err.message };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1972,13 +1972,15 @@ async function submitCode(e) {
|
||||||
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
||||||
res.end(JSON.stringify({ success: true, message: '验证码已发送到您的邮箱,请查收(15分钟内有效)' }));
|
res.end(JSON.stringify({ success: true, message: '验证码已发送到您的邮箱,请查收(15分钟内有效)' }));
|
||||||
} else {
|
} else {
|
||||||
|
const errDetail = result.error || '未知原因';
|
||||||
|
console.error('[bandwidth-send-code] 邮件发送失败:', errDetail);
|
||||||
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
||||||
res.end(JSON.stringify({ success: false, message: '📧 邮件发送失败,请稍后重试或联系冰朔获取验证码' }));
|
res.end(JSON.stringify({ success: false, message: `📧 邮件发送失败(${errDetail}),请稍后重试或联系冰朔获取验证码` }));
|
||||||
}
|
}
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
console.error('[bandwidth-send-code] 邮件发送异常:', err.message || err);
|
console.error('[bandwidth-send-code] 邮件发送异常:', err.message || err);
|
||||||
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
||||||
res.end(JSON.stringify({ success: false, message: '📧 邮件发送失败,请稍后重试或联系冰朔获取验证码' }));
|
res.end(JSON.stringify({ success: false, message: `📧 邮件发送异常(${err.message}),请稍后重试或联系冰朔获取验证码` }));
|
||||||
});
|
});
|
||||||
} catch {
|
} catch {
|
||||||
// Email module not available - code was still created
|
// Email module not available - code was still created
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue