Deleting IMAP mail from shell script

Sun Advocate Webmaster webmaster at sunad.com
Fri May 13 13:27:10 EDT 2005


Thank you for the information!

I tried the script, but authentication is failing. Cyrus is set up to do 
virtual domains based on userid. I set the server in the perl script to 
localhost, and the username "spam at domain.com" with the appropriate 
password. It fails. I verified with imtest that my credentials are correct.

I checked my cyrus logs and it appears the perl script is hacking off 
the domain, so the username cyrus gets is "spam.com".

Any suggestions?

Thanks!



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