系统管理工具包: 监视邮件的使用情况
2008-11-12 08:29:40 来源:WEB开发网在这个示例中,有三个用户出现了极端的情况。applicants 使用单个邮件文件夹存储其所有的邮件,而 finance 则合理地将其电子邮件分散到不同的文件夹中,以便进行组织。同时,sandra 看上去对电子邮件进行了合理的组织,但是仅使用了一个文件夹来保存她所发送的所有电子邮件。
可能需要向 applicants 和 sandra 提供相关的建议,以指出如何更好地组织他们的电子邮件。但是您是否可以为他们提供帮助呢?
自动地对电子邮件进行归档
有许多解决方案可以归档和自动地组织电子邮件。其中有些解决方案嵌入到了您的客户端中,有些解决方案嵌入到了邮件服务器中。例如,Cyrus IMAP 服务器附带了 Sieve 系统(请参见参考资料),它可以在电子邮件传递到用户的邮箱时,对其进行自动过滤。
尽管这样做可以帮助过滤新的电子邮件,但是对于现有的电子邮件,仍然需要手工地进行组织。要完成这项任务,其中一种方法是使用脚本,该脚本可以像任何其他的客户端那样访问邮箱,然后根据脚本中嵌入的某些规则,组织和过滤您的电子邮件。
清单 7 显示了一个 Perl 脚本,它可以像客户端那样访问电子邮件文件夹,并处理其中的内容。在这个示例中,该脚本在原始文件夹中创建了一个新的子文件夹结构,并按照电子邮件的日期对其进行组织,每年一个文件夹,并且在其中为每个月创建一个文件夹。
清单 7.根据日期自动地对某个文件夹中的电子邮件进行重新组织
#! /usr/bin/perl
# Mail filter to file mail on a date basis
use Mail::IMAPClient;
use Date::Parse;
use Data::Dumper;
use strict;
use warnings;
# The IMAP Server
my $Server = 'imap.example.com';
# The Mailbox we want to filter
my $INBOX = "Sent-Mail";
# Open the server connection
my $IMAP = Mail::IMAPClient -> new (Server => $Server,
User => 'user',
Password => 'password',);
# Open the mailbox we want to filter
$IMAP->select($INBOX) or die "Couldn't select $INBOX";
# We want to filter every message, so obtain a list of every
# message by the message ID
my @msgids = $IMAP->search("ALL");
# Don't do anything if there's nothing to process
exit(0) if (scalar @msgids == 0);
# Now parse the message contents to determine
# the From, To, Subject and Address of each message
my $parsed = $IMAP->parse_headers(
$IMAP->Range(@msgids),
"From",
"To",
"CC",
"Subject",
"Date",
);
# Set up some message counters
my $toprocess = scalar @msgids;
my $processed = 0;
my $counter = 0;
# Process each message
foreach my $msgid (keys %{$parsed})
{
$processed++;
# Extract the date, and build a new folder path
# The new path will split up emails first by
# year and then by month, all as subfolders
# of the current folder
my ($ss,$mm,$hh,$day,$month,$year,$zone) =
strptime($parsed->{$msgid}->{Date}->[0]);
# Try another date if the first one couldn't be identified
if (!defined($year))
{
($ss,$mm,$hh,$day,$month,$year,$zone) =
strptime($parsed->{$msgid}->{Date}->[1]);
}
# default to 2004 if we can't find a year
if (!defined($year))
{
$year = 2004;
}
# Make some assumptions about the year
# Occasionally a date will contain only two digits
# So assume it's either in the year 2000, or 1990+
$year += 2000 if ($year <10);
$year += 1900 if (($year >90) && ($year <= 200));
$year = 2004 if $year <= 1990;
$month += 1;
# Construct the new folder path
my $destfolder = sprintf('%s/%s/%02d',$INBOX,$year,$month);
# Grab the entire message
my $Message = $IMAP -> message_string($msgid);
# Try to change to the destination folder,
# or create it if we couldn't select the folder
my $selectstat = $IMAP->select($destfolder);
unless ($selectstat)
{
$IMAP->create($destfolder);
}
# Go back to the Inbox so that we select the right message
# next time round
$IMAP->select($INBOX);
# Add the original message to the new folder
my $AppendStatus = $IMAP -> append_string($destfolder,$Message);
# When you add a message to a folder, the message
# is marked as unread, so mark all the messages
# in the folder as read by reading them
$IMAP->select($destfolder);
my @unseenMIDs = $IMAP->unseen();
foreach my $MID (@unseenMIDs)
{
$IMAP->message_string($MID);
}
# Go back to the original folder, and delete the message
# if it was successfully moved
$IMAP->select($INBOX);
if ($AppendStatus)
{
$IMAP -> delete_message($msgid);
}
$counter++;
}
# Print out a summary of what we achieved
printf("Processed %5d out of %5d msgsr",$counter,$toprocess);
# Make sure we clean out the folder where we deleted messages
# and then disconnect
$IMAP->expunge();
$IMAP->disconnect();
更多精彩
赞助商链接