MeidokonWiki:

Postfix/Emptying_and_processing_mail_in_maildirs

From http://archives.neohapsis.com/archives/postfix/2009-10/0470.html

Someone asked how to process mails in a mailbox, eg. for spam processing, training filters, etc.


Removing old messages

here's what I use:

5 1 * * * find /path/to/.SPAM/{cur,new} -type f -ctime +7 -exec rm {} \;

Deletes all the spam over 7 days old at 0105 daily.

Processing ham

> -----Original Message-----
> From: owner-postfix-userspostfix.org [mailto:owner-postfix-
> userspostfix.org] On Behalf Of Bob Cohen
> Sent: Wednesday, October 14, 2009 1:26 PM
> To: users Postfix
> Subject: Emptying SPAM account
>
> I have set up SpamAssissin with an account to collect rejected
> emails. Is there a way to periodically empty the mail queue for that
> account with a cron job or some other such method that does not
> require human intervention

Not sure if this is helpful. We use horde webmail and have it setup for people can mark ham/spam. These messages are dropped into an IMAP account (name ham/spam). We have a script (that originated somewhere on the net) that walks those mailboxes and performs the necessary actions.

Anyway, If these email messages are in a IMAP enabled mailbox, you could use the script to train, move, delete, whatever you want to do, on the messages there. It's python, but I think the default installs of python should be able to run it without any problems. This particular script only does the ham. Minor changes, makes it handle spam.

Hope this helps.

   1 #!/usr/bin/env python
   2 import commands, os, time
   3 import imaplib
   4 import sys, re
   5 import string, random
   6 import StringIO, rfc822
   7 
   8 # Set required variables
   9 PREFS = "/etc/mail/spamassassin/local.cf"
  10 TMPFILE = "/var/tmp/salearn.tmp"
  11 SALEARN = "/usr/bin/sa-learn"
  12 SERVER = "myhostname"
  13 USER = "mylogin"
  14 PASSWORD = "mypass"
  15 LOGFILE = "/var/log/learn.ham.log"
  16 log = file(LOGFILE, 'a+')
  17 log.write("\n\nTraining SpamAssassin on %s at %s\n" % (time.strftime("%Y-%m-%d"),
  18 time.strftime("%H:%M:%S")))
  19 
  20 # connect to server
  21 server = imaplib.IMAP4(SERVER)
  22 
  23 # login
  24 server.login(USER, PASSWORD)
  25 server.select("INBOX")
  26 
  27 # Get messages
  28 typ, data = server.search(None, 'ALL')
  29 for num in data[0].split():
  30         typ, data = server.fetch(num, '(RFC822)')
  31         tmp = file(TMPFILE, 'w+')
  32         tmp.write(data[0][1])
  33         tmp.close()
  34         log.write(commands.getoutput("%s --username=filter --prefs-file=%s --spam --forget %s" % \
  35                 (SALEARN, PREFS, TMPFILE)))
  36         log.write("\n")
  37         log.write(commands.getoutput("%s --username=filter --prefs-file=%s --ham %s" % \
  38                 (SALEARN, PREFS, TMPFILE)))
  39         log.write("\n")
  40         # Make a copy if it for testing at this time.
  41         # server.copy(num, 'Inbox/Processed')
  42         # Mark learned ham as "Deleted"
  43         server.store(num, '+FLAGS', '\\Deleted')
  44 # Delete messages marked as "Deleted" from server
  45 server.expunge()
  46 server.logout()

MeidokonWiki: Postfix/Emptying_and_processing_mail_in_maildirs (last edited 2010-02-04 09:45:52 by furinkan)