Loading ...
Sorry, an error occurred while loading the content.

275976Re: example showing how to track bad/bounce emails

Expand Messages
  • Andrew Beverley
    Apr 3, 2011
    • 0 Attachment
      On Thu, 2011-03-31 at 12:56 -0400, Wietse Venema wrote:
      > marshall:
      > > Hello;
      > >
      > > I've been searching around for a while, but I've not found any documentation or
      > > examples that show how you can configure Postfix to log bad/bounce/failed emails
      > > to MySQL or how to read a log file and parse the bad/bounce/failed emails out of
      > > it.

      I wouldn't bother trying to parse the mail log, as Wietse has already
      implied.

      > >
      > > The application I'm working on needs to detect emails that bounce
      > > and ideally remove them from the database. If Postfix can insert
      > > the bounced emails into a db table (or a log file that just contains
      > > the bad email addres, one per line), that would make it pretty
      > > easy to run a cron job to remove these bad emails from the
      > > application's user database.
      >
      > Postfix delivers mail; it does not parse bounce messages.
      >
      > You could use the documented Postfix hooks to deliver the returned
      > mail to a command that parses out the failed recipient.
      >
      > - "|command" in local alias or .forward file:
      > http://www.postfix.org/local.8.html.
      > http://www.postfix.org/aliases.5.html.

      I do this, along with...

      > Bounce message parsing is not trivial because a lot of systems use
      > a non-standard notification format. Using VERP solves some but
      > not all of these problems (some mail systems ignore the envelope
      > sender address). http://www.postfix.org/VERP_README.html

      ...this, and It Works For Me (TM).

      You'll need to make sure that the sender address is something that can
      be recognised as a bounced email (I use owner-list@... for some
      mailing lists, which are then VERP encoded by Postfix).

      You'll then need an alias regular expression to pull out the email
      address and send to your script.

      I personally use a mysql aliases table, with the PREG_CAPTURE feature
      from the lib_mysqludf_preg library to pass the bounces to my bounce
      script. However, in your case, you'll probably just need to set up a
      regular expression aliases file to look for bounced email addresses.
      From the above example, something like:

      /^owner-(.*)$/ "|/path/to/script"

      Or, if you're using virtual domains, then I don't think you can pipe
      straight to a script, so you'll have to send to an alias (which should
      then forward to the script):

      /^owner-(.*)@domain$/ "alias"

      You'll then need to do something like this in a Perl script. Depending
      how your script is being called depends whether to use "x-original-to"
      or just "to", but I don't see any harm looking for both.

      while (<STDIN>) {
      if (/^x-original-to: <?\"?owner-([^+]*)\+(.*)\@([^>]*)/i) {
      $list = $1;
      $recipient = $2;
      $domain = $3;
      chomp($domain);
      } elsif (/^to: <?\"?owner-([^+]*)\+(.*)\@([^>]*)/i) {
      $list = $1;
      $recipient = $2;
      $domain = $3;
      chomp($domain);
      }
      }

      In this case, the bouncing email address is stored in $recipient, and
      the original owner-listname@domain email address is stored in $list and
      $domain (you may not need this bit).

      None of the above is tested, but hopefully it should give you something
      to work from.

      Andy
    • Show all 5 messages in this topic