HEX
Server: nginx/1.29.3
System: Linux 11979.bigscoots-wpo.com 6.8.0-88-generic #89-Ubuntu SMP PREEMPT_DYNAMIC Sat Oct 11 01:02:46 UTC 2025 x86_64
User: nginx (1068)
PHP: 7.4.33
Disabled: exec,system,passthru,shell_exec,proc_open,proc_close,popen,show_source,cmd# Do not modify this line # 1684243876
Upload Files
File: //bigscoots/wpo/mail/mailgun.sh
#!/bin/bash

# Function to generate a random string
generate_random_string() {
    cat /dev/urandom | tr -dc 'a-z0-9' | fold -w ${1:-32} | head -n 1
}

# Function to load API credentials and encode them
load_credentials() {
    if [[ -f /root/.bigscoots/mail/mailgun/api.credentials ]]; then
        source /root/.bigscoots/mail/mailgun/api.credentials
    fi

    if [[ -z "$API_USER" || -z "$API_PASS" ]]; then
        echo "Error: API credentials not provided and default credentials file not found."
        exit 1
    fi

    # Base64 encode credentials and export them
    ENCODED_CREDENTIALS=$(echo -n "$API_USER:$API_PASS" | base64 | tr -d '\n')
    export ENCODED_CREDENTIALS
}

# Function to list domains
list_domains() {
    load_credentials
    curl --silent --request GET 'https://api.mailgun.net/v3/domains' \
    --header "Authorization: Basic $ENCODED_CREDENTIALS"
}

# Function to create a domain
create_domain() {
    local domain=$1
    local sub=$2
    local smtp_password=$(generate_random_string)

    # Check if the --sub option is provided and set the sub variable accordingly

    if [ -z "$sub" ]
    then
        sub=$(generate_random_string)
    fi
    
    load_credentials
    
    # Check if the JSON file already exists
    if [ -f "/root/.bigscoots/mail/mailgun/domains/$domain.json" ]
    then
        # Rename the existing JSON file with an epoch timestamp suffix
        mv "/root/.bigscoots/mail/mailgun/domains/$domain.json" "/root/.bigscoots/mail/mailgun/domains/$domain.json.$(date +%s)"
    fi

    local response=$(curl -s --location 'https://api.mailgun.net/v3/domains' \
    --header "Authorization: Basic $ENCODED_CREDENTIALS" \
    --form 'name='"$sub.$domain"'' \
    --form 'smtp_password='"$smtp_password"'')


    # Add smtp_password to the JSON file
    echo "{\"smtp_password\": \"$smtp_password\"}" > /root/.bigscoots/mail/mailgun/domains/"$domain.json"
    echo "$response" >> /root/.bigscoots/mail/mailgun/domains/"$domain.json"
}

# Function to create a domain
remove_domain() {
    local domain=$1
    
    if [ -f "/root/.bigscoots/mail/mailgun/domains/$domain.json" ]
    then
        smtp_domain=$(cat "/root/.bigscoots/mail/mailgun/domains/$domain.json" | jq -r '.domain.name // empty')
    else
        smtp_domain="$domain"
    fi

    load_credentials
    local response=$(curl -s -X DELETE "https://api.mailgun.net/v3/domains/$smtp_domain" \
    --header "Authorization: Basic $ENCODED_CREDENTIALS")

    echo "$response"
}

# Function to create a domain
activate_dkim() {
    local domain=$1
    
    if [ -f "/root/.bigscoots/mail/mailgun/domains/$domain.json" ]
    then
        smtp_domain=$(cat "/root/.bigscoots/mail/mailgun/domains/$domain.json" | jq -r '.domain.name // empty')
        dkim_selector=$(cat "/root/.bigscoots/mail/mailgun/domains/$domain.json" | jq -r 'select(.sending_dns_records) | .sending_dns_records[] | select(.name | contains("_domainkey")) | .name | split(".")[0]')
    else
        smtp_domain="$domain"
    fi

    load_credentials
    local response=$(curl -s -X PUT "https://api.mailgun.net/v4/domains/$smtp_domain/keys/$dkim_selector/activate" \
    --header "Authorization: Basic $ENCODED_CREDENTIALS")

    echo "$response"
}

