Skip to content

Instantly share code, notes, and snippets.

@MPJHorner
Created July 2, 2021 13:02
Show Gist options
  • Save MPJHorner/f333de0a386f989131618b022fa22bd4 to your computer and use it in GitHub Desktop.
Save MPJHorner/f333de0a386f989131618b022fa22bd4 to your computer and use it in GitHub Desktop.
Amazon SES Suppression to CSV and Removal Tool (HACKY AF)
// Pleas excuse how messy and hacky AF this is... It works though.
// Please ensure you have local auth setup to AWS CLI already, otherwise you made need to modify the config bits to include some auth...
// import { SESv2Client, ListSuppressedDestinationsCommand } from "@aws-sdk/client-sesv2"; // ES Modules import
const { SESv2Client, ListSuppressedDestinationsCommand, DeleteSuppressedDestinationCommand } = require("@aws-sdk/client-sesv2"); // CommonJS import
const converter = require('json-2-csv');
const fs = require('fs');
const config = {};
const save = true; //Set to FALSE to stop saving results to CSV
const filter = false; //Set to TRUE to filter results
const remove = false; //Set to TRUE to remove the results from supression
const client = new SESv2Client(config);
async function fetchSupressed(nextToken = null){
const input = {};
if(nextToken){
input.NextToken = nextToken;
}
const command = new ListSuppressedDestinationsCommand(input);
let response = await client.send(command);
if(!response.NextToken){
loadMore = false;
}else{
nextToken = response.NextToken;
}
emails.push(...response.SuppressedDestinationSummaries)
count++;
await new Promise(r => setTimeout(r, 2000));
return nextToken;
}
async function removeSuppressed(email){
const input = {
'EmailAddress': email
}
const command = new DeleteSuppressedDestinationCommand(input);
const response = await client.send(command);
if(response.$metadata.httpStatusCode == 200){
console.log('[SUCCESS] ' + email + ' removed...');
}else{
console.log('[ERROR] ' + email + ' something went wrong...');
console.log(response);
}
await new Promise(r => setTimeout(r, 1000));
return true;
}
const emails = [];
let loadMore = true;
let nextToken = null;
let count = 0;
async function fetchLoop(){
while(loadMore == true){
let response = await fetchSupressed(nextToken);
nextToken = response;
}
}
async function removeLoop(emails){
for (const element of emails){
let response = await removeSuppressed(element.EmailAddress);
}
}
let json2csvCallback = function (err, csv) {
if (err) throw err;
fs.writeFile('name.csv', csv, 'utf8', function(err) {
if (err) {
console.log('Some error occured - file either not saved or corrupted file saved.');
} else {
console.log('It\'s saved!');
}
});
};
let toCsv = function(data){
let myObj = {
"rows": data
}
converter.json2csv(myObj.rows, json2csvCallback, {
prependHeader: false
});
}
fetchLoop().then(function(){
console.log('Counting emails...');
console.log(emails.length);
let filteredEmails = [];
if(filter) {
filteredEmails = emails.filter(function callbackFn(element) {
if (element.EmailAddress.includes('@icloud.com') && element.Reason == 'BOUNCE') {
return true;
} else {
return false;
}
});
console.log(filteredEmails.length)
}
if(save){
if(filteredEmails.length > 0){
toCsv(filteredEmails);
}else{
toCsv(emails);
}
}
if(remove){
if(filteredEmails.length == 0 && !filter){
removeLoop(emails);
}else{
removeLoop(filteredEmails);
}
}
})
{
"name": "amazon-aws-ses-v2-supression-remover",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@aws-sdk/client-sesv2": "^3.19.0",
"fs": "*",
"json-2-csv": "^3.14.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment