Old post, but if anyone needs some tool to import users, this is a script I made based on @SleepIT answer.
It imports from CSV file and supports multiple domains and custom user quota. All domains need to be added to Mailcow before running this script. Beware of domain quota, it should not be exceeded by the sum of all users quotas for their respective domain. The CSV format is [email,full name,user quota,password] no headers.
The script auto detects both parts [mailbox]@[domain.tld] from the email addresses
The script asks for: CSV location, Mailcow server address and API key
It doesn’t log errors or records of any kind. No input validation either. This is just a very simple, straightforward and primitive script that reads from a file and POST to the API.
I tested it with 82 records and did the job. Hope it can help you too.
To do: import alias functionality
#> /opt/mailcow-dockerized/importer/import.sh
#!/bin/sh
clear
echo "INSTRUCTIONS:"
echo " 1. Create a folder with 'mkdir -p /opt/mailcow-dockerized/importer/"
echo " 2. Copy your CSV file to that folder"
echo " 3. Make sure your CSV file is in the format: [email,full name,user quota,password] without headers"
echo " 4. Make sure all the emails domains in the CSV file are already added to mailcow"
echo " 5. Make sure global domain quota is not exceeded by the sum of all users quota for their corresponding domain"
echo " 6. Execute, input CSV location, Mailcow address and API Key. Profit."
echo " "
read -r -p "Do you want to continue? Choose \"No\" to Cancel and Exit [y/N]: " response
echo " "
case $response in
[yY][eE][sS]|[yY])
echo "Starting Mailcow importer..."
;;
*)
exit 1
;;
esac
echo " "
echo "Press enter to confirm the default value '[value]' where applicable or enter a custom value."
echo " "
#Asking for location of CSV file
while [ -z "$INPUT" ]; do
read -p "Input the location and name of your CSV file e.g. [/opt/mailcow-dockerized/importer/mailboxdb.csv]: " INPUT
: ${INPUT:='/opt/mailcow-dockerized/importer/mailboxdb.csv'}
done
echo " "
#Check if the CSV file exist
[ ! -f $INPUT ] && { echo "$INPUT doesn't exist"; exit 99; }
echo " "
#Asking for mail server hostname
while [ -z "$MAILSERVER" ]; do
read -p "Input the mail server address running mailcow e.g. [mail.domain.tld]: " MAILSERVER
: ${MAILSERVER:='mail.domain.tld'}
done
echo " "
read -p "Input or paste your API Key e.g. XXXXX-XXXXX-XXXXX-XXXXX-XXXXX: " APIK
echo " "
#Reading each line of CSV file, converting fields into usable variables for the API,
#and making the POST request to create each user using the API
while IFS=',' read EMAIL FULLNAME UQUOTA PWORD
do
MAILBOX=`echo "${EMAIL}" | awk -F@ '{print $1}'`
DOMAIN=`echo "${EMAIL}" | awk -F@ '{print $2}'`
echo " "
echo " =====================> Importing User: $FULLNAME"
curl -X POST "https://${MAILSERVER}/api/v1/add/mailbox" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-API-Key: ${APIK}" \
-d "{\"local_part\":\"${MAILBOX}\", \"domain\":\"${DOMAIN}\", \"name\":\"${FULLNAME}\", \"quota\":\"${UQUOTA}\", \"password\":\"${PWORD}\", \"password2\":\"${PWORD}\", \"active\":\"1\"}"
echo "<====================== Import Completed"
echo " "
done < ${INPUT}
echo " "
echo "done"
echo " "