# Function to create a domain
verify_domain() {
    local domain=$1
    
    if [ -f "/root/.bigscoots/mail/mailgun/domains/$domain.json" ]
    then
        smtp_domain=$(cat "/root/.bigscoots/mail/mailgun/domains/$domain.json" | jq -r '.domain.name // empty')
    else
        smtp_domain="$domain"
    fi

    load_credentials
    local response=$(curl -s -X PUT "https://api.mailgun.net/v4/domains/$smtp_domain/verify" \
    --header "Authorization: Basic $ENCODED_CREDENTIALS")

    echo "$response"
}

# Function to configure SMTP for a domain
configure_smtp() {
    local domain=$1
    
    # Retrieve smtp_login from the JSON file
    local smtp_login=$(cat "/root/.bigscoots/mail/mailgun/domains/$domain.json" | jq -r '.domain.smtp_login // empty')
    local smtp_password=$(cat "/root/.bigscoots/mail/mailgun/domains/$domain.json" | jq -r '.smtp_password // empty')

    # Run the SMTP configuration script
    bash /bigscoots/wpo/mail/smtp.sh --domain "$domain" --username "$smtp_login" --password "$smtp_password" --server "smtp.mailgun.org"
}

function show_help {
  echo "Usage: $0 {command} [options]"
  echo ""
  echo "Commands:"
  echo "  list                            List domains"
  echo "  create --domain <domain> [--configure] [--sub <subdomain>] Create a new domain"
  echo "  remove --domain <domain>         Remove a domain"
  echo "  activate_dkim --domain <domain>  Activate DKIM for a domain"
  echo "  verify --domain <domain>         Verify a domain"
  echo ""
  echo "Options:"
  echo "  --domain <domain>               Specify the domain name (required for create, remove, activate_dkim, and verify)"
  echo "  --sub <subdomain>               Specify a subdomain (optional)"
  echo "  --configure                     Configure SMTP on the WordPress site (optional)"
  echo ""
  echo "Examples:"
  echo "  List domains: $0 list"
  echo "  Create a domain without subdomain: $0 create --domain example.com"
  echo "  Create a domain with subdomain and configure: $0 create --domain example.com --sub website --configure"
  echo "  Remove a domain: $0 remove --domain example.com"
  echo "  Verify a domain(Shows DNS records and status): $0 verify --domain example.com"
  echo "  Activate DKIM(Run this after adding all DNS Records) for a domain: $0 activate_dkim --domain example.com"
  echo ""
}


mkdir -p /root/.bigscoots/mail/mailgun/domains


# Initialize the command variable
command="$1"
shift

# Parse command line arguments
while [[ $# -gt 0 ]]; do
  case "$1" in
    --domain)
      domain="$2"
      shift 2
      ;;
    --sub)
      sub="$2"
      shift 2
      ;;
    --configure)
      configure=true
      ;;
    *)
      echo "Invalid argument: $1"
      show_help
      exit 1
      ;;
  esac
done

# Now you can use the command variable in the case statement
case "$command" in
  list)
    list_domains
    exit 0
    ;;
  create)
    if [[ -z "$domain" ]]
    then
        echo "Usage: $0 create --domain <domain> [--configure] [--sub]"
        exit 1
    fi
    if [ -n "$sub" ]
    then
      create_domain "$domain" "$sub"
    else
      create_domain "$domain"
    fi
    if [ "$configure" == true ]
    then
      configure_smtp "$domain"
    fi
    exit 0
    ;;
  activate_dkim)
    if [[ -z "$domain" ]]
    then
        echo "Usage: $0 activate --domain <domain>"
        exit 1
    fi
    activate_dkim "$domain"
    exit 0
    ;;
  verify)
    if [[ -z "$domain" ]]
    then
        echo "Usage: $0 verify --domain <domain>"
        exit 1
    fi
    verify_domain "$domain"
    exit 0
    ;;
  remove)
    if [[ -z "$domain" ]]
    then
        echo "Usage: $0 remove --domain <domain>"
        exit 1
    fi
    remove_domain "$domain"
    exit 0
    ;;
  *)
    echo "Invalid command: $command"
    show_help
    exit 1
    ;;
esac