Spaces:
Sleeping
Sleeping
Codex commited on
Commit ·
533dfc4
1
Parent(s): 19d2058
Add SMTP send timeout and log send report details
Browse files
server.js
CHANGED
|
@@ -28,9 +28,17 @@ const CHECK_INTERVAL = 60000; // 1 minute
|
|
| 28 |
const SMTP_HOST = process.env.SMTP_HOST || 'smtp-relay.brevo.com';
|
| 29 |
const SMTP_PORT = parseInt(process.env.SMTP_PORT || '587', 10);
|
| 30 |
const SMTP_SECURE = process.env.SMTP_SECURE === 'true' || SMTP_PORT === 465;
|
|
|
|
| 31 |
let emailTransport = null;
|
| 32 |
let emailConfigChecked = false;
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
function getEmailTransport() {
|
| 35 |
if (emailTransport || emailConfigChecked) return emailTransport;
|
| 36 |
|
|
@@ -111,7 +119,7 @@ async function sendAlertEmail(statusEntry) {
|
|
| 111 |
const emailFrom = process.env.EMAIL_FROM;
|
| 112 |
const emailTo = process.env.EMAIL_TO;
|
| 113 |
|
| 114 |
-
await
|
| 115 |
from: emailFrom,
|
| 116 |
to: emailTo,
|
| 117 |
subject,
|
|
@@ -231,8 +239,9 @@ async function sendStatusReportEmail() {
|
|
| 231 |
}
|
| 232 |
|
| 233 |
const { subject, text, html } = buildStatusReportEmail();
|
|
|
|
| 234 |
|
| 235 |
-
await
|
| 236 |
from: emailFrom,
|
| 237 |
to: emailTo,
|
| 238 |
subject,
|
|
|
|
| 28 |
const SMTP_HOST = process.env.SMTP_HOST || 'smtp-relay.brevo.com';
|
| 29 |
const SMTP_PORT = parseInt(process.env.SMTP_PORT || '587', 10);
|
| 30 |
const SMTP_SECURE = process.env.SMTP_SECURE === 'true' || SMTP_PORT === 465;
|
| 31 |
+
const SMTP_TIMEOUT = parseInt(process.env.SMTP_TIMEOUT_MS || '15000', 10);
|
| 32 |
let emailTransport = null;
|
| 33 |
let emailConfigChecked = false;
|
| 34 |
|
| 35 |
+
async function sendMailWithTimeout(transport, mailOptions, timeoutMs = SMTP_TIMEOUT) {
|
| 36 |
+
return await Promise.race([
|
| 37 |
+
transport.sendMail(mailOptions),
|
| 38 |
+
new Promise((_, reject) => setTimeout(() => reject(new Error('SMTP send timed out')), timeoutMs))
|
| 39 |
+
]);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
function getEmailTransport() {
|
| 43 |
if (emailTransport || emailConfigChecked) return emailTransport;
|
| 44 |
|
|
|
|
| 119 |
const emailFrom = process.env.EMAIL_FROM;
|
| 120 |
const emailTo = process.env.EMAIL_TO;
|
| 121 |
|
| 122 |
+
await sendMailWithTimeout(transport, {
|
| 123 |
from: emailFrom,
|
| 124 |
to: emailTo,
|
| 125 |
subject,
|
|
|
|
| 239 |
}
|
| 240 |
|
| 241 |
const { subject, text, html } = buildStatusReportEmail();
|
| 242 |
+
console.log(`Sending status report email via ${SMTP_HOST}:${SMTP_PORT} secure=${SMTP_SECURE} as ${smtpUser} to ${emailTo}`);
|
| 243 |
|
| 244 |
+
await sendMailWithTimeout(transport, {
|
| 245 |
from: emailFrom,
|
| 246 |
to: emailTo,
|
| 247 |
subject,
|