#!/usr/bin/perl
use Env; ## This allows the script to read environment variables
use lib $MY_PERL; ## my custom site_perl directory
use Net::IMAP::Simple;
use Email::Simple;
use Sys::Syslog;
my $user = $IMAP_USER;
my $password = $IMAP_PASS;
my $mailhost = $IMAP_HOST;
###########################
# subroutine log_msg
###########################
sub log_msg {
my $error_msg = shift;
openlog $0,'nowait,pid','local7';
$rc=syslog('notice', "[ID xxx local7.alert] $error_msg");
closelog(); ## Close the syslog handle
return;
}
my $server = Net::IMAP::Simple->new($mailhost);
$server->login($user, $password);
my $before_msgs = $server->select('INBOX');
my $heartbeat_msg = "BEFORE EXPUNGE: $before_msgs message(s) in the inbox";
log_msg($heartbeat_msg);
my @boxes = $server->mailboxes;
#foreach my $box ( @boxes ) {
# print "BOX: $box\n";
#}##double check the names of the mailboxes
foreach my $msg (1 .. $before_msgs){
my $delete_rc = $server->delete($msg);
print "DELETE RC for msg $msg: $delete_rc\n";
}
my $expunge_rc = $server->expunge_mailbox('INBOX');
print "EXPUNGE RC: $expunge_rc\n";
print "\$?: $? \$! $!\n";
my $after_msgs = $server->select('INBOX');
my $heartbeat_msg = "AFTER EXPUNGE: $after_msgs message(s) in the inbox";
log_msg($heartbeat_msg);
$server->quit();
exit();
####
Printed results:
$ test.pl
DELETE RC for msg 1: 1
DELETE RC for msg 2: 1
EXPUNGE RC:
$?: 0 $! Illegal seek
####
syslog contents:
Sep 15 10:34:08 somebox test.pl[17786]: [ID xxx local7.alert] BEFORE EXPUNGE: 2 message(s) in the inbox
Sep 15 10:34:08 somebox test.pl[17786]: [ID xxx local7.alert] AFTER EXPUNGE: 2 message(s) in the inbox