# Procmail.pl # Monitors a directory for new saved emails # Emails are in text format saved from Outlook # Other progs may save in slightly different format - YMMV # Required fields in email are - # From: xxxxx [xxxxx@xxxx.xxxx] # Subject: xxxxx xxxxx xxxxx # File name is year-mo-da-hhmmss.txt - used for determining # time of email use Strict; use Win32::ChangeNotify; $indir = "x:/xxx/xxx"; #dir for saved messages $status = "procstat.txt"; #status file $notify = Win32::ChangeNotify->new($indir, 0, FILE_NAME); open(STATUS, ">>".$status) or die "Error opening stats file: $!\n"; my $now = localtime; #start time print STATUS "Procmail started: $now\n"; my $nummail = 0; #number of mails processed my $done = 0; #done processing (back at desk)? while (!($done)) { $notify->wait or warn "Problem waiting: $!\n"; #wait for files if (-e $indir . "/.stopmail") # file created to signal stop { $done = 1; unlink($indir . "/.stopmail"); } opendir(INDIR, $indir) or die "Error opening directory: $!\n"; my @mail = grep { $_ ne '.' and $_ ne '..' } readdir INDIR; foreach (@mail) { my $from = ""; my $subject = ""; my $time; open(INFILE, "<". $indir . "/" . $_) or die "Could not open file: $!\n"; while ($next = ) { chomp($next); if ($next =~ /^From:/) { if ($from eq "") # Only set from if empty to avoid problems with forwards { $next =~ /.*\[(.*)\].*/; $from = $1; $from = (split(/:/,$from))[1]; } } elsif ($next =~ /^Subject:/) { if ($subject eq "") # Set subject if empty to avoid probs with forwards { $next =~ /^Subject: *(.*)/; $subject = $1; for ($subject) { s/^\s+//; } } } } /(.*)\.txt/; # Get time from file name $time = $1; # Compose message info # My phone is limited, so only the facts my $msg = "From: $from\nSubject: $subject\nTime: $time\n"; # Blat works best using a file if (open(OUTMP, ">" . $time . ".tmp")) { print OUTMP $msg; close(OUTMP); my $blatcmd = "c:\\blat\\blat.exe " . $time . ".tmp -s \"New Email\" -t xxxxxxxx\@xxxx.xxx.com"; print STATUS "Blatted:\n"; my $blatres = `$blatcmd`; print STATUS "$blatres\n"; unlink($time . ".tmp"); $nummail ++; } close(INFILE); unlink($indir . "/" . $_); } undef(@mail); sleep(1); closedir(INDIR); $notify->reset; # Wait for next file(s) } $notify->close; $now = localtime; print STATUS "Caught stop message: $now.\n"; print STATUS "$nummail messages processed\n\n"; close(STATUS);