#!/usr/bin/env bash
# Create a suppression entry for a recipient.
set -u
: "${MAILCHANNELS_API_KEY:?Set MAILCHANNELS_API_KEY (parent or sub-account) before running}"
: "${RECIPIENT:?Set RECIPIENT to the email address to suppress}"
# SUPPRESSION_TYPES is optional, a comma-separated list (e.g. "transactional,non-transactional").
# If unset, MailChannels uses the default "non-transactional".
TYPES_FIELD=""
if [ -n "${SUPPRESSION_TYPES:-}" ]; then
IFS=',' read -ra TYPES_ARR <<< "$SUPPRESSION_TYPES"
TYPES_JSON=""
for t in "${TYPES_ARR[@]}"; do
TYPES_JSON+=",\"$t\""
done
TYPES_FIELD=", \"suppression_types\": [${TYPES_JSON#,}]"
fi
curl -X POST https://api.mailchannels.net/tx/v1/suppression-list \
-H "Content-Type: application/json" \
-H "X-Api-Key: $MAILCHANNELS_API_KEY" \
-d "{ \"suppression_entries\": [{ \"recipient\": \"$RECIPIENT\"$TYPES_FIELD }] }"