Knowledge Base/Help from the Community/Tips & Tricks

Implementing Bounce Notification For Multiple From Addresses

Anthony
posted this on December 08, 2010 04:32

Hi all,

We send emails from multiple FROM addresses, and it is important that email bounces get sent back to the person sending the message. After a bit of digging around in the SendGrid SMTP API & Event Notification API we managed to implement a solution. Here's how:

  1. When sending your emails, you need to add an X-SMTPAPI header to each message which includes the from address of the sender. I also recommend including the subject as this is useful for when you send the bounce response back to the sender.

    The format of the X-SMTPAPI header is important. The value of the header must be in JSON format & look like this: { "unique_args" : { "from" : "from_email_address", "subject" : "subject_line" }}. The "from" & "subject" parameters can be different, that's just what we did - but the important bit is the unique_args wrapper.

    There is documentation & sample code on how to add the header here: http://wiki.sendgrid.com/doku.php?id=smtp_api
  2. You need to create a publicly accessible page on your site that accepts a HTTP POST from the SendGrid Event Notification API (http://wiki.sendgrid.com/doku.php?id=event_api). There is sample code on the wiki. The important thing to note here is that SendGrid will also POST the custom header values you specified in the X-SMTPAPI header above ("from" & "subject" in our case). These are the key to sending your bounce response.

    Within your custom page, you can read the form values for email (the failed address), from (your custom header with the sender address), reason (the reason for the failure), and if you've added it, subject. With all this you can build an email message and send it back to the sender. You may also want to check the event form value to only process events of type bounce.
  3. The final step is to hook up your notification POST page to the SendGrid API, just log into your account and go to Developers > Event Notification.

Cheers,
Anthony.

 

Comments

User photo
Marcelo

Thanks for the great tip! It worked beautifully!

I tweaked the PHP API example for sending emails by adding the UniqueArgs as follows: 

$from = "yourname@yourdomain";

$subject = "This is the email subject line";

$hdr->setUniqueArgs(array('from'=>$from, 'subject'=>$subject));

This adds the extra information (from and subject) to the outgoing email which in turn is available as $_POST['from'] and $_POST['subject'] variables in the PHP script that monitors events available here: http://sendgrid.com/documentation/display/api/Event

Thanks,

 

Marcelo Ramagem

June 16, 2011 08:37
User photo
Kyle Kern
SendGrid

Another solution is to enable bounce forwarding on your account and leave the email address field blank, this will send all bounce notifications back to the senders "From" address.

An article was created that explains this more in depth this can be found here: 

http://support.sendgrid.com/entries/21838602-bounce-reports-notific...

Thanks

Kyle Kern

SendGrid Support

August 13, 2012 09:02
User photo
Neophyte

Hi Kyle,

How can I enable the bounce forwarding? At what settings in admin?

January 07, 2013 18:35
User photo
Kyle Kern
SendGrid

Neophyte,

In order to enable bounce forwarding on your SendGrid account, log in then go to email reports>bounces>settings, then just check the box for " Enable bounces forwarding"

Or you can follow this link http://sendgrid.com/bounces/bounceManagement

If you have any problems our support team can help you out, just send an email to support@sendgrid.com 

Thanks!

Kyle Kern

SendGrid Support

January 08, 2013 07:45