Add email accounts from a CSV file on the command line.

As of DirectAdmin 1.39.2, a new script will exist which allows for the adding of email accounts from the command line:

/usr/local/directadmin/scripts/add_email.sh

This guide will describe how to read in a csv file formatted like this:

user1@domain.com,password1,500
fred@domain.com,password2,500
bob@domain.com,password3,500
joe@domain.com,password4,500
carl@domain.com,password5,500
gary@domain.com,password6,500

and have it automatically be added into the /etc/virtual/domain.com/passwd file.  Note, it's assumed the numbers on the right (500) are in Megabytes, and password are plaintext (can be changed to use crypted password).  It's important that password do not contain any commas, since CSV requires that commas be used as the separators.  Also the DA User and domain should already be setup.  Add a test email account first to ensure all paths are set correctly.

1) Ensure you've got the latest version of DA.  Test that you have the script and it's ready to accept input, by typing:

cd /usr/local/directadmin/scripts/
./add_email.sh

You may want to manually run the script once with a test account, to ensure the script is working, before adding things in mass.


2) Create a new script:

cd /root
nano csv.sh

and fill it with the following code:

#!/bin/sh
#sample line:
#user@domain.com,password,500

FILE=mailboxes.csv

#the quotas are stored in bytes. so the # in meg, x 1024 x 1024 gives us bytes.(1024x1024=1048576)
QUOTAMULTIPLIER=1048576

if [ ! -s $FILE ]; then
echo "Ensure ${FILE} exists";
exit 1;
fi

for line in `cat $FILE`; do
{
EMAIL=`echo "$line" | cut -d, -f1`
PASS=` echo "$line" | cut -d, -f2`
QUOTA=`echo "$line" | cut -d, -f3`

QUOTA=`perl -e "print $QUOTA * $QUOTAMULTIPLIER"`

USER=`echo "$EMAIL" | cut -d@ -f1`
DOMAIN=`echo "$EMAIL" | cut -d@ -f2`

/usr/local/directadmin/scripts/add_email.sh $USER $DOMAIN "$PASS" 1 $QUOTA
};
done;

exit 0;


3) Add your data into the file /root/mailboxes.csv, set the csv.sh script to 755, then run it:

chmod 755 csv.sh
./csv.sh



4) Confirm that the data has been added correctly by viewing the accounts in:
User Level -> Email Accounts

as well as testing the login to /squirrelmail.


Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 12091