Skip to content

Commit 5d1a7c7

Browse files
committed
Update email worker to reply to too long emails
1 parent 0cd37cc commit 5d1a7c7

File tree

5 files changed

+110
-7
lines changed

5 files changed

+110
-7
lines changed

email-worker/package-lock.json

Lines changed: 78 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

email-worker/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"wrangler": "^4.20.3"
1212
},
1313
"dependencies": {
14-
"postal-mime": "^2.4.3"
14+
"postal-mime": "^2.4.3",
15+
"mimetext": "^3.0.27"
1516
}
1617
}

email-worker/src/worker.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
const API_ENDPOINT = "https://api.mxgo.ai/process-email";
33
// const API_ENDPOINT = "https://f9cb-2401-4900-1f27-802d-9dc8-c5a1-9434-c1f1.ngrok-free.app/process-email"
44
import PostalMime from 'postal-mime';
5+
import { EmailMessage } from "cloudflare:email";
6+
import { createMimeMessage } from "mimetext";
57

68
// Helper function to calculate size of base64 string
79
function base64Size(base64String) {
@@ -112,6 +114,32 @@ export default {
112114
responseText = "Your query is being processed.";
113115
} else if (response.status === 401) {
114116
responseText = "Please sign up first to use our service.";
117+
} else if (response.status === 413) {
118+
responseText = "Your email and attachments combined are too large to process. Please try with smaller attachments or contact support.";
119+
120+
// Send reply email for 413 error
121+
try {
122+
const replyMsg = createMimeMessage();
123+
replyMsg.setHeader("In-Reply-To", message.headers.get("Message-ID"));
124+
replyMsg.setSender({ name: "MXGo Support", addr: message.to });
125+
replyMsg.setRecipient(message.from);
126+
replyMsg.setSubject(`Re: ${subject}`);
127+
replyMsg.addMessage({
128+
contentType: 'text/plain',
129+
data: responseText
130+
});
131+
132+
const replyMessage = new EmailMessage(
133+
message.to,
134+
message.from,
135+
replyMsg.asRaw()
136+
);
137+
138+
await message.reply(replyMessage);
139+
console.log("Sent 413 error reply email to:", message.from);
140+
} catch (replyError) {
141+
console.error("Error sending reply email:", replyError);
142+
}
115143
} else {
116144
responseText = "There was an error processing your request. Please try again later.";
117145
}

email-worker/wrangler.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
name = "mxgo-email-worker"
22
main = "src/worker.js"
3-
compatibility_date = "2000-01-01"
3+
compatibility_date = "2024-09-23"
44
workers_dev = true
55
preview_urls = false
6-
compatibility_flags = [ "set_forwardable_email_full_headers" ]
7-
6+
compatibility_flags = [ "set_forwardable_email_full_headers", "nodejs_compat" ]

mxgo/api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ async def lifespan(_app: FastAPI):
121121
bearer_auth_scheme = HTTPBearer()
122122

123123

124-
125124
@app.get("/health")
126125
async def health_check():
127126
"""Health check endpoint for Docker and load balancers."""

0 commit comments

Comments
 (0)