#Email Worker

29 messages · Page 1 of 1 (latest)

coral wave
#

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?

heavy crater
#

Are you seeing dropped in the dashboard? That’s expected with workers

coral wave
# heavy crater Are you seeing dropped in the dashboard? That’s expected with workers

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);
    }
  }
}
coral wave
heavy crater
#

Are you getting an tail logs from the worker?

coral wave
#

this? sorry im new to worker

heavy crater
#

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

coral wave
#

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
}
heavy crater
#

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

coral wave
#

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"})
  }
})
#

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.

heavy crater
#

Oh are these both workers on your zone?

coral wave
#

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

heavy crater
coral wave
#

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);
    }
  }
}

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"})
  }
})
heavy crater
#

Wait I'm confused. You have the same worker with two triggers and you are sending messages with POST between them?

coral wave
#

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

heavy crater
#

Ah sorry

coral wave
#

its ok, i didn't explain it very well 😅

heavy crater
#

Do you have any code in the vps that does redirects? Something there is redirecting the worker

coral wave
#

not that i see. let me take another look

#

nothing is redrecting. let me run a few test

coral wave
heavy crater