Search

Monday, December 06, 2010

How to use dovecot expire plugin

Here I explain how to configure and use the expire plugin with dovecot.
 
We have a platform as below.
Fedora release 13 (Goddard)
dovecot 1.2.16:
 
1.In the first place, in /etc/dovecot.conf, we have to enable the use of expire plugin.
protocol pop3 {
  mail_plugins = expire
}
 
protocol lda {
  mail_plugins = expire
}
 
protocol imap {
  mail_plugins = expire
}
 
dict {
  expire = mysql:/etc/dovecot-dict-expire.conf
}
 
# it specifies that mails saved in INBOX, Trash and Spam are to be expired after 15 days
plugin {
  expire = Trash 15 Spam 15 INBOX 15
  expire_dict = proxy::expire
}
 
2.Secondly, we tell the plugin where to keep the timestamp.  In this example, we use a mysql table named mails.expires.
 
cat /etc/dovecot-dict-expire.conf
connect = host=localhost dbname=mails user=username password=password
map {
  pattern = shared/expire/$user/$mailbox
  table = expires
  value_field = expire_stamp
 
  fields {
    username = $user
    mailbox = $mailbox
  }
}
The table would look like this.
mysql> show create table expires
    -> ;
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                  |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| expires | CREATE TABLE `expires` (
  `username` varchar(75) NOT NULL,
  `mailbox` varchar(255) NOT NULL,
  `expire_stamp` int(11) NOT NULL,
  PRIMARY KEY (`username`,`mailbox`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.03 sec)
3.We have to make sure that mysql is supported by dovecot. 
 
yum install dovecot-mysql
4.After restarting dovecot, we shall soon see some entries in the table expires upon emails being saved to INBOX, Trash and Spam.  They represent the timestamp before which emails should be expunged.
 
mysql> select * from expires;
+----------------+---------+--------------+
| username       | mailbox | expire_stamp |
+----------------+---------+--------------+
| tina         | INBOX   |   1294191607 |
| louise     | INBOX   |   1294197182 |
 
5.The plugin is responsible to keep track of timestamp.  The actual expungement is carried out by:
 
/usr/sbin/dovecot --exec-mail ext /usr/libexec/dovecot/expire-tool
Usually, we run this command in daily cron job.
 
What if we have emails in mbox before the plugin is enabled?  They will be expunged accordingly when the first expungement takes place.  The expire-tool finds satisfying emails according to the timestamp in mysql and that will include those emails exist there before our expire plugin is implemented. 
 
Note that we can use /usr/sbin/dovecot --exec-mail ext /usr/libexec/dovecot/expire-tool --test to verify what would be executed before change is made. 
 
6.It is a good idea to turn on dovecot logging if you find the plugin not working.
# Log file to use for error messages, instead of sending them to syslog.
# /dev/stderr can be used to log into stderr.
log_path = /var/log/dovecot.err
 
For example, if you see something like 'dict: Error: Unknown dict module: mysql' in the log, then you know that dovecot-mysql is not installed.
 

No comments:

Post a Comment