email worker webhook not working

I am currently trying to relay emails sent to a worker to my webhook. it works fine when i send a test on the cloudflare site but when a real email is sent it doesnt send anything to my webhook. in the overview tab it shows that the email was received (although it says it was dropped ive read in other threads that that's just a default response) nothing is sent to the webhook. I am in control of the server and have put as many things as i can think of to log or find out what the issue is but ive come to the conclusion that it just isnt sending the request. anyone have any idea how to fix it?
No description
No description
61 Replies
Chaika
Chaikaā€¢9mo ago
The most helpful tool you have for debugging live workers is Tailing them, either via wrangler tail or the dashboard, in the Worker under Logs -> Begin Log Stream A few things I see: Why is that http and not https? Are you specifying a port? Custom Ports would get dropped I doubt you can just stringify the message, most likely you would at least lose the body Yea JSON doesn't know how to serialize the message object, it just returns {}. You can just pass the message body itself as it's a readable stream, like this:
Gute Nacht
Gute Nachtā€¢9mo ago
im using http because its not a domain its my vps ip yes i am specifcying a port
Chaika
Chaikaā€¢9mo ago
You can't fetch raw IPs in Workers, have to be domains
Gute Nacht
Gute Nachtā€¢9mo ago
well thats the issue then
Chaika
Chaikaā€¢9mo ago
and yea ports just get dropped unless the Worker is executing on the same zone as the fetch target (ex. a Worker running on worker.example.com can fetch origin.example.com with a custom port (with some restrictions), but Email Workers execute on your workers.dev so it'll just always drop it
Chaika
Chaikaā€¢9mo ago
No description
Chaika
Chaikaā€¢9mo ago
zone = website in Cloudflare, ex. anything under the "Websites" tab (Just including that flow chart for completeness, like I said none of that really matters to you other then the fact ports just aren't supported in email worker fetches since it'll never be same-zone) As for the worker code itself, you can use something like this:
export default {
async email(message, env, ctx) {
await fetch("https://yourwebserver.example.com", {
body: message.raw,
method: "POST",
headers: {
"email-to": message.to,
"email-from": message.from,
"apikeyorsomeidentifideryouuse": env.secret,
}
});
}
}
export default {
async email(message, env, ctx) {
await fetch("https://yourwebserver.example.com", {
body: message.raw,
method: "POST",
headers: {
"email-to": message.to,
"email-from": message.from,
"apikeyorsomeidentifideryouuse": env.secret,
}
});
}
}
That would forward the email, and the from/to properties, to a target. You could then just read the request body to get the raw email
Gute Nacht
Gute Nachtā€¢9mo ago
tjaml ypu chjaika
Chaika
Chaikaā€¢9mo ago
no worries. Email Workers are super cool but don't have the most examples and such around them right now, can be a bit hard to get started. If your intent is to use your VPS for actual production use, Cloudflare offers Origin Certificates under your website -> SSL/TLS -> Origin Server, they work with proxy enabled. You can create a DNS A Record to point at your server, create and install an origin ca certificate on it, and then use that to pass the email event to it. If you run into any issues, remember that Tail is your best bet when debugging deployed Workers. npx wrangler tail if you're using Wrangler or go to the Worker in the dashboard, Logs -> Real-time Logs -> Begin Log Stream, and wait a few seconds and then send the email. You should see the email event after a bit and any logs/exceptions that occured.
Gute Nacht
Gute Nachtā€¢9mo ago
@Chaika any ideas?
No description
No description
No description
Chaika
Chaikaā€¢9mo ago
That doesn't send JSON, that sends just the raw email Looks like your using Express with body-parser which thinks its JSON, probably because you're setting Content-type json or something else
Gute Nacht
Gute Nachtā€¢9mo ago
mesage.raw does the same thing
Chaika
Chaikaā€¢9mo ago
message.raw isn't JSON either
Gute Nacht
Gute Nachtā€¢9mo ago
is there a way for me to use like axios for this or a different library ive never used fetch before šŸ˜­
Chaika
Chaikaā€¢9mo ago
no but it wouldn't help I would drop the Content-Type: 'application/json' everything else though is on Express's end
Want results from more Discord servers?
Add your server