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/smtp.sh
#!/bin/bash

source /bigscoots/includes/common.sh

# Function to print script usage
print_usage() {
    echo "Usage: $0 [ACTION] [OPTIONS]"
    echo "Actions:"
    echo "  configure     Configure SMTP authentication"
    echo "  status        Check if SMTP authentication is active"
    echo "  remove        Remove SMTP authentication"
    echo "Options:"
    echo "  --domain      [REQUIRED]   Domain of the WordPress site"
    echo "  --username    [REQUIRED]   SMTP username (email address)"
    echo "  --password    [REQUIRED]   SMTP password"
    echo "  --server      [OPTIONAL]   SMTP server (defaults to mail.scoots.email if not set)"
    echo "  --port        [OPTIONAL]   SMTP port (defaults to 587 if not set)"
    echo "  --secure      [OPTIONAL]   SMTP security (defaults to tls if not set)"
    echo "  --auth        [OPTIONAL]   SMTP authentication (defaults to true if not set)"
    echo "  --debug       [OPTIONAL]   Enable SMTP debugging (defaults to false if not set)"
    echo ""
    echo "Example usage: $0 configure --domain example.com --username [email protected] --password mypassword"
    echo "               $0 status --domain example.com"
    echo "               $0 remove --domain example.com"
}

# Function to generate JSON response
generate_json_response() {
    local success="$1"
    local message="$2"
    local result="$3"
    local domain="$4"
    local serverip="$5"

    jq -n --argjson success "$success" --arg message "$message" --arg result "$result" --arg domain "$domain" --arg serverip "$serverip" \
        '{
            success: $success,
            messages: [$message],
            domain: $domain,
            ip: $serverip,
            result: $result
        }'
}

# Function to execute wp-cli command and capture error
execute_wp_cli() {
    local option="$1"
    local value="$2"

    wpcli --path="$WP_PATH" config set "$option" "$value" --quiet
    if [[ $? -ne 0 ]]; then
        generate_json_response false "Failed to set $option" null "$DOMAIN" "$serverip"
        exit 1
    fi
}

# Parse command-line arguments
ACTION="$1"
shift

while [[ $# -gt 0 ]]; do
    case "$1" in
        --domain)
            DOMAIN="$2"
            shift 2
            ;;
        --username)
            SMTP_USERNAME="$2"
            shift 2
            ;;
        --password)
            SMTP_PASSWORD="$2"
            shift 2
            ;;
        --server)
            SMTP_SERVER="$2"
            shift 2
            ;;
        --port)
            SMTP_PORT="$2"
            shift 2
            ;;
        --secure)
            SMTP_SECURE="$2"
            shift 2
            ;;
        --auth)
            SMTP_AUTH="$2"
            shift 2
            ;;
        --debug)
            SMTP_DEBUG="$2"
            shift 2
            ;;
        *)
            echo "Unknown option: $1"
            print_usage
            exit 1
            ;;
    esac
done

# Set path for WP-CLI
WP_PATH="/home/nginx/domains/$DOMAIN/public"

