fix api server

This commit is contained in:
shenjianZ 2025-02-27 18:30:17 +08:00
parent ef28e2ea64
commit e46519ef57
8 changed files with 371 additions and 310 deletions

View File

@ -38,6 +38,25 @@ const transporter = nodemailer.createTransport({
user: process.env.EMAIL_USER || '', // 发送方邮箱
pass: process.env.EMAIL_PASS || '', // 授权码或密码
},
// 增加连接超时设置
connectionTimeout: 10000, // 10秒连接超时
greetingTimeout: 10000, // 10秒问候超时
socketTimeout: 15000, // 15秒socket超时
// 增加重试选项
pool: true, // 使用连接池
maxConnections: 5, // 最大连接数
maxMessages: 100, // 每个连接最大消息数
// 调试选项
debug: true, // 启用调试日志
});
// 验证传输器连接
transporter.verify(function(error, success) {
if (error) {
console.error('SMTP连接验证失败:', error);
} else {
console.log('SMTP服务器连接成功准备发送邮件');
}
});
// 存储IP地址最后发送邮件的时间
@ -72,7 +91,6 @@ app.post('/api/send-email', rateLimitMiddleware, async (req, res) => {
return res.status(400).json({ error: '请填写所有必填字段' });
}
try {
// 邮件选项
const mailOptions = {
from: `"个人网站联系表单" <${process.env.EMAIL_USER}>`,
@ -90,13 +108,56 @@ app.post('/api/send-email', rateLimitMiddleware, async (req, res) => {
replyTo: email
};
// 发送邮件
await transporter.sendMail(mailOptions);
// 最大重试次数
const maxRetries = 3;
let retries = 0;
let lastError = null;
return res.status(200).json({ success: true, message: '邮件已成功发送' });
// 重试发送邮件的函数
const attemptSendMail = async () => {
try {
const info = await transporter.sendMail(mailOptions);
console.log('邮件发送成功:', info.messageId);
return true;
} catch (error) {
console.error('发送邮件时出错:', error);
return res.status(500).json({ error: '发送邮件失败,请稍后再试' });
lastError = error;
console.error(`发送邮件失败 (尝试 ${retries + 1}/${maxRetries}):`, error);
return false;
}
};
try {
// 尝试发送邮件最多重试maxRetries次
let success = false;
while (retries < maxRetries && !success) {
success = await attemptSendMail();
if (!success) {
retries++;
if (retries < maxRetries) {
// 等待一段时间后重试,每次等待时间增加
const waitTime = 1000 * retries; // 1秒, 2秒, 3秒...
console.log(`等待 ${waitTime}ms 后重试...`);
await new Promise(resolve => setTimeout(resolve, waitTime));
}
}
}
if (success) {
return res.status(200).json({ success: true, message: '邮件已成功发送' });
} else {
// 所有重试都失败了
console.error('所有重试都失败,最后一个错误:', lastError);
return res.status(500).json({
error: '发送邮件失败,请稍后再试',
details: lastError ? lastError.message : '未知错误'
});
}
} catch (error) {
console.error('发送邮件过程中出现异常:', error);
return res.status(500).json({
error: '发送邮件过程中出现异常',
details: error.message
});
}
});

View File

@ -1,15 +1,15 @@
{
"files": {
"main.css": "/static/css/main.e6c13ad2.css",
"main.js": "/static/js/main.780215db.js",
"main.js": "/static/js/main.411478f7.js",
"static/js/453.b21437c2.chunk.js": "/static/js/453.b21437c2.chunk.js",
"index.html": "/index.html",
"main.e6c13ad2.css.map": "/static/css/main.e6c13ad2.css.map",
"main.780215db.js.map": "/static/js/main.780215db.js.map",
"main.411478f7.js.map": "/static/js/main.411478f7.js.map",
"453.b21437c2.chunk.js.map": "/static/js/453.b21437c2.chunk.js.map"
},
"entrypoints": [
"static/css/main.e6c13ad2.css",
"static/js/main.780215db.js"
"static/js/main.411478f7.js"
]
}

View File

@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.780215db.js"></script><link href="/static/css/main.e6c13ad2.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.411478f7.js"></script><link href="/static/css/main.e6c13ad2.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long