#!/bin/bash # Script version # Version: 5.2 # Arrays to store successfully migrated emails and emails with errors success_emails=() error_emails=() # Function to add email on cPanel and synchronize using imapsync add_email() { local email=$1 local password=$2 local user=${email%%@*} # Extract user part before @ local domain=${email##*@} local default_imap="mail.$domain" # Add email on cPanel echo "Adding email: $email" uapi --output=jsonpretty Email add_pop email="$user" password="$password" domain="$domain" # Check if credentials are valid echo "Checking credentials for email: $email" if imapsync --justlogin --host1 "$server_imap_sync" --user1 "$email" --password1 "$password" --host2 localhost --user2 "$email" --password2 "$password"; then # Start email synchronization echo "Synchronizing email: $email" if imapsync --host1 "$server_imap_sync" --user1 "$email" --password1 "$password" --host2 localhost --user2 "$email" --password2 "$password"; then success_emails+=("$email") else echo "Failed to sync email: $email" error_emails+=("$email") fi else echo "Failed to login to $email. Please update the password without special characters." error_emails+=("$email") fi } # Function to show the help manual show_help() { cat << EOF Usage: $(basename "$0") [file.csv | email1] Description: This script synchronizes IMAP email boxes on cPanel. Arguments: file.csv CSV file containing email and password separated by ';'. email1 Email address for which password will be asked interactively. Example: $(basename "$0") emails.csv $(basename "$0") user@example.com EOF } # Function to print email details in tabular form print_email_details() { echo "Email Details:" echo "--------------" printf "%-30s %-30s\n" "Email" "Password" printf "%-30s %-30s\n" "-----" "--------" while IFS=';' read -r email password; do printf "%-30s %-30s\n" "$email" "$password" done < "$csv_file" } # Function to print summary of operations in tabular form print_summary() { echo "Summary:" echo "--------" echo "Successfully Migrated Emails:" echo "-----------------------------" printf "%-30s\n" "${success_emails[@]}" echo echo "Emails with Errors:" echo "-------------------" printf "%-30s\n" "${error_emails[@]}" } # If no arguments or --help is passed, show the help manual if [ $# -eq 0 ] || [ "$1" = "--help" ]; then show_help exit 0 fi # Check if the argument is a file or an email if [ -f "$1" ]; then csv_file="$1" # Get the domain name from the first email in the CSV file first_email=$(head -n 1 "$csv_file" | cut -d ";" -f 1) domain=$(echo "$first_email" | awk -F '@' '{print $2}') default_imap="mail.$domain" # Ask for the source IMAP server read -p "Enter the source IMAP server (default: $default_imap): " server_imap_sync server_imap_sync=${server_imap_sync:-$default_imap} # Print email details from CSV file print_email_details # Confirm before proceeding with email creation and synchronization read -p "Do you want to proceed with email creation and synchronization? (y/n): " confirm if [ "$confirm" != "y" ]; then echo "Aborted." exit 0 fi # Read emails from CSV file and synchronize them echo "Parsing CSV file: $csv_file" while IFS=';' read -r email password; do add_email "$email" "$password" done < "$csv_file" else email="$1" domain=$(echo "$email" | awk -F '@' '{print $2}') default_imap="mail.$domain" read -p "Enter the source IMAP server (default: $default_imap): " server_imap_sync server_imap_sync=${server_imap_sync:-$default_imap} read -p "Enter the password for $email: " -s password echo add_email "$email" "$password" fi # Print summary of operations print_summary