Handling Mailgun Bounce Notifications in MeteorJS

I’m working on a MeteorJS project and I’m stuck with Mailgun webhooks. I’ve set up a route to handle incoming webhook data, but I’m having trouble with the ‘bounced’ event. When this event occurs, the req.body is empty. I can’t figure out why.

Here’s a simplified version of my code:

Router.route('/webhook/mailgun', { where: 'server' })
.post(function() {
  const req = this.request;
  const res = this.response;
  
  if (req.body.event === 'bounced') {
    console.log('Bounce event received');
    console.log(req.body);  // This is empty
  }
  
  res.end('Webhook received');
});

I’ve tested other events like ‘delivered’ and ‘opened’, and they work fine. It’s just the ‘bounced’ event that’s giving me trouble. Any ideas what could be causing this? Is there something special about bounce notifications that I’m missing?

hey john, i ran into this too. mailgun sends bounces differently - it’s like a whole email attachment. try using something like ‘busboy’ to handle it. you’ll need to parse the MIME data. also double check your mailgun settings are set to send full bounce info. good luck!

I’ve encountered this issue before with Mailgun bounce notifications in MeteorJS. The problem likely stems from how Mailgun encodes bounce data. Unlike other events, bounces are often sent as MIME attachments.

To resolve this, you’ll need to modify your route to handle multipart form data. Consider using a package like ‘busboy’ or ‘multer’. Here’s a basic example:

import Busboy from 'busboy';

Router.route('/webhook/mailgun', { where: 'server' })
.post(function() {
  const req = this.request;
  const res = this.response;
  
  const busboy = new Busboy({ headers: req.headers });
  busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
    // Process the MIME data here
  });
  
  req.pipe(busboy);
  
  res.end('Webhook received');
});

Also, ensure your Mailgun webhook settings are configured to send full MIME data for bounces. This should solve your empty req.body issue for bounce events.

Having dealt with Mailgun webhooks in MeteorJS before, I can share some insights. The issue you’re facing with empty ‘bounced’ event data is likely due to how Mailgun sends bounce notifications. Unlike other events, bounce data is often sent as a MIME-encoded email attachment.

To handle this, you’ll need to parse the incoming data differently. I’d suggest using a package like ‘busboy’ or ‘multer’ to handle multipart form data. Here’s a rough idea of how you might modify your route:

import Busboy from 'busboy';

Router.route('/webhook/mailgun', { where: 'server' })
.post(function() {
  const req = this.request;
  const res = this.response;
  
  const busboy = new Busboy({ headers: req.headers });
  busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
    // Handle the attached MIME data here
  });
  
  req.pipe(busboy);
  
  res.end('Webhook received');
});

This approach should allow you to access the bounce data. Remember to adjust your Mailgun webhook settings to send full MIME data for bounces if it’s not already configured that way.