AWS SES exporting the suppression list as a CSV using GO Lang

~ 2 min read

Pre-requisites:

  • Go Lang is installed on your computer
  • Your AWS access key and secret configured as a profile in ~/.aws/credentials

Writing the code

Create a directory for the code, in it create a file called awsSesGetSuppressionList.go and paste in the following code. You may choose to change the hard coded AWS Region and AWS profile or set them to blank strings, so they can be set via environment variables of AWS_REGION and AWS_PROFILE respectively.

package main

import (
	"encoding/csv"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/sesv2"
	"log"
	"os"
)

func main() {
	// Initialize a session in eu-west-2 that the SDK will use to load
	// credentials from the shared credentials file ~/.aws/credentials.
	sess, err := session.NewSession(&aws.Config{
		Region:      aws.String("eu-west-2"),                         // Or leave blank and specify with env AWS_REGION=
		Credentials: credentials.NewSharedCredentials("", "default"), // Or leave blank and specify with env AWS_PROFILE=
	},
	)

	// Create SESv2 service client
	client := sesv2.New(sess)

	// Example iterating over at most 2 pages of a ListSuppressedDestinations operation.
	pageNum := 0
	params := sesv2.ListSuppressedDestinationsInput{}
	w := csv.NewWriter(os.Stdout)
	err = w.Write([]string{"email", "reason", "datetime"})
	err = client.ListSuppressedDestinationsPages(&params,
		func(page *sesv2.ListSuppressedDestinationsOutput, lastPage bool) bool {
			pageNum++
			for _, value := range page.SuppressedDestinationSummaries {
				email, reason, updated := *value.EmailAddress, *value.Reason, *value.LastUpdateTime
				row := []string{email, reason, updated.String()}
				if err := w.Write(row); err != nil {
					log.Fatalln("error writing record to csv:", err)
				}
			}
			return lastPage == false
		})

	if err != nil {
		return
	}

	// Write any buffered data to the underlying writer (standard output).
	w.Flush()

	if err := w.Error(); err != nil {
		log.Fatal(err)
	}
}

Then on the command line in the same directory as the code run go build and you should have a binary executable file in the same directory called awsSesGetSuppressionList i.e. no file extension.

Retrieving the suppression list from AWS

Open a terminal window in the directory containing your code, then type

AWS_PROFILE=default; AWS_REGION=eu-west-2; ./awsSesGetSuppressionList >suppressed.csv

Which will pull down the suppression list from region eu-west-2 using the default AWS credentials and place the results in the file supressed.csv which can then be imported into the relevant tool to mark emails as undeliverable at source rather than have AWS suppression them.

all posts →