Email Worker

Hi! I have a email worker set up to send a post request to my server when one of my emails recieves an email. For some reason the email is getting dropped instead of going to the worker. Is there any reason this might be happening?
21 Replies
Cyb3r-Jak3
Cyb3r-Jak39mo ago
Are you seeing dropped in the dashboard? That’s expected with workers
KNOX
KNOX9mo ago
Yeah. when i send an email from my gmail to the email created on cloudflare just to see if it actually works, it doesn't get sent to my server. it just gets dropped. here is the code that i have
export default {
async email(message, env, ctx) {
console.log(message.plainBody);
const webhookUrl = 'https://mail.logick.live/incoming';
const webhookPayload = JSON.stringify({
subject: message.headers.get('subject'),
from: message.from,
to: message.to,
body: message.content
});
try {
const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: webhookPayload,
});
} catch (error) {
console.error('Error sending webhook request:', error);
}
}
}
export default {
async email(message, env, ctx) {
console.log(message.plainBody);
const webhookUrl = 'https://mail.logick.live/incoming';
const webhookPayload = JSON.stringify({
subject: message.headers.get('subject'),
from: message.from,
to: message.to,
body: message.content
});
try {
const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: webhookPayload,
});
} catch (error) {
console.error('Error sending webhook request:', error);
}
}
}
Would you know any way to prevent the drop or is it just something I have to deal with?
Cyb3r-Jak3
Cyb3r-Jak39mo ago
Are you getting an tail logs from the worker?
KNOX
KNOX9mo ago
this? sorry im new to worker
No description
KNOX
KNOX9mo ago
No description
Cyb3r-Jak3
Cyb3r-Jak39mo ago
You seem to have an high error rate. I would suggest starting a real-time logging session then send an email to see what error you are getting
KNOX
KNOX9mo ago
ok ill send the results in a sec.
{
"outcome": "ok",
"scriptName": "sendtoserver",
"diagnosticsChannelEvents": [],
"exceptions": [],
"logs": [
{
"message": [
null
],
"level": "log",
"timestamp": 1694476362979
},
{
"message": [
"Error sending webhook request:",
"TypeError: Too many redirects.https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming"
],
"level": "error",
"timestamp": 1694476365951
}
],
"eventTimestamp": 1694476362959,
"event": {
"rawSize": 5401,
"rcptTo": "knox@logick.live",
"mailFrom": "me@gmail.com"
},
"id": 0
}
{
"outcome": "ok",
"scriptName": "sendtoserver",
"diagnosticsChannelEvents": [],
"exceptions": [],
"logs": [
{
"message": [
null
],
"level": "log",
"timestamp": 1694476362979
},
{
"message": [
"Error sending webhook request:",
"TypeError: Too many redirects.https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming, https://mail.logick.live/incoming"
],
"level": "error",
"timestamp": 1694476365951
}
],
"eventTimestamp": 1694476362959,
"event": {
"rawSize": 5401,
"rcptTo": "knox@logick.live",
"mailFrom": "me@gmail.com"
},
"id": 0
}
Cyb3r-Jak3
Cyb3r-Jak39mo ago
Looks like you are getting the email fine but it seems you are being redirected by the server you are trying to send the webhook to
KNOX
KNOX9mo ago
yeah. let me see if i can debug on my server because currently this is how i am receiving the webhook
app.post("/incoming", async (req, res) => {
console.log(req)
console.log(req.body)
if(req.body.length > 0){
res.send({response: "ok"})
}
})
app.post("/incoming", async (req, res) => {
console.log(req)
console.log(req.body)
if(req.body.length > 0){
res.send({response: "ok"})
}
})
when i edit the worker an use the testing tool it works just fine. but them when i try it by actually emailing the email, it redirects to many times.
Cyb3r-Jak3
Cyb3r-Jak39mo ago
Oh are these both workers on your zone?
KNOX
KNOX9mo ago
the app.post code is on my server that hosts my website. the exports default code that i sent awaile ago is the worker code sorry if im not understanding your questions
Cyb3r-Jak3
Cyb3r-Jak39mo ago
If it is a second worker then you want a service binding https://developers.cloudflare.com/workers/configuration/bindings/about-service-bindings/ as they are way to make requests between workers on the same account
KNOX
KNOX9mo ago
it's not two different workers sorry. this is the only worker that handles emails
export default {
async email(message, env, ctx) {
console.log(message.plainBody);
const webhookUrl = 'https://mail.logick.live/incoming';
const webhookPayload = JSON.stringify({
subject: message.headers.get('subject'),
from: message.from,
to: message.to,
body: message.content
});
try {
const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: webhookPayload,
});
} catch (error) {
console.error('Error sending webhook request:', error);
}
}
}
export default {
async email(message, env, ctx) {
console.log(message.plainBody);
const webhookUrl = 'https://mail.logick.live/incoming';
const webhookPayload = JSON.stringify({
subject: message.headers.get('subject'),
from: message.from,
to: message.to,
body: message.content
});
try {
const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: webhookPayload,
});
} catch (error) {
console.error('Error sending webhook request:', error);
}
}
}
while the following code is my servers code for my website.
app.post("/incoming", async (req, res) => {
console.log(req)
console.log(req.body)
if(req.body.length > 0){
res.send({response: "ok"})
}
})
app.post("/incoming", async (req, res) => {
console.log(req)
console.log(req.body)
if(req.body.length > 0){
res.send({response: "ok"})
}
})
Cyb3r-Jak3
Cyb3r-Jak39mo ago
Wait I'm confused. You have the same worker with two triggers and you are sending messages with POST between them?
KNOX
KNOX9mo ago
no 😅 ok so the first set of code is the email worker that handles emails. the second set of code is hosted on my vps that holds my website using express js. the app.post("/incoming") is what worker send a request to. the worker is on cloudflare. the app.post is on my own server separate from cloudflare
Cyb3r-Jak3
Cyb3r-Jak39mo ago
Ah sorry
KNOX
KNOX9mo ago
its ok, i didn't explain it very well 😅
Cyb3r-Jak3
Cyb3r-Jak39mo ago
Do you have any code in the vps that does redirects? Something there is redirecting the worker
KNOX
KNOX9mo ago
not that i see. let me take another look nothing is redrecting. let me run a few test i figured out the issue. i had to get creative and use iFTTT. my server wasnt wanting to accept the post from cloudflare
Cyb3r-Jak3
Cyb3r-Jak39mo ago
That's bizarre. Glad you got it fixed
KNOX
KNOX9mo ago
ikr!! Thank you for all your help!!!!