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](https://cdn.answeroverflow.com/1185774979283943524/image.png)
![No description](https://cdn.answeroverflow.com/1185774979502059591/image.png)
61 Replies
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:im using http because its not a domain its my vps ip
yes i am specifcying a port
You can't fetch raw IPs in Workers, have to be domains
well thats the issue then
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
![No description](https://cdn.answeroverflow.com/1185782530696294410/e9ae7e44c051cc5157a3e7ae2d30b274e6a57049_2_1035x543.png)
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:
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
tjaml ypu chjaika
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.@Chaika any ideas?
![No description](https://cdn.answeroverflow.com/1185793497941414009/image.png)
![No description](https://cdn.answeroverflow.com/1185793498193076305/image.png)
![No description](https://cdn.answeroverflow.com/1185793498402803722/image.png)
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
mesage.raw does the same thing
message.raw isn't JSON either
is there a way for me to use like
axios for this
or a different library
ive never used fetch before š
no but it wouldn't help
I would drop the Content-Type: 'application/json'
everything else though is on Express's end