Bulk Updating cPanel Account Contact Emails via SSH

When managing multiple cPanel accounts, you may need to update the notification email address for all users at once — for example, when switching support email addresses or consolidating contact information.
Manually changing each account’s email from WHM can be time-consuming, but with a simple Bash script, you can automate the process.

Below is a step-by-step guide on how to do it.


1. Prerequisites

Before you begin, make sure you have:

  • Root SSH access to the server.

  • Basic knowledge of running commands in Linux.

  • A backup of your server or at least the /var/cpanel/users/ directory. (Not compulsory – Recommended)

 2. How to Use the Script:

  1. Log in to your server via SSH as root:

    ssh root@your-server-ip

     

  2. Create a new script file:

    nano bulk_update_cpanel_emails.sh

     

  3. Paste the script below into the file.

    Here’s the cleaned and safe-to-use version of the script:

    #!/bin/bash
    
    # Set the old and new email addresses
    OLD_EMAIL="old@example.com"
    NEW_EMAIL="new@example.com"
    
    echo "Starting bulk update: replacing '$OLD_EMAIL' with '$NEW_EMAIL'"
    
    # Loop through all usernames in the system
    cut -f2 -d' ' /etc/trueuserdomains | sort | while read -r username; do
        config="/var/cpanel/users/$username"
    
        # Skip if the config file doesn't exist
        [ ! -f "$config" ] && continue
    
        # Extract the current contact email
        current=$(grep -i "^CONTACTEMAIL=" "$config" | cut -d= -f2)
    
        # If the email matches the OLD_EMAIL, update it
        if [[ "$current" == "$OLD_EMAIL" ]]; then
            echo "Updating $username from $OLD_EMAIL$NEW_EMAIL"
            /usr/local/cpanel/bin/whmapi1 modifyacct user="$username" contactemail="$NEW_EMAIL"
    
            # Check if update was successful
            if [ $? -eq 0 ]; then
                echo "✅ Success: $username"
            else
                echo "❌ Error updating $username"
            fi
        fi
    done
    
    echo "Bulk update finished."
    
  4. Replace:

    • old@example.com with your current contact email.

    • new@example.com with your new contact email.

  5. Save and exit (CTRL + O, Enter, then CTRL + X).

   6. Make the script executable:

chmod +x bulk_update_cpanel_emails.sh

7. Run the script:

./bulk_update_cpanel_emails.sh

 


3. How It Works

  • The script checks every cPanel account’s configuration file in /var/cpanel/users/.

  • It looks for the CONTACTEMAIL entry.

  • If the entry matches the old email address, it uses WHM’s modifyacct API to update it.

  • It logs each update result to the terminal.


4 . Important Notes

  • This will only update accounts that have the old email exactly matching what you define in OLD_EMAIL.

  • If some accounts use different old emails, you’ll need to adapt the script.

  • Always test the script on a staging server or with a small subset of accounts before running it on production.


💡 Pro Tip:
If you want to change all cPanel accounts to the new email regardless of what the old email is, you can modify the script to skip the check for OLD_EMAIL.
This “universal update” method is faster when you need to replace every account’s contact email in one go.