Created
February 13, 2022 00:48
-
-
Save molomby/a862a0ee3c104090ebc8df8ce97fd689 to your computer and use it in GitHub Desktop.
Forward Email API: Wrapper Script for Creating Aliases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Usage: | |
# create-email-alias.sh $URL_OR_DESC [$SLUG] | |
# Example: | |
# create-email-alias.sh "https://www.darwinsailingclub.com.au" dsc | |
# Results in: | |
# Email [email protected] -> [email protected], comment: "https://www.darwinsailingclub.com.au" | |
# Environment variables can be supplied at run time or set in ~/.forward-email | |
# FORWARD_EMAIL_API_TOKEN The secure API token for your Forward Email account. | |
# FORWARD_EMAIL_MAILBOX The private mailbox emails should forwarded to. Assumed to support "+" aliasing. | |
# FORWARD_EMAIL_DOMAIN The public domain you have setup in Forward Email on which aliases should be created. | |
# Exit on error, empty vars error | |
set -eu | |
GREEN='\x1b[32m' | |
TEXT='\x1b[37m' | |
NC='\e[0m' | |
print_status () { | |
printf "\n${GREEN} ➔ ${TEXT}${1}${TEXT}\n"; | |
} | |
# Current dir | |
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
# Get local settings | |
VARS_PATH="${HOME}/.forward-email" | |
print_status "Sourcing vars from ${VARS_PATH}" | |
source "${VARS_PATH}" | |
# Check for required env vars | |
: "${FORWARD_EMAIL_API_TOKEN:?A API token must be supplied as FORWARD_EMAIL_API_TOKEN}" | |
: "${FORWARD_EMAIL_MAILBOX:?A target mailbox must be supplied as FORWARD_EMAIL_MAILBOX}" | |
: "${FORWARD_EMAIL_DOMAIN:?The forwarding domain must be supplied as FORWARD_EMAIL_DOMAIN}" | |
print_status "Using forwarding domain ${GREEN}${FORWARD_EMAIL_DOMAIN}${TEXT} and base mailbox ${GREEN}${FORWARD_EMAIL_MAILBOX}${TEXT}" | |
# Check we have a domain or description | |
: "${1:?A site url or description must be supplied as the 1st argument}" | |
URL_OR_DESC=${1} | |
# Extract a default slug from (what we assume is) the URL | |
UOD_NO_PROTO="${URL_OR_DESC##*//}" | |
UOD_NO_WWW="${UOD_NO_PROTO##www.}" | |
UOD_NO_TLD="${UOD_NO_WWW%%.*}" | |
# Remove bad chars (this also can potentially give us a decent slug if the value wasn't a url) | |
UOD_LOWER="$(echo "$UOD_NO_TLD" | tr '[:upper:]' '[:lower:]')" | |
UOD_SAFE="${UOD_LOWER//[^a-zA-Z0-9\-]/}" | |
SLUG=${2:-${UOD_SAFE}} | |
# Incorporate the slug into the forwarding address | |
FORWARD_ADDRESS="${FORWARD_EMAIL_MAILBOX/@/+${SLUG}@}" | |
# Incorporate the slug into the alias + random string | |
ALIAS="${SLUG}.$(head -c32 /dev/urandom | base64 | tr -dc 'a-z0-9' | head -c6)" | |
# Confirm | |
print_status "Creating alias ${GREEN}${ALIAS}@${FORWARD_EMAIL_DOMAIN}${TEXT} forwarding to ${GREEN}${FORWARD_ADDRESS}${TEXT} with the description ${GREEN}${URL_OR_DESC}${TEXT}\n Continue? (y/n)" | |
read -n 1 -r | |
if [[ ! $REPLY =~ ^[Yy]$ ]] | |
then | |
exit 1 | |
fi | |
# echo "FORWARD_EMAIL_DOMAIN : ${FORWARD_EMAIL_DOMAIN}" | |
# echo "ALIAS : ${ALIAS}" | |
# echo "FORWARD_ADDRESS : ${FORWARD_ADDRESS}" | |
# echo "URL_OR_DESC : ${URL_OR_DESC}" | |
# Capture the response | |
print_status "Calling API..." | |
RESPONSE=$( | |
curl -s -X POST "https://api.forwardemail.net/v1/domains/${FORWARD_EMAIL_DOMAIN}/aliases" \ | |
-u "${FORWARD_EMAIL_API_TOKEN}:" \ | |
--data-urlencode "name=${ALIAS}" \ | |
--data-urlencode "recipients=${FORWARD_ADDRESS}" \ | |
--data-urlencode "description=${URL_OR_DESC}" \ | |
--data-urlencode "is_enabled=true" | |
) | |
# Output | |
print_status "Done for ${GREEN}${ALIAS}@${FORWARD_EMAIL_DOMAIN}${TEXT}" | |
echo $RESPONSE | jq -r '{ id: .id, alias: .name, domain: .domain.name, recipient: .recipients[0], description: .description, enabled: .is_enabled }' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assumes you have
jq
installed.