Skip to content

Instantly share code, notes, and snippets.

@uglyrobot
Created October 16, 2020 21:53
Show Gist options
  • Save uglyrobot/d29c51b88583086c7ecc94ddeacc917c to your computer and use it in GitHub Desktop.
Save uglyrobot/d29c51b88583086c7ecc94ddeacc917c to your computer and use it in GitHub Desktop.
When changing the domain you send from in Mailgun, it's a good idea to copy the existing suppression list to the new domain to prevent spikes in bounces and complaints.
#!/bin/bash
if [ -z "$1" ]
then
echo "Missing API Key argument!"
exit 1
fi
if [ -z "$2" ]
then
echo "Missing source domain!"
exit 1
fi
if [ -z "$3" ]
then
echo "Missing destination domain!"
exit 1
fi
APIKEY="api:$1"
echo "Processing Bounces"
NEXT_URL="https://api.mailgun.net/v3/$2/bounces?limit=100"
while [[ ! -z "$NEXT_URL" ]]; do
echo "Importing $NEXT_URL..."
RESULTS=$(curl -s --user "$APIKEY" -G "$NEXT_URL")
NEXT_URL=$(echo $RESULTS | jq -r '.paging.next')
ITEMS=$(echo $RESULTS | jq '.items')
if [[ "$ITEMS" = "[]" ]]
then
break
fi
curl --user "$APIKEY" --header "Content-Type: application/json" --request POST --data "$ITEMS" "https://api.mailgun.net/v3/$3/bounces"
done
echo "Processing Complaints"
NEXT_URL="https://api.mailgun.net/v3/$2/complaints?limit=100"
while [[ ! -z "$NEXT_URL" ]]; do
echo "Importing $NEXT_URL..."
RESULTS=$(curl -s --user "$APIKEY" -G "$NEXT_URL")
NEXT_URL=$(echo $RESULTS | jq -r '.paging.next')
ITEMS=$(echo $RESULTS | jq '.items')
if [[ "$ITEMS" = "[]" ]]
then
break
fi
curl --user "$APIKEY" --header "Content-Type: application/json" --request POST --data "$ITEMS" "https://api.mailgun.net/v3/$3/complaints"
done
echo "Processing Unsubscribes"
NEXT_URL="https://api.mailgun.net/v3/$2/unsubscribes?limit=100"
while [[ ! -z "$NEXT_URL" ]]; do
echo "Importing $NEXT_URL..."
RESULTS=$(curl -s --user "$APIKEY" -G "$NEXT_URL")
NEXT_URL=$(echo $RESULTS | jq -r '.paging.next')
ITEMS=$(echo $RESULTS | jq '.items')
if [[ "$ITEMS" = "[]" ]]
then
break
fi
curl --user "$APIKEY" --header "Content-Type: application/json" --request POST --data "$ITEMS" "https://api.mailgun.net/v3/$3/unsubscribes"
done
echo "Finished!"
@uglyrobot
Copy link
Author

Requires jq and curl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment