Spaces:
Sleeping
Sleeping
Codex commited on
Commit ·
4992155
1
Parent(s): 10e9ccd
Surface Brevo API auth errors and avoid SMTP fallback on 401/403
Browse files
server.js
CHANGED
|
@@ -64,22 +64,25 @@ async function sendViaBrevoApi(message) {
|
|
| 64 |
htmlContent: message.html
|
| 65 |
};
|
| 66 |
|
| 67 |
-
await axios.post(
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
},
|
| 76 |
-
timeout: BREVO_API_TIMEOUT
|
| 77 |
-
}
|
| 78 |
-
);
|
| 79 |
|
| 80 |
console.log('Email sent via Brevo API');
|
| 81 |
}
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
function getEmailTransport() {
|
| 84 |
if (emailTransport || emailConfigChecked) return emailTransport;
|
| 85 |
|
|
@@ -165,7 +168,12 @@ async function sendEmail(message) {
|
|
| 165 |
await sendViaBrevoApi(message);
|
| 166 |
return;
|
| 167 |
} catch (err) {
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
}
|
| 170 |
}
|
| 171 |
|
|
|
|
| 64 |
htmlContent: message.html
|
| 65 |
};
|
| 66 |
|
| 67 |
+
await axios.post('https://api.brevo.com/v3/smtp/email', payload, {
|
| 68 |
+
headers: {
|
| 69 |
+
'api-key': apiKey,
|
| 70 |
+
accept: 'application/json',
|
| 71 |
+
'content-type': 'application/json'
|
| 72 |
+
},
|
| 73 |
+
timeout: BREVO_API_TIMEOUT
|
| 74 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
console.log('Email sent via Brevo API');
|
| 77 |
}
|
| 78 |
|
| 79 |
+
function describeBrevoError(err) {
|
| 80 |
+
const status = err?.response?.status;
|
| 81 |
+
const dataMsg = err?.response?.data?.message || err?.response?.data?.error || '';
|
| 82 |
+
const text = err?.message || String(err);
|
| 83 |
+
return { status, detail: dataMsg || text };
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
function getEmailTransport() {
|
| 87 |
if (emailTransport || emailConfigChecked) return emailTransport;
|
| 88 |
|
|
|
|
| 168 |
await sendViaBrevoApi(message);
|
| 169 |
return;
|
| 170 |
} catch (err) {
|
| 171 |
+
const info = describeBrevoError(err);
|
| 172 |
+
// If auth error, do not fallback to SMTP (likely wrong key) — surface error early
|
| 173 |
+
if (info.status === 401 || info.status === 403) {
|
| 174 |
+
throw new Error(`Brevo API auth error (${info.status}): ${info.detail}`);
|
| 175 |
+
}
|
| 176 |
+
console.error('Brevo API send failed, falling back to SMTP if available:', info.detail || err);
|
| 177 |
}
|
| 178 |
}
|
| 179 |
|