First, decide how often this mailing list is going to change. If you’re going to add a user every once in a while, your easiest bet is probably to run a MySQL query from the command line and output it into a .qmail file. You could use this in a cron job every minute/hour/day/month/etc to meet your goals of keeping your distribution list up to date:
mysql -sN -e ‘SELECT CONCAT(“&”,emailaddress) FROM table WHERE criteria’ > .qmailtmp
if [ $? -eq 0 ]; then
mv .qmailtmp .qmail
fi
Advantage of this setup is that you could create it in a temporary location and then copy it over the .qmail file after validating its success. Hence your mail doesn’t stop delivering when your MySQL server goes down. This is probably your fastest, easiest, and best way to implement this. It also ensures many requests to send to that e-mail address won’t generate enormous MySQL traffic and slow your delivery or overload your MySQL’s server number of connections.
If you need something that changes more regularly, consider that the recipient of the original e-mail will always be the to of the forwarded e-mails and not the one you’re forwarding to. You may find that reduces deliverability due to spam filters. To solve this, you may find a mailing list manager such as ezmlm to be a possible solution, which can store its subscribers list in a MySQL database.
As a third option using a mail filter such as Courier’s maildrop with preline ahead of it could work. http://www.courier-mta.org/maildrop/ That said, the MySQL patches appear to have been abandoned and I can only find sources in old Linux distribution source archives. You could still use Maildrop to run an external program such as MySQL to get your list, but if you’re going to do that, it’s basically the same as the first option but in real time. Since I can’t find anything in 10 years on maildrop-MySQL, here’s an example using an external program.
# set default Maildir
MAILDIR="$HOME/Maildir"
logfile "$HOME/mailfilter.log"
#user settings
DATABASE=<YOUR_DATABASE_USERNAME_HERE>
#mysql select
RESULT=`echo -ne "select toemail from addresses;" | mysql $DATABASE --skip-column-names`
### deliver to each RESULT here using your local methods