|
NAMEMail::Procmail - Procmail-like facility for creating easy mail filters.SYNOPSISuse Mail::Procmail; # Set up. Log everything up to log level 3. my $m_obj = pm_init ( loglevel => 3 ); # Pre-fetch some interesting headers. my $m_from = pm_gethdr("from"); my $m_to = pm_gethdr("to"); my $m_subject = pm_gethdr("subject"); # Default mailbox. my $default = "/var/spool/mail/".getpwuid($>); pm_log(1, "Mail from $m_from"); pm_ignore("Non-ASCII in subject") if $m_subject =~ /[\232-\355]{3}/; pm_resend("jojan") if $m_to =~ /jjk@/i; # Make sure I see these. pm_deliver($default, continue => 1) if $m_subject =~ /getopt(ions|(-|::)?long)/i; # And so on ... # Final delivery. pm_deliver($default); DESCRIPTIONprocmail is a great mail filter program, but it has weird recipe format. It's pattern matching capabilities are basic and often insufficient. I wanted something flexible whereby I could filter my mail using the power of Perl.I've been considering to write a procmail replacement in Perl for a while, but it was Simon Cozen's "Mail::Audit" module, and his article in The Perl Journal #18, that set it off. I first started using Simon's great module, and then decided to write my own since I liked certain things to be done differently. And I couldn't wait for his updates. "Mail::Procmail" allows a piece of email to be logged, examined, delivered into a mailbox, filtered, resent elsewhere, rejected, and so on. It is designed to allow you to easily create filter programs to stick in a .forward or .procmailrc file, or similar. DIFFERENCES WITH MAIL::AUDITNote that several changes are due to personal preferences and do not necessarily imply deficiencies in "Mail::Audit".
EXPORTED ROUTINESNote that most delivery routines exit the program unless the attribute "continue=>1" is passed.Also, the delivery routines log the line number in the calling program so it is easy to find out which 'rule' caused a specific delivery to take place. pm_initThis routine performs the basic initialisation. It must be called once.Example: pm_init (logfile => "my.log", loglevel => 3, test => 1); Attributes:
pm_gethdrThis routine fetches the contents of a header. The result will have excess whitepace tidied up.The header is reported using warn() if the debug attribute was passed (with a true value) to pm_init(); Example: $m_rcvd = pm_gethdr("received"); # get first (or only) Received: header $m_rcvd = pm_gethdr("received",2); # get 3rd Received: header @m_rcvd = pm_gethdr("received"); # get all Received: headers pm_gethdr_rawLike pm_gethdr, but without whitespace cleanup.pm_bodyThis routine fetches the body of a message, as a reference to an array of lines.Example: $body = pm_body(); # ref of lines $body = join("", @{pm_body()}); # as one string pm_deliverThis routine performs delivery to a Unix style mbox file, or maildir.In case of an mbox file, the file is locked first by acquiring exclusive access. Note that older style locking, with a lockfile with ".lock" extension, is not supported. Example: pm_deliver("/var/spool/mail/".getpwuid($>)); Attributes:
pm_pipe_toThis routine performs delivery to a command via a pipe.Return the command exit status if the continue attribute is supplied. If execution is skipped due to test mode, the return value will be 0. See also attribute "testalso" below. If the name of a lockfile is supplied, multiple deliveries are throttled. Example: pm_pipe_to("my_filter", lockfile => "/tmp/pm.lock"); Attributes:
pm_commandExecutes a system command for its side effects.If the name of a lockfile is supplied, multiple executes are throttled. This would be required if the command manipulates external data in an otherwise unprotected manner. Example: pm_command("grep foo some.dat > /tmp/pm.dat", lockfile => "/tmp/pm.dat.lock"); Attributes:
pm_resendSend this message through to some other user.Example: pm_resend("root"); Attributes:
pm_rejectReject a message. The sender will get a mail back with the reason for the rejection (unless stderr has been redirected).Example: pm_reject("Non-existent address"); pm_ignoreIgnore a message. The program will do nothing and just exit with a DELIVERED status. A descriptive text may be passed to log the reason for ignoring.Example: pm_ignore("Another make money fast message"); pm_dupcheckCheck for duplicate messages. Reject the message if its message ID has already been received.Example: pm_dupcheck(scalar(pm_gethdr("message-id"))); Attributes:
Warning: In the current implementation, the DBM file will grow unlimited. A separate tool will be supplied to expire old message IDs. pm_lockfileThe program will try to get an exclusive lock using this file.Example: $lock_id = pm_lockfile("my.mailbox.lock"); The lock id is returned, or undef on failure. pm_unlockfileUnlocks a lock acquired earlier using pm_lockfile().Example: pm_unlockfile($lock_id); If unlocking succeeds, the lock file is removed. pm_logLogging facility. If pm_init() was supplied the name of a log file, this file will be opened, created if necessary. Every log message written will get a timestamp attached. The log level (first argument) must be less than or equal to the loglevel attribute used with pm_init(). If not, this message will be skipped.Example: pm_log(2,"Retrying"); pm_reportpm_report() produces a summary report from log files from Mail::Procmail applications.Example: pm_report(logfile => "pmlog"); The report shows the deliveries, and the rules that caused the deliveries. For example: 393 393 deliver[203] /home/jv/Mail/perl5-porters.spool 370 370 deliver[203] /home/jv/Mail/perl6-language.spool 174 174 deliver[203] /home/jv/Mail/perl6-internals.spool 160 81 deliver[311] /var/spool/mail/jv 46 deliver[337] 23 deliver[363] 10 deliver[165] The first column is the total number of deliveries for this target. The second column is the number of deliveries triggered by the indicated rule. If more rules apply to a target, this line is followed by additional lines with an empty first and last column. Attributes:
If no logfile attribute is passed, pm_report() reads all files supplied on the command line. This makes it straighforward to run from the command line: $ perl -MMail::Procmail -e 'pm_report()' syslog/pm_logs/* USING WITH PROCMAILThe following lines at the start of .procmailrc will cause a copy of each incoming message to be saved in $HOME/syslog/mail, after which the procmail-pl is run as a TRAP program (see the procmailrc documentation). As a result, procmail will transfer the exit status of procmail-pl to the mail transfer agent that invoked procmail (e.g., sendmail, or postfix).LOGFILE=$HOME/syslog/procmail VERBOSE=off LOGABSTRACT=off EXITCODE= TRAP=$HOME/bin/procmail-pl :0: $HOME/syslog/mail WARNING: procmail seems to have problems when $HOME/syslog/mail gets too big (over 50Mb). If you want to maintain a huge archive, you can specify excess extents, like this: :0: $HOME/syslog/mail-ext1 :0: $HOME/syslog/mail-ext2 EXAMPLEAn extensive example can be found in the examples directory of the "Mail::Procmail" kit.SEE ALSOMail::InternetLockFile::Simple procmail documentation. AUTHORJohan Vromans, Squirrel Consultancy <jvromans@squirrel.nl>Some parts are shamelessly stolen from Mail::Audit by Simon Cozens <simon@cpan.org>, who admitted that he stole most of it from programs by Tom Christiansen. COPYRIGHT and DISCLAIMERThis program is Copyright 2000,2004 by Squirrel Consultancy. All rights reserved.This program is free software; you can redistribute it and/or modify it under the terms of either: a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" which comes with Perl. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the Artistic License for more details.
Visit the GSP FreeBSD Man Page Interface. |