Page 1 of 1

How I can delete mails in subfolders?

Posted: Mon Aug 20, 2007 4:43 am
by fho
Hello,

how can I delete mails in subfolders?
With omtidyu its only possible to clean some Top-folders. (Why its don't support to clean custom (sub)folders? Shouldn't be much complicated to implement that)

I have folders that contain over >10.000 mails.. I can't delete them with a client or the webinterface, because i always get an timeout when I try to remove more than ~200 mails at once.

Posted: Tue Aug 21, 2007 5:17 am
by fho
there is no way to clean up my mail folders except creating an new account? That sounds like a very good mailserver design :)
WTF? :o

Posted: Tue Aug 21, 2007 5:56 am
by mikethebike
you could move the folder to the top level and then use omtidyu?

omtidyu -B -u user -Tf -l <foldername> -a 0 -dk

Posted: Tue Aug 21, 2007 5:59 am
by fho
there is no direct way to do this simple task?

Posted: Tue Aug 21, 2007 6:04 am
by mikethebike
not for an individual sub-folder, although omtidyu does allow nested search, but that would apply to all sub-folders, so:

omtidyu -B -u user -S -Tf -l <foldername> -a 0 -dk

would delete everything in all sub-folders.
If the messages in that folder all have something common in the subject line or sender, you could use the subject or sender in you criteria, but do it in report mode first

omtidyu -B -u user -S -Tf -l <foldername> -t "subject text" -R

Posted: Tue Aug 21, 2007 10:32 am
by ianare
here you go. I have been using this script in cron to delete emails in the 'spam' folder that each of the users have. However it would be a simple matter to use it to delete all messages in a certain folder. May take a while though, as it gets the timestamp for each mail.

Code: Select all

#!/usr/bin/perl
#
# Looks in a user's spam folder and deletes
# all messages received within a specified
# amount of days.
#
# Copyright (C) 2006-2007 Lorien Technologies < www.lorientech.com >
#
# Authors : Ianaré Sévi, Phillip Bunker
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


use strict;
use warnings;
use Getopt::Std;
use Mail::IMAPClient;
use POSIX qw(strftime);
use Date::Calc qw(Parse_Date Decode_Date_EU Date_to_Days);

# --- User Variables --- #
my $host="mail.myserver.com"; # FQDN
my $username="sxadmin"; # scalix admin
my $password="topsecret"; # admin password
my $folder="Inbox/spam"; # spam folder
my $days=7; # default days


sub showUse{
   print "spam-clean.pl\nDeletes all SPAM messages over a certain age from IMAP users.\n\n";
   print "Usage:\t\t\tspamclean.pl -u <username || all> [options]\n\n";
   print "options: -d days\tnumber of days (-1 = all messages)\n\n";
}

my @real_users=();
my ($year1,$month1,$day1) = Parse_Date(`/bin/date`);
my %opt = ();

# parse user options
getopts("u:d:", \%opt);
if (exists($opt{u})) {
    if ($opt{u} eq "all") {
        @real_users = (`/opt/scalix/bin/omshowu -m all -i`);
    } else {
        @real_users = $opt{u};
    }
} else {
    showUse();
}
if ($opt{d}) {
    $days = $opt{d};
}

foreach my $punter (@real_users)
{
    if ($punter !~ /sxadmin|sxqueryadmin|$username/){
        chomp $punter;
        print "$punter:\n";
       my $user="mboxadmin:$username:$punter";
        my $imap = new Mail::IMAPClient('Server' => $host ,'User' => $user,'Password' => $password) or die "could not select user";
       $imap->select($folder) or next; #die "could not select folder";
       my @list=$imap->messages or next;
       my $i = 0;

       foreach my $msg (reverse(@list)){
           my $date = $imap->date($msg);   
          $date = substr $date, 0,16;
            my $diff = 0;
           
            # good date, calculate difference
            if (length($date) > 10){
               my ($year2,$month2,$day2) = Decode_Date_EU($date);           
                $diff = Date_to_Days($year1,$month1,$day1) - Date_to_Days($year2,$month2,$day2);
            }
            # malformed date
            else{
                $diff = -1;
            }
           
          if ($diff > $days || $diff < 0){
                $imap->delete_message($msg) or die "Could not delete message: $msg";
                $i++;
          }
          
       }
    $imap->expunge($folder) or die "Could not expunge folder.";
    print "$i messages deleted\n";
   
    }
}

# EOF

Posted: Tue Dec 04, 2007 6:47 am
by fho
thank you ianare for your perl Script, because I don't handle Perl, I wrote an python Script that delete all messages in an folder.. It worked good for my SPAM Folder and an Mailinglist Folder with 15k mails...

But no I have problems 2 other Foldes, my python Script get an search result of 0 mails, also I've I search for All mails...
But in the scalix webinterface and in Thunderbird I get all ~37.000 mails listed
What could be wrong?

Code: Select all


fho@schneewittchen:~/Documents/coding/scalix$ cat delmails.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Last modified: 04/12/07 11:37:28(CET) by Fabian Holler

import imaplib,string,sys

imapserver="scalix"
imapuser="fho@zensored"
imappasswd="zensored"
mailboxes=[ "logs", "logs/Logwatch", "logs/backup" ]

con=imaplib.IMAP4(imapserver)
con.login_cram_md5(imapuser,imappasswd)
result=""
for mailbox in mailboxes:
    print "Mailbox: "+mailbox
    if not con.select(mailbox,readonly=None)[0] == "OK":
        print "Mailbox auswählen fehlgeschlagen"
        sys.exit()
#    typ, data = con.search(None,'(BEFORE 30-DEC-2009)') #Alle Mails löschen die älter sind als..
    typ, data = con.search(None,'(UNSEEN)') #Alle Mails löschen
    print data
    count=0
    for num in string.split(data[0]):
        con.store(num,"+FLAGS.SILENT",'\DELETED')
        print "Deleted message: "+num+" "+(con.fetch(num,'(BODY[HEADER.FIELDS(SUBJECT)])'))[1][0][1]
        count+=1
    con.expunge()
    result+="\ndeleted "+str(count)+" messages in "+mailbox+"\n"
    con.close()
con.logout()
print result