To automatically create the maildirectories in ‘/usr/local/virtual/‘ when you are using maildrop as a delivery mechanism is difficult. Maildrop does not support it and I haven’t found a way to accomplish this in the mailfilter script. Therefore I’ve created a simple shell script that can be run periodically using cron or the command line depending on your needs. If you add users with postfixadmin regurarly than I would suggest running the script automatically. If you don’t than just run it from the commandline.
Here is the script I’ve written, it’s pretty simple. It first gets a list of all configured active users and it will test them one by one if the user directory exists. If it isn’t found it will copy an empty maildir with all the default folders like Junk and Trash into a new user folder.
Create a new textfile called createmaildir and copy the following content into it:
Note: Replace the user and password to the one you have configured. Make sure that the backticks (reverse quotes) are copied correctly and don’t get translated into normal quotes.
#
# Simple shell script create maildirectories
# – info@richard5.net
#
#============================================
USERFILE=/tmp/newusers
HOME=/usr/local/virtual
MYSQL=/usr/local/mysql/bin/mysql
MAILDIR=/usr/local/virtual/emptymaildir
DBUSER=postfix
DBPASS=postfixpassword
DB=postfix
# this query grabs all the active users
$MYSQL -u $DBUSER -p$DBPASS -e “select distinct domain, username from
mailbox where active = 1″ –skip-column-names $DB > $USERFILE
while read DOMAIN EMAIL
do
# first check domain
if [ -e $HOME/$DOMAIN ]
then
echo “Domain directory $HOME/$DOMAIN already exists.”
else
echo “Domain directory $HOME/$DOMAIN will be created”
`/bin/mkdir $HOME/$DOMAIN`
`/usr/sbin/chown _vmail:_postfix $HOME/$DOMAIN`
`/bin/chmod 771 $HOME/$DOMAIN`
fi
# get the username from the email address
USER=`echo $EMAIL|cut -d “@” -f1`
# now check users
if [ -e $HOME/$DOMAIN/$USER ]
then
echo “Domain directory $HOME/$DOMAIN/$USER already exists.”
else
echo “Domain directory $HOME/$DOMAIN/$USER will be created”
`/bin/cp -R $MAILDIR $HOME/$DOMAIN/$USER`
`/usr/sbin/chown -R _vmail:_postfix $HOME/$DOMAIN/$USER`
`/bin/chmod -R 771 $HOME/$DOMAIN/USER`
fi
done < $USERFILE
# clean up action..
rm -f $USERFILE
And then set the X bit on the file:
To make sure it will run you will have to download my maildir template file and extract it into /usr/local/virtual. You can do this using the following commands:
cd /usr/local/virtual
tar -xvf /whereyoudownloaded/emptymaildir.tar
exit
To start using it execute the script using the command:
or add it to cron or write a launchd startup script that will run it periodically.


