Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey perl monks, I was wondering if anyone could give me an example of how to forward mail from a folder every 5 seconds using Mail::Audit? Something along the lines of

#! /usr/bin/perl; use Mail::Audit; my $folder = "/home/me/mail/toForward"; my $item = Mail::Audit->new; my $subject = $item->subject(); my ($hour, $wday) = (localtime)[2,6]; if ($wday !=0 and $wday !=6 # Not Saturday/Sunday and $hour > 9 and $hour < 18) { # Between 9am and 6pm $item->resend('archive@theoffice.com'); # resend is the only action # which doesn't end the program. }

I used this example from Simon Cozen's tutorial, but I'm not sure how to change the time sensitivity to every 5 seconds instead of btw 9am-6pm. Also, I'm not sure if this will only fwd mail from the toForward folder. Any advice is welcome.

thanks.

jc

Replies are listed 'Best First'.
Re: Mail::Audit and time sensitivity
by hossman (Prior) on Jul 27, 2003 at 18:46 UTC
    I used this example from Simon Cozen's tutorial, but I'm not sure how to change the time sensitivity to every 5 seconds instead of btw 9am-6pm. Also, I'm not sure if this will only fwd mail from the toForward folder.

    I think you are missunderstanding the intent of this example, and perhaps the purpose of Mail::Audit.

    This module is designed to make it very easy for you to write very complicated real-time mail filters in perl. Or to put it another way: It's not intended for writting scritps that run once (or once every X seconds) and process mail in a specified folder, It's intended to process your mail in real time as it is being delivered to you.

    In Simon's example, there is no "sensitivity" of "btw 9am-6pm" .. what he is doing, is saying that if the current time (that the script is being executed) is durring work hours, then forward the message to his work account. The reason he is looking at the current time, is becuase he knows that his program will be executed at the moment the mail has arrived.

    Quite simply, I think you need to do one of two things...

    1. Don't use Mail::Audit. If your true goal is to write a script that checks for mail that you have recently placed in a "toForward" then you probably want to write yourself a script that uses something like Mail::Box to open a mail box, read the messages, forward them and delete them; and then run it using a cron job
    2. Don't worry about trying to do something every 5 seconds, don't worry about putting mail in a "toForward" folder, find some propety of all the message you recieve that you want to use as a criteria to forward them, and then write a Mail::Audit script that uses that criteria, then make sure you read the Mail::Audit FAQ to see how to get your script to run everytime you get a new email.
Re: Mail::Audit and time sensitivity
by saintbrie (Scribe) on Jul 27, 2003 at 15:25 UTC

    You could do something like:

    unless (time % 5) { (forward mail) }

    The percent is the modulus operator, which gives the integer remainder of the divisor. 6 % 5 is 1. 12 % 5 is 2. 10 % 5 is 0. As time() is delivered in seconds since the epoch, you can see if it is divisible by 5 (every 5 seconds). If it is, you do something with it.

    Alternately, you could put the whole thing in a loop:

    while (1) { sleep 5; (forward mail) }

    But you'll probably want some kind of monitoring service to check to make sure your daemon is still alive and running.

    Of course, if your purpose is simply to forward all mail from an inbox to a particular address, you don't need to use perl for that. More than likely, you could just use your MTA software.

    Sendmail uses .forward files. You can use procmail rules to forward mail. Qmail uses .qmail files. (I'm not familiar with Postfix, but I'm sure there's a way to do it there, too.) check with your service provider.