Deleting IMAP mail from shell script
Sun Advocate Webmaster
webmaster at sunad.com
Thu May 12 19:50:45 EDT 2005
Does 'setinfo expire 7' only apply to messages currently within the
mailbox, or does it apply to future messages as well? References online
seem to be a bit vague on this subject.
Wow... what a script! I will give it a go and see how it works... Thank you!
Ludovic Marcotte wrote:
> Hi,
>
>> I have a simple question concerning mail deletion in Cyrus mailboxes.
>> I have spamassassin set up, with two mailboxes "spam at domain.com" and
>> "notspam at domain.com". A shell script runs periodicly and parses these
>> messages (sa-learn compiles training data from the messages). After
>> that parsing is done, it needs to empty the mailboxes.
>>
>> I was going to add a "rm" command, but I'm thinking that will cause
>> data corruption with cyrus.
>>
>> Is there a better way to do this?
>
>
> You could use a small Perl script along the lines of:
>
> === SNIP ===
> #!/usr/bin/perl
> #
> # cyradm --user=<cyrus admin> <localhost>
> # > cm spam
> # > sam spam user/<username> to give access to a user to the shared
> folder "spam"
> #
> # To set automatic expiration for messages:
> #
> # > setinfo expire 7
> #
> use Mail::IMAPClient;
>
> #
> # Global preferences used in the script.
> #
> my %prefs = (
> Server => "the.imap.server.com",
> Username => "foo",
> Password => "bari",
> Folder => "Shared Folders/spam"
> );
>
> my @messages = undef;
>
> my $imap = Mail::IMAPClient->new(Server=> $prefs{Server},
> User => $prefs{Username},
> Password => $prefs{Password});
>
> if (!defined($imap)) {
> die "Could not connect to the IMAP server.";
> }
>
> if (!$imap->select($prefs{Folder})) {
> die "Could not examine the spam folder.";
> }
>
> @messages = $imap->search("ALL");
>
> for (my $i = 0; $i <= $#messages; $i++)
> {
> my $filename = "/tmp/sa-learn.".$i;
>
> # We retrieve the message and store it into a file
> $imap->message_to_file($filename,$messages[$i]);
>
> $cmd = "su - amavis -c \"/usr/bin/sa-learn --no-rebuild --spam
> ".$filename."\"";
> system($cmd);
>
> # We now remove the temporary file
> $imap->delete_message($messages[$i]);
> unlink($filename);
> }
>
> # We rebuild our database
> `su - amavis -c \"/usr/bin/sa-learn --rebuild\"`;
>
> # Close the IMAP connection
> $imap->expunge();
> $imap->disconnect() or warn "Could not close IMAP connection.";
> === SNIP ===
>
> HTH,
> Ludo
---
Cyrus Home Page: http://asg.web.cmu.edu/cyrus
Cyrus Wiki/FAQ: http://cyruswiki.andrew.cmu.edu
List Archives/Info: http://asg.web.cmu.edu/cyrus/mailing-list.html
More information about the Info-cyrus
mailing list