0

I am trying to forward an incoming mail to multiple recipients. The recipient's addresses are stored inside a mysql database, so I need to call a script to get the addresses before forwarding the mail.

I already tried piping the mail to a PHP script, then parse the email and forward it via PHP Mail, but this has its disadvantages as you need to rely on an email parser and handle headers, encoding and attachments by yourself.

Is there any easier solution e.g. using a bash script to fetch the recipients and forward the mail?

JoJota
  • 3
  • 1

1 Answers1

0

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
michaelkrieger
  • 200
  • 1
  • 4
  • Thank you for your detailed answer. I think its best to rewrite my .qmail file every time when the recipients change. – JoJota Dec 13 '18 at 10:26
  • Great! Glad I could help. Please vote it as your solution and let me know if you need help implimenting it more than the above. – michaelkrieger Dec 14 '18 at 13:09