Back to Articles

Zimbra Bulk Account Creation.

2 min read
asrar
Zimbra Bulk Account Creation.

Here’s the script that help me to create multiple account form csv file with format
“email,password,firstname,lastname,middlename”

    #!/bin/bash
    
    # Function to add a user to Zimbra using the provided parameters
    function add_user() {
        local email=$1
        local password=$2
        local firstname=$3
        local lastname=$4
        local middlename=$5
    
        # Construct the Zimbra command using the provided parameters
        zimbractl adduser -a $email -s $password -f "$firstname $lastname $middlename"
    
        # Check if the command executed successfully and print a message accordingly
        if [[ $? -eq 0 ]]; then
            echo "User $email added successfully."
        else
            echo "Failed to add user $email. Please check the error message."
        fi
    }
    
    # Check if a CSV file is provided as an argument
    if [[ $# -ne 1 ]]; then
        echo "Usage: $0 "
        exit 1
    fi
    
    # Read the CSV file line by line
    while IFS="," read -r email password firstname lastname middlename; do
        # Skip the header row
        if [[ "$email" == "email" ]]; then
            continue
        fi
    
        # Call the add_user function with the read values
        add_user "$email" "$password" "$firstname" "$lastname" "$middlename"
    done < "$1"

Explanation:

  1. Function Definition:

    The add_user function takes the email, password, first name, last name, and middle name as parameters.
    It constructs the Zimbra command using the provided values and executes it.
    The function checks the exit status of the command and prints an appropriate message.

  2. **Argument Validation:**The script checks if a CSV file is provided as an argument and exits if not.

  3. CSV File Reading:

    The script reads the CSV file line by line using the while IFS="," read -r email password firstname lastname middlename; do loop.
    The IFS variable is set to a comma to separate the fields in the CSV.
    The -r option prevents backslashes from being interpreted as escape characters.

  4. Header Row Skipping:

    The script checks if the current line is the header row and skips it if so.

  5. User Addition:

    The add_user function is called with the values read from the CSV file to add the user.

To use this script:

  1. Save it as a Bash file (e.g., add_users.sh).
  2. Make it executable: chmod +x add_users.sh
  3. Run the script under zimbra account
  4. Run it with the CSV file as an argument: ./add_users.sh your_csv_file.csv

Thanks

Share this article

Discussion

Join the discussion

Loading comments...