case "$ACTION" in
    configure)
        # Verify mandatory options are provided
        if [[ -z $DOMAIN || -z $SMTP_USERNAME || -z $SMTP_PASSWORD ]]; then
            generate_json_response false "Missing mandatory options" null "$DOMAIN" "$serverip"
            exit 1
        fi

        # Check if WP_PATH exists
        if [[ ! -d $WP_PATH ]]; then
            generate_json_response false "WordPress path does not exist: $WP_PATH" null "$DOMAIN" "$serverip"
            exit 1
        fi

        domain="$DOMAIN"

        if grep -qi "SMTP_username" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_username --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_password" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_password --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_server" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_server --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_PORT" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_PORT --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_SECURE" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_SECURE --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_AUTH" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_AUTH --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_DEBUG" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_DEBUG --quiet 2>/dev/null
        fi


        [ -f "$WP_PATH"/wp-content/mu-plugins/smtpauth-mu.php ] && rm -f "$WP_PATH"/wp-content/mu-plugins/smtpauth-mu.php

        wpcli plugin install https://wp-plugins.bigscoots.com/download/bigscoots-helper --force --activate --quiet --path="$WP_PATH" 2>/dev/null

        # Run the command to list modules and capture the output
        MODULES_STATUS=$(wp bs_helper list_modules --skip-plugins="$(skip_all_plugins_except bigscoots-helper)" --skip-themes --allow-root --path="$WP_PATH" --format=json 2>/dev/null)

        # Check if smtp-email-setup module is anything other than "Enabled"
        SMTP_STATUS=$(echo "$MODULES_STATUS" | jq -r '.[] | select(.module_id=="smtp-email-setup") | .status')

        if [[ "$SMTP_STATUS" != "active" ]]; then
            # Do something if smtp-email-setup is not enabled (either Disabled or missing)
            wp bs_helper enable_module smtp-email-setup --skip-plugins="$(skip_all_plugins_except bigscoots-helper)" --skip-themes --allow-root --quiet --path="$WP_PATH"
        fi

        wp bs_helper set_smtp_auth_details --username="$SMTP_USERNAME" --password="$SMTP_PASSWORD" --mode=custom --skip-plugins="$(skip_all_plugins_except bigscoots-helper)" --skip-themes --allow-root --quiet --path="$WP_PATH" 2>/dev/null
        correct_permissions_ownership

        generate_json_response true "SMTP authentication has been set up for the WordPress site: $DOMAIN" null "$DOMAIN" "$serverip"
        ;;
    status)
        if [[ -z $DOMAIN ]]; then
            generate_json_response false "Missing mandatory option: --domain" null "$DOMAIN" "$serverip"
            exit 1
        fi

        domain="$DOMAIN"

        # Check if SMTP is active
        SMTP_USERNAME=$(wpcli config get SMTP_username --path="$WP_PATH" --quiet 2>/dev/null)
        SMTP_AUTH_DETAILS=$(wp bs_helper get_smtp_auth_details --skip-plugins="$(skip_all_plugins_except bigscoots-helper)" --skip-themes --allow-root --path="$WP_PATH" 2>/dev/null)

        if [[ $? -ne 0 || -z $SMTP_USERNAME && -z $SMTP_AUTH_DETAILS ]]; then
            generate_json_response true "SMTP is not active for the domain: $DOMAIN" null "$DOMAIN" "$serverip"
            exit 0
        fi

        if [[ -z $SMTP_USERNAME ]]; then
            SMTP_USERNAME=$(wp bs_helper get_smtp_auth_details --skip-plugins="$(skip_all_plugins_except bigscoots-helper)" --skip-themes --allow-root --path="$WP_PATH" --format=json | jq -r '.[] | .username')
        fi

        # Check for presence of mu-plugin and SMTP details
        if [[ -f "$WP_PATH/wp-content/mu-plugins/smtpauth-mu.php" || -n "$SMTP_AUTH_DETAILS" ]]; then
            generate_json_response true "SMTP is active for the domain: $DOMAIN" "$SMTP_USERNAME" "$DOMAIN" "$serverip"
        else
            generate_json_response true "SMTP is not active for the domain: $DOMAIN" null "$DOMAIN" "$serverip"
        fi
        ;;
    remove)
        if [[ -z $DOMAIN ]]; then
            generate_json_response false "Missing mandatory option: --domain" null "$DOMAIN" "$serverip"
            exit 1
        fi

        domain="$DOMAIN"

        if grep -qi "SMTP_username" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_username --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_password" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_password --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_server" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_server --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_PORT" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_PORT --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_SECURE" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_SECURE --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_AUTH" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_AUTH --quiet 2>/dev/null
        fi
        if grep -qi "SMTP_DEBUG" "$WP_PATH/wp-config.php" ; then
            wpcli --path="$WP_PATH" config delete SMTP_DEBUG --quiet 2>/dev/null
        fi

        [ -f "$WP_PATH"/wp-content/mu-plugins/smtpauth-mu.php ] && rm -f "$WP_PATH"/wp-content/mu-plugins/smtpauth-mu.php

        wp bs_helper disable_module smtp-email-setup --skip-plugins="$(skip_all_plugins_except bigscoots-helper)" --skip-themes --allow-root --quiet --path="$WP_PATH" > /dev/null 2>&1
        
        generate_json_response true "SMTP authentication has been removed for the WordPress site: $DOMAIN" null "$DOMAIN" "$serverip"
        ;;
    *)
        echo "Unknown action: $ACTION"
        print_usage
        exit 1
        ;;
esac