Track Amazon AWS SES email bounce and complains with ColdFusion

In the previous post I talked about how to send emails using Amazon SES (Simple Email Service) SendRawEmail call and fetch the message-Id assigned to the email by Amazon mail servers. With that ID in our hand, if a mail bounced back into our email box, we can use CFPOP to fetch that mail, fish out the message-id and update our mail table's matching record.

Amazon SNS (simple notification service) make this process bit neater and with that we can create a notification topic, select our own web page as a calling point. With SES (Simple Email Service) we can set our SNS notification topic as the return message location. That mean instead mail bounces into our email box, Amazon will call one of our web pages with message details.

First create a CFM page in your site (let's call MailReturn.cfm)  and copy and paste bellow code at the end of the page. 

Next Create an (SNS) Notification Topic and point to the MailReturn.cfm file in your site.

Amazon sends a request to this page with a "URL", that we suppose to "click on" and confirm we want to receive messages and also to verify we are the owner of that page. The code bellow will automatically handle that bit, it will fetch the URL and call it back with CFHTTP. But once the verification is done, you should comment out that potion.

Next choose the notification topic as the return location for your email.

 

Now if a mail returned it will go to the SNS topic and that will call our web page MailReturn.cfm.  In this page I separated most useful four variables sns.type, sns.subtype, sns.Recipients and sns.MessageId.

Sns.type contains either " complaint", " out_of_office" or " bounce".
Sns.subtype hold whatever the sub types belong the main types.
Sns.Recipients is an array of emails responsible for this return.

To keep things safe, our end-point page should only accept requests from AWS, not any other requests over the internet. Amazon sends signature with every request that we suppose decode and verify against. I did not had time to sort that bit out yet, so I add IP verification to the page for the time been.