man Mail::Audit () - Library for creating easy mail filters

NAME

Mail::Audit - Library for creating easy mail filters

SYNOPSIS

 use Mail::Audit; # use Mail::Audit qw(...plugins...);
 my $mail = Mail::Audit->new(emergency=>"~/emergency_mbox");
 $mail->pipe("listgate p5p")            if $mail->from =~ /perl5-porters/;
 $mail->accept("perl")                  if $mail->from =~ /perl/;
 $mail->reject("We do not accept spam") if $mail->rblcheck();
 $mail->ignore                          if $mail->subject =~ /boring/i;
 ...
 $mail->noexit(1); $mail->accept("~/Mail/Archive/%Y%m%d"); $mail->noexit(0);
 $mail->accept()

DESCRIPTION

procmail is nasty. It has a tortuous and complicated recipe format, and I don't like it. I wanted something flexible whereby I could filter my mail using Perl tests.

CWMail::Audit was inspired by Tom Christiansen's audit_mail and deliverlib programs. It allows a piece of email to be logged, examined, accepted into a mailbox, filtered, resent elsewhere, rejected, replied to, and so on. It's designed to allow you to easily create filter programs to stick in a .forward file or similar.

CWMail::Audit groks MIME; when appropriate, it subclasses CWMIME::Entity. Read the MIME::Tools man page for details.

CONSTRUCTOR

The constructor reads a mail message from CWSTDIN (or, if the CWdata option is set, from an array reference) and creates a CWMail::Audit object from it. Other options include the CWaccept, CWreject or CWpipe keys, which specify subroutine references to override the methods with those names. You are encouraged to specify an CWemergency argument and check for the appearance of messages in that mailbox on a regular basis. If for any reason an CWaccept() is unsuccessful, the message will be saved to the CWemergency mailbox instead. If no CWemergency mailbox is defined, messages will be deferred back to the MTA, where they will show up in your mailq. You may also specify CWlog => $logfile to write a debugging log; you can set the verbosity of the log with the CWloglevel key, on a scale of 1 to 4. If you specify a log level without a log file, logging will be written to .mail_audit.log in your home directory if the HOME environment variable is set, or to /tmp/mail_audit.log-XXXXX (where XXXXX contains random characters) if not. Usually, the delivery methods CWaccept, CWpipe, and CWresend are final; Mail::Audit will terminate when they are done. If you specify CWnoexit => 1, CWMail::Audit will not exit after completing the above actions, but continue running your script. The CWreject delivery method is always final; CWnoexit has no effect. Percent (%) signs seen in arguments to CWaccept and CWpipe do not undergo CWstrftime interpolation by default. If you want this, use the CWinterpolate_strftime option. You can override the global interpolate_strftime option by passing an overriding option to CWaccept and CWpipe. By default, MIME messages are automatically recognized and parsed. This is potentially expensive; if you don't want MIME parsing, use the CWnomime option.

DELIVERY METHODS

You can choose to accept the mail into a mailbox by calling the CWaccept method; with no argument, this accepts to /var/spool/mail/you. The mailbox is opened append-write, then locked LOCK_EX, the mail written and then the mailbox unlocked and closed. If Mail::Audit sees that you have a maildir style system, where /var/spool/mail/you is a directory, it'll deliver in maildir style. If the path you specify does not exist, Mail::Audit will assume mbox, unless it ends in /, which means maildir. If multiple maildirs are given, Mail::Audit will use hardlinks to deliver to them, so that multiple hardlinks point to the same underlying file. (If the maildirs turn out to be on multiple filesystems, you get multiple files.) If you want % signs to be expanded according to strftime(3), you can pass CWaccept the option CWinterpolate_strftime:

 accept( {interpolate_strftime=>1}, file1, file2, ... );
interpolate_strftime is not enabled by default for two reasons: backward compatibility (though nobody I know has a % in any mail folder name) and username interpolation: many people like to save messages by their correspondent's username, and that username may contain a % sign. If you are one of these people, you should
 $username =~ s/%/%%/g;
If your arguments contain /, CWaccept will create arbitarily deep subdirectories accordingly. Untaint your input by saying
 $username =~ s,/,-,g;
By default, CWaccept is final; Mail::Audit will terminate after successfully accepting the message. If you want to keep going, set CWnoexit. If for any reason CWaccept is unable to write the message (eg. you're over quota), Mail::Audit will attempt delivery to the CWemergency mailbox. If CWaccept was called with multiple destinations, the CWemergency action will only be taken if the message couldn't be delivered to any of the desired destinations. By default the CWemergency mailbox is set to the system mailbox. If we were unable to save to the emergency mailbox, the message will be deferred back into the MTA's queue. This happens whether or not CWnoexit is set, so if you observe that some of your CWaccepts somehow aren't getting run, check your mailq. If this isn't how you want local delivery to happen, you'll need to override this method. This rejects the email; it will be bounced back to the sender as undeliverable. If a reason is given, this will be included in the bounce. This is a final delivery method. The CWnoexit option has no effect here. Bounces the email in its entirety to another address. This is a final delivery method. Set CWnoexit if you want to keep going. At this time this method is not overrideable by an argument to CWnew. This opens a pipe to an external program and feeds the mail to it. This is a final delivery method. Set CWnoexit if you want to keep going. This merely ignores the email, dropping it into the bit bucket for eternity. This is a final delivery method. Set CWnoexit if you want to keep going. Sends an autoreply to the sender of the message. Return value: the recipient address of the reply. Recognized content-related options are: from, subject, cc, bcc, body. The To field defaults to the incoming message's Reply-To and From fields. CWbody should be a single multiline string. Set the option CWEVEN_IF_FROM_DAEMON to send a reply even if the original message was from some sort of automated agent. What that set, only X-Loop will stop loops. If you use this method, use KillDups to keep track of who you've autoreplied to, so you don't autoreply more than once.
 use Mail::Audit qw(KillDups);
 $mail->reply(body=>"I am on vacation") if not $self->killdups($mail->from);
CWreply is not considered a final delivery method, so execution will continue after completion.

HEADER MANAGEMENT METHODS

Retrieves the named header from the mail message. Inserts a new header into the mail message with the given value. Removes the old header, adds a new one. Guess.

MISCELLANEOUS METHODS

Tidies up the email as per Mail::Internet. If the message is a MIME message, nothing happens. Toggle noexit.

ATTRIBUTE METHODS

The following attributes correspond to fields in the mail:

•
from
•
to
•
subject
•
cc
•
bcc Returns a reference to an array of lines in the body of the email. Returns the header as a single string. am I a MIME message? If so, MIME::Entity methods apply. Otherwise, Mail::Internet methods apply.

LICENSE

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

BUGS

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mail-Audit

CAVEATS

If your mailbox file in /var/spool/mail/ doesn't already exist, you may need to use your standard system MDA to create it. After it's been created, Mail::Audit should be able to append to it. Mail::Audit may not be able to create /var/spool/mail because programs run from .forward don't inherit the special permissions needed to create files in that directory.

AUTHORS

Simon Cozens <simon@cpan.org> wrote versions 1 and 2.

Meng Weng Wong <mengwong@pobox.com> turned a petite demure v2.0 into a raging bloated v2.1, adding MIME support, emergency recovery, filename interpolation, and autoreply features.

SEE ALSO

Mail::Internet, Mail::SMTP, Mail::Audit::List, Mail::Audit::PGP, Mail::Audit::MAPS, Mail::Audit::KillDups, Mail::Audit::Razor...