Retrieving Message Details of a Bounced Email via Amazon SES/SNS

As data scientists or software engineers working with email-based communication systems, understanding bounced emails is crucial. In this post, we will focus on retrieving message details of a bounced email using Amazon Simple Email Service (SES) and Simple Notification Service (SNS).

Retrieving Message Details of a Bounced Email via Amazon SES/SNS

As data scientists or software engineers working with email-based communication systems, understanding bounced emails is crucial. In this post, we will focus on retrieving message details of a bounced email using Amazon Simple Email Service (SES) and Simple Notification Service (SNS).

What is a Bounced Email?

A bounced email is an email that fails to deliver to the recipient’s mailbox. This failure could be due to various reasons, including full mailboxes, inactive email accounts, or server issues.

Why is it Important to Track Bounced Emails?

Bounced emails impact your sender reputation. High bounce rates can lead to lowered email deliverability. As a best practice, it’s essential to remove bounced email addresses from your mailing list.

How to Retrieve Bounced Email Details Using Amazon SES/SNS?

Amazon SES provides a feature to handle bounced emails effectively. It integrates with Amazon SNS to send notifications about bounced emails. Let’s go through the steps to set this up.

Step 1: Setting up Amazon SNS Topic

First, you need to create an Amazon SNS Topic where SES will publish the bounce notifications.

aws sns create-topic --name BounceTopic

Make note of the TopicArn in the response.

Step 2: Setting up SES to Send Notifications to SNS

Next, you need to configure SES to send bounce notifications to the SNS Topic you created. Use the set-identity-notification-topic command and provide the TopicArn.

aws ses set-identity-notification-topic --identity "your-email@example.com" --notification-type Bounce --sns-topic "arn:aws:sns:us-west-2:123456789012:BounceTopic"

Step 3: Subscribing to the SNS Topic

Now, you need to create a subscription to your SNS Topic. This could be an HTTP/HTTPS endpoint, an email address, or a Lambda function.

aws sns subscribe --topic-arn "arn:aws:sns:us-west-2:123456789012:BounceTopic" --protocol https --notification-endpoint "https://your-endpoint.com/notifications"

Step 4: Retrieving Bounce Notifications

When an email bounces, SES will publish a message to your SNS Topic. This message contains details about the bounced email, including the bounce type, bounced recipients, and more.

Here’s an example of what a bounce notification might look like:

{
  "notificationType": "Bounce",
  "bounce": {
    "bounceType": "Permanent",
    "bounceSubType": "General",
    "bouncedRecipients": [
      {
        "emailAddress": "recipient@example.com",
        "action": "failed",
        "status": "5.1.1",
        "diagnosticCode": "smtp; 550 5.1.1 user unknown"
      }
    ],
    "timestamp": "2019-06-17T10:41:59.000Z",
    "feedbackId": "01000106c88d2092-8a1b1c88-00000000-00000000-00000000",
    "remoteMtaIp": "203.0.113.0",
    "reportingMTA": "dsn; a8-83.smtp-out.amazonses.com"
  },
  "mail": {
    "timestamp": "2019-06-17T10:41:59.000Z",
    "source": "sender@example.com",
    "sourceArn": "arn:aws:ses:us-west-2:123456789012:identity/sender@example.com",
    "sendingAccountId": "123456789012",
    "messageId": "0101016c89d1f926-8a1b1c88-00000000-00000000-00000000",
    "destination": [
      "recipient@example.com"
    ],
    "headersTruncated": false,
    "headers": [
      {
        "name": "From",
        "value": "sender@example.com"
      },
      {
        "name": "To",
        "value": "recipient@example.com"
      }
    ]
  }
}

This JSON object contains all the information you need to understand the bounce.

Conclusion

Tracking bounced emails and understanding their causes are essential for maintaining a healthy email system. By leveraging Amazon SES and SNS, we can automate this process and retrieve detailed information about bounced emails. Happy emailing!


Keywords: Amazon SES, Amazon SNS, Bounced Emails, Email Deliverability, AWS, Retrieve Bounced Email Details.

Meta Description: Learn how to retrieve bounced email details using Amazon SES and SNS. Improve your email deliverability by tracking and understanding bounced emails.


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.