#!/usr/bin/perl # You may need to change this path to /usr/local/bin/perl use POSIX; $mailprog = "/usr/sbin/sendmail"; #************************************************************** # # Script to clean mail queue from WHM: PWSautocleanmailqueue V1.6 # # Written by: # Premier Website Solutions - http://www.premierwebsitesolutions.com # Created - December 8, 2005 (V1.5) # Last Modified - August 6, 2008 (V1.6) # - fixed a small glitch that caused 0 for days to not work # # # Provide the path to the exim folder that contains the input and msglog folders # below, and the sender and sendto email addresses for the email that will be sent # when a cleanup is done. # Upload this script to your server wherever you want # Set ownership and permissions to root:root, and 0700 # Put your search terms and times in the PWSautocleanmailqueue.dat file # and put that file in the same folder as this script # Set a cronjob to run the script automatically ("crontab -e" in shell as root) # example: # 25 */8 * * * perl /path/to/PWSautocleanmailqueue.pl # would run the script every 8 hours, 25 minutes after the hour # # # Registered users of this script will be notified of any future updates. # If you registered this copy with me, put your email here for future reference. # This copy is registered to: # #************************************************************** # This is the path to the exim folder that contains the input and msglog folders. # They may need to be changed depending on how your server is setup. # They must be like this: /path/to/exim/directory $exim_path = "/var/spool/exim"; # If you want to be emailed when this script is run, put your email address here. # If you use spamassassin, you should include a name, like this: # $sendto_email = 'me '; $sendto_email = ''; # If you provide an email address you will be emailed when the script is used. # This will be the subject of the email. You can change it as you wish. $subject = "Some files in the mail queue were deleted"; # This is the sender for the email if you do include a sendto_email. # Change it if you wish. $sender_email = 'Mike '; #------------------------------------------------------------------- #------------------------------------------------------------------- # UNLESS YOU KNOW CGI, DO NOT EDIT ANYTHING BELOW HERE # Feel free to study the code, but alterations are at your own risk #------------------------------------------------------------------- #------------------------------------------------------------------- open (datafile,"PWSautocleanmailqueue.dat") || print "Unable to open the data file"; @lines = ; close (datafile); $message = "Emails Deleted:\n"; foreach $_ (@lines) { chomp($_); if ($_ !~ /^#/ && $_ =~ /\|/) { ($searchterm,$days) = split(/\|/,$_); &clean_queue; } } if ($sendto_email ne "" && $sender_email ne "" && $action eq 1) {&sendmail} exit; sub clean_queue { @eximfolders = `ls -d1 $exim_path/*/`; foreach $_ (@eximfolders) { if ($_ =~ /input/) {$t1 = 1} if ($_ =~ /msglog/) {$t2 = 1} } if ($t1 eq 1 && $t2 eq 1) { @inputfolders = `ls -d1 $exim_path/input/*/`; @msglogfolders = `ls -d1 $exim_path/msglog/*/`; } else { print "Your exim folder path seems to be wrong";exit; } if (!$inputfolders[5] || !$msglogfolders[5]) { print "We can't seem to find one or both of the input or msglog folders";exit; } @folders = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'); foreach $dir (@folders) { @files = `ls -1A $exim_path/input/$dir`; foreach $file (@files) { chomp($file); open (FILE,"$exim_path/input/$dir/$file") or next; @email = ; close (FILE); foreach $line (@email) { if ($line =~ /$searchterm/) { $tmp = "$dir/$file"; push(@todelete,$tmp); last; } } } } foreach $_ (@todelete) { chomp($_); $deleteit = 1; $_ =~ s/(-D|-H)$//; if ($days > "0") { $file_name = "$exim_path/input/$_-D"; &check_age; } if ($deleteit eq "1") { $action = 1; system ("rm -f $exim_path/input/$_-H"); system ("rm -f $exim_path/input/$_-D"); system ("rm -f $exim_path/msglog/$_"); $_ =~ s/^\w\///; $message .= "$_\n"; } } } sub check_age { ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($file_name); ($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime); $timestamp = mktime($sec,$min,$hr,$mday,$mon,$year,$wday,0,-1); $age = (time - $timestamp)/86400; if ($age < $days) {$deleteit = 0} } sub sendmail { # Open The Mail Program open(SENDMAIL,"|$mailprog -t"); print SENDMAIL "To: $sendto_email\n"; print SENDMAIL "From: $sender_email\n"; print SENDMAIL "Subject: $subject\n\n"; print SENDMAIL "$message\n"; close (SENDMAIL); }