in reply to postfix filter...

If I isolate the sub  find_exp_to into a separate script, it breaks on Postfix's Delivered-To: and X-Original-To: headers when an email is piped through by hand. Also your regexes search through the entire email, so if the body contains "To: blah@blah" that probably gets returned..

To parse email, it will be useful to get explicit about the header and the body - probably best to use a module that does that for you like Mail::Internet, or look in the Perl Cookbook where you'll find the use of the  .. range operator to determine whether you're in headers or body (P179 of the first edition)

I would work on parsing the email first, without the mysql stuff. Have your filter write output of the parsed email to a disk file you can verify as a debugging step, so that you can make sure the piping through Postfix is working. You should see the filter return properly through Postfix's very imformative logs, but before installing the filter your test case should work from the command line, as you are already doing..

Most importantly,
use strict; use warnings;