Connect. Communicate. Collaborate. Securely.

Home » Kerio User Forums » Kerio Connect » Batch move or delete mail from or to certain email addresses (In case you need to clean up your mail server)

Messages: 119
Karma: 0
Send a private message to this user
I needed to cleanup our mailserver by deleting mail from and to certain email addresses. I created these two PERL and BASH scripts to help me a hand. You'll need to create a file called tocheck.txt with the names of the users of which you would like to move or delete mail for. And a file with keywords or email addresses to check the emails for, called adresseslist.txt.
if [ `id -u` != "0" ]; then
	echo "Sorry, you are not root."
	exit 1


# Only check certain users specified in tocheck.txt
cat tocheck.txt | while read user;

	# Base folder
	echo "Checking. You will only see matched items as output. Please be patient.";
	echo "";

	# Specify search parameter, excluded folders and loop the perl script for each true result
	find "$base" -wholename '*#msgs/*' -name '*.eml' ! -wholename '*Work/*' ! -wholename '*Calendar/*' ! -wholename '*Tasks/*' ! -wholename '*#deleted/*' ! -wholename '*Drafts/*' ! -wholename '*Journal/*' ! -wholename '*Junk E-mail/*' ! -wholename '*__keriomapi__STORE/*' ! -wholename '*Notes/*' ! -wholename '*Posta indesiderata/*' ! -wholename '*Contacts/*' ! -wholename '*Deleted/*' | while read eml; 
	echo Checking "$eml";
		# Call external script and if entry in adresseslist.txt matches, move the email
                folder=$dest`dirname "$eml"`
                email=`basename "$eml"`
		if perl ./ ./adresseslist.txt  "$eml"; 
			# If destination folder does not exist, make one
			if [ ! -d "$folder" ]; then
				mkdir -p "$folder"; 
				echo Made folder "$folder"; 
			# If the file has been previously copied, don't move it again
			echo $dest/$eml
			if [ ! -f "$dest/$eml" ]; then
				mv "$eml" "$folder";  
				echo Moved $email to "$folder";
			echo "";

And the PERL script that checks the headers:
use strict;
use Email::Simple::Header;
use Email::AddressParser;

my @headers_to_check =

die "usage:\n$0 adresseslist.txt file.eml\n"
  unless @ARGV == 2 and -e $ARGV[0] and -e $ARGV[1];

open FH, "<$ARGV[0]" or die;
my @keywords = <FH>;
map { s/[\r\n]+//g } @keywords;

# slurp in just the headers part
my $headers_text;
open FH, "<$ARGV[1]" or die;
while (<FH>) {
    $headers_text .= $_;
    last unless /[^\r\n]/;

my $found = '';
my $header = Email::Simple::Header->new($headers_text);
foreach my $name (@headers_to_check) {
    my $value = $header->header($name);
    next unless $value;
    my @addresses = Email::AddressParser->parse($value);
    foreach my $address (@addresses) {
        foreach my $keyword (@keywords) {
            if (lc($keyword) eq lc($address->address)) {
                $found = $address->address;
        last if $found;
    last if $found;
print "Matched email address: $found\n" if $found;
exit ($found ? 0 : 1);

You can use another script to rename the index.fld to index.bad.
Thanx to my friend MoonStruck for his help with the PERL script.
Pavel Dobry (Kerio)

Messages: 5245
Karma: 251
Send a private message to this user
I would like to remind everyone that by modifying the message store you can easily broke the data consistency or synchronization with clients. Our technical support will have very little chance to help you then.

Messages: 119
Karma: 0
Send a private message to this user
Of course, he's right. For internal use only and for own personal risk. Untill now, I haven't found any problems with our 6.7.3 installation but your milage may vary.

Messages: 1
Karma: 0
Send a private message to this user
If you need more flexibility in finding/deleting emails, here is a biterscripting script that will delete files from the email account you specify.

# Script DeleteEmail.txt
var str list, email, from, searchstr, content
set $searchstr = "^From&"+$from+"^"
cd "/opt/kerio/mailserver/backup"
# Collect list of mail files.
lf -r -n -g "*.eml" > $list
while ($list <> "")
    lex "1" $list > $file
    cat $file > $content
    if ( { sen -c -r $searchstr $content } > 0 )
        system del ("\""+$file+"\"")

Copy and paste the script into file C:/Scripts/DeleteEmail.txt, kick off biterscripting, and enter this command.

script "C:/Scripts/DeleteEmail.txt" from("someguy<_at_>")

It will remove all emails that contain any of these strings.

From: someguy<_at_>
From: <> someguy<_at_>
From:<> someguy<_at_>

I am using the regular expression with &, so any combinations will match. Also I am using -c (case independent), so any capitalization will match.

Previous Topic: Can't disable Archive Folder Access for one user
Next Topic: Slow Startup / Too Much Disk Activity?
Goto Forum:

Kerio discussion forums are intended for open communication between forum members and may contain information and material posted by members which may be useful in learning about Kerio products. The discussion forums are not intended to provide technical support for any specific product. Any information implied or expressed in the discussion forums is that of the posting member. Kerio is in no way responsible for the information posted in the forums, or its accuracy. Kerio employees may participate in the discussions, but their postings do not represent an offical position of the company on any issues raised or discussed. Kerio reserves the right to monitor and maintain the forums to promote free and accurate exchange of information.

Current Time: Tue Oct 17 09:45:28 CEST 2017

Total time taken to generate the page: 0.00444 seconds
.:: Contact :: Home ::.
Powered by: FUDforum 3.0.4.