Jeff Bisbee
and 1 contributors


Email::Filter::Rules - Simple Rules for Routing Mail with Email::Filter




  #!/usr/bin/perl -w
  use strict;
  use Email::Filter;
  use Email::Filter::Rules;

  my $maildir = '/home/jbisbee/mail/';
  my $msg = Email::Filter->new(emergency => $maildir . 'emergency');

  my $mail_lists = Email::Filter::Rules->new(
      rules => '/home/jbisbee/bin/mail_lists'

  if (my $mail_list_folder = $mail_lists->apply_rules($msg)) {
      $msg->accept($maildir . $mail_list_folder);

  $msg->accept($maildir . 'inbox');

Where the 'rules' can be a filename, array ref, or scalar and looks like this

  # DESTINATION FOLDER <space> Email::Filter->$METHOD(S) <space> SUBSTRING
  # Linux - FLUX
  lists/linux/flux/linux        to:cc
  lists/linux/flux/talk         to:cc
  lists/linux/flux/website      to:cc
  lists/linux/flux/announce     to
  # Linux - Fluxbox
  lists/linux/fluxbox/users     to:cc
  lists/linux/fluxbox/devel     to:cc
  lists/linux/fluxbox/announce  to:cc
  # Linux - Debian
  lists/linux/debian/news       to
  lists/linux/debian/announce   to
  lists/linux/debian/mirrors    to
  lists/linux/debian/bugs       to:cc
  # Linux - Mozilla
  lists/linux/mozilla-bugs      to    mozilla-bugs
  # Perl
  lists/perl/useperl            to:cc use-perl
  # Perl - PM
  lists/perl/pm_groups          to:cc
  lists/perl/pm/southflorida    to:cc
  # Word of the Day
  lists/word-of-the-day from


Email::Filter::Rules is a simple way to route e-mail into folders without having to touch your filter script. I used to edit my filter script often to add or remove e-mail lists and often would fat finger something and enter a typo. This would result in all my e-mail bouncing and all in all would be a real bummer.

I wanted to make it syntax simple so it wouldn't end up looking like to awful procmail recipe or some cryptic piece of junk.


Simply put, a rule consists of a destination folder, one to many Email::Filter method names, and a substring to test the result of the method call.

  DESTINATION FOLDER <space> Email::Filter->$METHOD(S) <space> SUBSTRING

where a rule looks like this

  lists/perl/pm/southflorida    to:cc

each rule is tested and quotemeta is used on the substring

  $msg->to =~ /southflorida-pm\@mail\.pm\.org/i
  $msg->cc =~ /southflorida-pm\@mail\.pm\.org/i

and the destination folder is returned for the first matching test


and now I have the mail folder, I can tell the Email::Filter object to accept to that folder.

So thats it, short, simple, and to the point. No more boucing e-mails by editing my filter directly. :)


Email::Filter::Rules->new( rules => $rules )

  • rules

    Can be a scalar, array reference, or a file name name.

  • debug

    Boolean to turn on warnings during apply_rules


$efr->apply_rules( $email )

Pass in an e-mail object and attempt to call methods you defined in your rules file on it.


Copyright 2005 Jeff Bisbee <>


This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.


Email::Filter, Email::Simple