Added script

This commit is contained in:
Phil 2026-03-10 14:11:57 +00:00
parent e61a2866f4
commit f6cfd3a94f
2 changed files with 189 additions and 0 deletions

175
generate_email_alias.sh Executable file
View File

@ -0,0 +1,175 @@
#!/usr/bin/env bash
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load configuration
source "$SCRIPT_DIR/mailcow.conf"
# Clipboard helper function
copy_to_clipboard() {
if command -v xclip >/dev/null 2>&1; then
echo -n "$1" | xclip -selection clipboard
echo "(Copied to clipboard via xclip)"
elif command -v wl-copy >/dev/null 2>&1; then
echo -n "$1" | wl-copy
echo "(Copied to clipboard via wl-copy)"
elif command -v pbcopy >/dev/null 2>&1; then
echo -n "$1" | pbcopy
echo "(Copied to clipboard via pbcopy)"
fi
}
# Create a new alias
create_alias() {
local SITE="$1"
local DOMAIN_CHOICE="$2"
if [ -z "$SITE" ]; then
echo "Error: You must provide a site name or identifier."
exit 1
fi
# Select domain
if [ -n "$DOMAIN_CHOICE" ]; then
SELECTED_DOMAIN="$DOMAIN_CHOICE"
else
echo "Select a domain:"
for i in "${!DOMAINS[@]}"; do
echo "$((i+1))) ${DOMAINS[$i]}"
done
read -rp "Enter choice number: " choice
INDEX=$((choice-1))
if [[ $INDEX -ge 0 && $INDEX -lt ${#DOMAINS[@]} ]]; then
SELECTED_DOMAIN="${DOMAINS[$INDEX]}"
else
echo "Invalid choice. Defaulting to ${DOMAINS[0]}"
SELECTED_DOMAIN="${DOMAINS[0]}"
fi
fi
local RAND=$(tr -dc a-z0-9 </dev/urandom | head -c ${RANDOM_LENGTH})
local ALIAS="${SITE}-${RAND}@${SELECTED_DOMAIN}"
echo "Creating alias: $ALIAS -> $REAL_EMAIL"
RESPONSE=$(curl -s -X POST "${MAILCOW_URL}/api/v1/add/alias" \
-H "X-API-Key: ${API_KEY}" \
-H "Content-Type: application/json" \
-d "{
\"address\": \"${ALIAS}\",
\"goto\": \"${REAL_EMAIL}\",
\"active\": \"1\",
\"public_comment\": \"aliasgen ${SITE}\"
}")
if echo "$RESPONSE" | grep -q "error"; then
echo "Error creating alias: $RESPONSE"
else
echo "Alias created: $ALIAS"
copy_to_clipboard "$ALIAS"
fi
}
# List all aliases
list_aliases() {
curl -s -X GET "${MAILCOW_URL}/api/v1/get/alias/all" \
-H "X-API-Key: ${API_KEY}" \
| jq -r '.[] | "\(.address) -> \(.goto) [active: \(.active)]"' \
| column -t
}
# Search aliases by keyword
search_aliases() {
local TERM="$1"
if [ -z "$TERM" ]; then
echo "Error: Please provide a search term."
exit 1
fi
curl -s -X GET "${MAILCOW_URL}/api/v1/get/alias/all" \
-H "X-API-Key: ${API_KEY}" \
| jq -r --arg TERM "$TERM" '.[] | select(.address | test($TERM)) | "\(.address) -> \(.goto) [active: \(.active)]"' \
| column -t
}
# Delete alias
delete_alias() {
local ALIAS="$1"
if [ -z "$ALIAS" ]; then
echo "Error: Please provide the alias to delete."
exit 1
fi
RESPONSE=$(curl -s -X POST "${MAILCOW_URL}/api/v1/delete/alias" \
-H "X-API-Key: ${API_KEY}" \
-H "Content-Type: application/json" \
-d "{
\"address\": \"${ALIAS}\"
}")
echo "Response: $RESPONSE"
}
# Disable alias (sets active=0)
disable_alias() {
local ALIAS="$1"
if [ -z "$ALIAS" ]; then
echo "Error: Please provide the alias to disable."
exit 1
fi
RESPONSE=$(curl -s -X POST "${MAILCOW_URL}/api/v1/edit/alias" \
-H "X-API-Key: ${API_KEY}" \
-H "Content-Type: application/json" \
-d "{
\"address\": \"${ALIAS}\",
\"active\": \"0\"
}")
echo "Response: $RESPONSE"
}
# Enhanced help menu
show_help() {
cat <<EOF
Mailcow Alias CLI - aliasgen
Usage: aliasgen <command> [arguments]
Commands:
create <site> Create a new alias for a website or service
Example: aliasgen create amazon
Generates: amazon-abc12@domain.com -> $REAL_EMAIL
list List all aliases with their destination emails and active status
Example: aliasgen list
search <term> Search for aliases by a keyword
Example: aliasgen search amazon
delete <alias> Permanently delete an alias
Example: aliasgen delete amazon-abc12@domain.com
disable <alias> Disable an alias without deleting it (active=0)
Example: aliasgen disable amazon-abc12@domain.com
help Show this help menu
Example: aliasgen help
Notes:
- Created aliases are automatically copied to clipboard (Linux/macOS supported)
- Configuration is stored in 'mailcow.conf' (API key, domain, real email)
- Random suffix length can be adjusted with RANDOM_LENGTH in mailcow.conf
EOF
}
# Main command dispatch
case "$1" in
create) create_alias "$2" ;;
list) list_aliases ;;
search) search_aliases "$2" ;;
delete) delete_alias "$2" ;;
disable) disable_alias "$2" ;;
help|"" ) show_help ;;
*) echo "Unknown command: $1"; show_help ;;
esac

14
mailcow.conf Normal file
View File

@ -0,0 +1,14 @@
# Mailcow server URL
MAILCOW_URL="https://mail.yourdomain.com"
# API key from Mailcow
API_KEY="DD22D7-970D76-90FA4C-0DFBE7-E20ACF"
# List of domains (comma-separated)
DOMAINS=("yourdomain1.com" "yourdomain2.com")
# Your real email to forward aliases to
REAL_EMAIL="[email protected]"
# Length of random suffix for aliases
RANDOM_LENGTH=10