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.



10:39 pm
Guess I’m destined never to have this work… Despite converting the quotes above to ASCII, I still get the following error when trying to run the script:
./createmaildir: line 41: unexpected EOF while looking for matching `”‘
./createmaildir: line 51: syntax error: unexpected end of file
6:55 am
Hahahaha…clear one error, another one eagerly awaits. Above “EOF” error was due to a ‘?’ character instead of ‘ ” ‘ after “active = 1″ portion. Now, w/ that out of the way, I get:
/usr/local/mysql/bin/mysql: unknown option ‘-k’
7:29 am
That’s strange, we don’t use an option -k in our command lines. I think there must be a typo in the query.
7:46 am
I thought so, too. I inserted an ‘echo’ line to track what was going on, and the error occurs as the script reads the $MYSQL line. It makes it to, but never makes it past “while read DOMAIN EMAIL”, as my debug echo comment placed after this does not show up (but placed just before _does_).
11:43 pm
Gettin’ closer to getting this to work, Richard. Sorry for all the bother. Do you have any thoughts on why this wouldn’t work? It does not process the line,
while read DOMAIN EMAIL
Instead yielding the error:
/usr/local/mysql/bin/mysql: unknown option â-kâ
How can I further troubleshoot this? Also, is this required every time I add a new user? Or is there some more automated way?
7:59 pm
Can you mail me your file as attachment ?
7:31 am
There was a problem in the mysql commandline it missed a -, the option skip-colum-names needed 2.