Site icon https://inertz.org

How to Automatically Create Email Accounts in cPanel After Account Creation (WHM Hook Guide)

Managing hosting accounts manually can be time-consuming—especially when you need to create email accounts for every new user. If you’re using cPanel with WHM, there’s a powerful way to automate this process using the postwwwacct hook.

In this guide, you’ll learn how to automatically create email accounts (like info@domain.com) immediately after a hosting account is created, even when provisioning via WHMCS.


⚙️ What Is postwwwacct?

postwwwacct is a script hook that runs right after a new cPanel account is created.

This allows you to:


🎯 What We’re Building

We will:


📜 The Script

Here is the working production-ready script:

#!/bin/bash
LOGFILE="/root/email_hook.log"
echo "===== $(date) =====" >> $LOGFILE
echo "ARGS: $@" >> $LOGFILE
# Parse arguments (key value pairs)
while [[ $# -gt 0 ]]; do
  key="$1"
  value="$2"
  case $key in
    user)
      USER="$value"
      shift 2
      ;;
    domain)
      DOMAIN="$value"
      shift 2
      ;;
    *)
      shift
      ;;
  esac
done

echo "Parsed USER=$USER DOMAIN=$DOMAIN" >> $LOGFILE

# Validate
if [[ -z "$USER" || -z "$DOMAIN" ]]; then
  echo "Invalid parameters, skipping..." >> $LOGFILE
  exit 1
fi

# Wait for mail system ready
sleep 10

# Create email
/usr/bin/uapi --user="$USER" Email add_pop \
  email=info \
  password='StrongP@ssword123!' \
  quota=1024 >> $LOGFILE 2>&1

echo "Done for $USER ($DOMAIN)" >> $LOGFILE

📂 Installation

  1. Save the script to:
/scripts/postwwwacct
  1. Make it executable:
chmod +x /scripts/postwwwacct

🔍 How It Works

When a new account is created:

  1. WHM triggers the script
  2. Script reads system arguments
  3. Extracts:
    • Username
    • Domain
  4. Waits for mail service readiness
  5. Creates email using uapi
  6. Logs everything

📊 Log File

All activity is logged here:

/root/email_hook.log

Example output:

Parsed USER=john DOMAIN=example.com
status: 1
Done for john (example.com)

⚠️ Common Pitfalls (Important!)

❌ Wrong Argument Parsing

cPanel does NOT pass arguments like:

user=username
Instead it uses:
user username

👉 This script correctly handles that format.


❌ Email Not Created via WHMCS

If you’re using WHMCS, this script still works—but only if:


❌ Timing Issues

Sometimes mail services aren’t ready instantly.

Fix:

sleep 10

🔐 Security Improvement (Recommended)

Avoid hardcoded passwords.Instead, generate dynamically:

PASSWORD=$(openssl rand -base64 12)

🧩 Advanced Customization

Create Multiple Emails

for EMAIL in info admin support; do
/usr/bin/uapi –user=$USER Email add_pop \
email=$EMAIL \
password=‘StrongP@ssword123!’ \
quota=1024
done

Prevent Duplicate Emails

/usr/bin/uapi –user=$USER Email list_pops | grep “info@$DOMAIN && exit 0

🧠 Why This Matters

Automation like this:


✅ Final Result

After setup, every new hosting account will automatically have:

info@domain.com

Ready to use—without manual setup.


🏁 Conclusion

Using postwwwacct with cPanel is a simple yet powerful way to automate email provisioning.

Whether you’re running a hosting business or managing multiple servers, this method ensures consistency and efficiency.

Exit mobile version