Sendgrid Inbound Parse Webhook in loop

Hey there!
I am using Sendgrid to setup emails on a custom domain and Inbound Parse Webhook keeps getting in a loop and sending all the emails out multiple times and it won’t stop!
Here’s the config:


And the code:

app.post("/incomingmail",(req,res)=>{
  var from = req.body.from;
  var text = req.body.text;
  var subject = req.body.subject;
  var num_attachments = req.body.attachments;
  var i
  for (i = 1; i <= num_attachments; i++){
    var attachment = req.files['attachment' + i];
    // attachment will be a File object
  }
  console.log(req.body)
                  async function main(){
                let transporter = nodemailer.createTransport({
                  host: 'smtp.sendgrid.net',
                  port: 587,
                  secure: false,
                  ignoreTLS: true,
                  auth: {
                    user: "apikey",
                    pass: process.env.EMAILPASS
                  }
                });
                  // send mail with defined transport object
                let info = await transporter.sendMail({
                  from: 'My Email <emailredacted@mydomain.com>',
                  replyTo: from,// sender address
                  to: "contact@eddiestech.co.uk",
                  subject: subject+" | New Contact Message", // Subject line
                  html: text
                });
                }
                main().catch(console.error);

Anyone know what’s up here?

For reference:


https://sendgrid.com/docs/for-developers/tracking-events/nodejs-code-example/

tmw the guy who answers everyone’s questions asks his own questions and no one answers.

Just kidding. The only thing I can think of is the value of num_attachments is either a non-number variable or a massive number. Can you log num_attachments and see how big it is?

Edit - sorry, I don’t think the loop has anything to do with it. Did you check out the nodemailer docs?
Also how do you add custom tags? I’ve never been able to do it…

2 Likes

All my other nodemailer functions work fine except for this webhook thing

I’m assuming you mean for this topic. You need to be a Regular (TL3) user

1 Like

I see.

This might be unrelated to your problem, but the app.post function is missing a matching curly brace after (req, res)=>{ - there isn’t a matching one in the code block you pasted.

1 Like

Sorry my bad. Mustn’t had copied it in

Found out the reason was that I didn’t send a response back. So SendGrid saw the No Response Error and sent the POST request again until it got a response.
My bad :man_facepalming: :slight_smile:

1 Like