Automatic Cleaning Of Trash And/Or Junk Folders With ISPConfig (With Roundcube + Tmpreaper (Tmpwatch))

Want to support HowtoForge? Become a subscriber!
 
Submitted by MuisjeNL (Contact Author) (Forums) on Tue, 2008-08-26 12:53. :: Debian | ISPConfig | Linux | Control Panels | Email

Automatic Cleaning Of Trash And/Or Junk Folders With ISPConfig (With Roundcube + Tmpreaper (Tmpwatch))

Overview:

This short mini-howto will help you set up automatic cleaning of Trash- and Junkfolders. Most (web)mail clients (can) automatically create these. A lot of people don't clean there maildirectories causing Junk and Trash folders to grow massively in size and with ISPConfig (not yet) being able to set good maildirectory size limits the alternative is to have these folders cleaned after several days.

 

Prerequisites:

This tutorial assumes you are currently using the following or similar configuration:

  • Linux Debian Etch (v4.0)
  • ISPConfig (v2.2.24 or later)
  • Make  
  • Nano or vi
  • Roundcubemail (v0.1.1 package install)
  • Atleast 1 domain with 1 mailbox running
  • Junk mail set to be moved to a folder instead of deleting (If not set, See links)

If this configuration does not match yours, you might need to adjust the howto to fit your configuration.

 

Links:

Tutorial move junk mail to a folder instead of deleting: http://www.howtoforge.com/forums/showthread.php?t=15704
Roundcubemail v0.1.1 package install: http://ispconfig.bb-hosting.org/downloads/roundcube/roundcubemail-0.1.1.pkg 

 

1 - Set roundcubemail to automatically create Trash and Junk folders

If you have already done this either by Roundcubemail or other methods, you can skip this chapter. Make sure .Trash and .Junk exist.

To set Roundcubemail to automatically create mailfolders we need to modify the Roundcubemail configuration:

nano /home/admispconfig/ispconfig/web/roundcubemail/config/main.inc.php

Locate "$rcmail_config['create_default_folders']" and make sure it is set to TRUE and make sure the configuration is similar to:

[...]
// store draft message is this mailbox
// leave blank if draft messages should not be stored
$rcmail_config['drafts_mbox'] = 'Drafts';
// store spam messages in this mailbox
$rcmail_config['junk_mbox'] = 'Junk';
// store sent message is this mailbox
// leave blank if sent messages should not be stored
$rcmail_config['sent_mbox'] = 'Sent';
// move messages to this folder when deleting them
// leave blank if they should be deleted directly
$rcmail_config['trash_mbox'] = 'Trash';
// display these folders separately in the mailbox list.
// these folders will also be displayed with localized names
$rcmail_config['default_imap_folders'] = array('INBOX', 'Drafts', 'Sent', 'Junk', 'Trash');
// automatically create the above listed default folders on login
$rcmail_config['create_default_folders'] = TRUE;
// protect the default folders from renames, deletes, and subscription changes
$rcmail_config['protect_default_folders'] = TRUE;
[...]

Now when an user logs in the mailfolders will be created.

NOTE: This requires that the user needs to have logged in, else trash and junk folders just do not exist and won't be cleaned ofcourse!

 

2 - Install tmpreaper

The package tmpwatch used to be in the package manager Aptitude however for unknown reasons it is been replaced. For other linux variants, see tmpwatch.

To do this we enter the following command:

apt-get install tempreaper

IMPORTANT: If you can use tmpreaper skip to chapter 3. If you you have to use tmpwatch don't forget to modify the cron file!

If you cannot use tmpreaper you can download & install tmpwatch as followed:

cd /usr/src
wget http://linux.bononline.nl/linux/tmpwatch/src/tmpwatch-2.9.0.tar.gz
tar xvzf tmpwatch-2.9.0.tar.gz
cd tmpwatch-2.9.0
make
make install

Thanks to MTvermoes!

 

3 - Add a cronjob

We now need to add a cronjob so the system will check daily which emails are going to be needed for deletion. We suggest you set this to cron.daily making it check every day for mails that are old enough to be deleted. However you could also place it in cron.hourly which is neccesary if you use a maximum time of less than 24 hours else it still would only be checked on daily basis.

To do this we go to /etc/cron.daily

cd /etc/cron.daily

Now we create a new file "clean-mailfolders"

nano clean-mailfolders

And make it look like:

#!/bin/sh
# Time to wait before removing mails from the Junk folder (Default: 7 days) Set 0 to turn off.
junk_max_hours=$((24*7))
# Time to wait before removing mails from the Trash folder (Default: 30 days) Set 0 to turn off.
trash_max_hours=$((24*30))
for domain in /var/www/*
do
  if [ -d "$domain"/user ]
  then
    for user in $domain/user/*
    do
      if [ "$junk_max_hours" -gt "0" ]
      then
        if [ -d "$user/Maildir/.Junk" ]
        then
          tmpreaper -m $junk_max_hours $user/Maildir/.Junk/{cur,new}
        fi
      fi
      if [ "$trash_max_hours" -gt "0" ]
      then
        if [ -d "$user/Maildir/.Trash" ]
        then
          tmpreaper -m $trash_max_hours $user/Maildir/.Trash/{cur,new}
        fi
      fi
    done
  fi
done

You might want to change the dates to fit your own needs. In the beginning of the file 2 variables are given named "junk_max_hours" containing the maximum of hours mail in the Junk folder is kept and "trash_max_hours" containing the maximum of hours mail in the Trash folder is kept. Setting the value to 0 will disable the cleanup of that type.

NOTE: If you have to use tmpwatch instead of tmpreaper don't forget to modify the cron file and replace "tmpreaper" with "tmpwatch".

 

Conclusion

Make sure that you check this before actually running it on production servers. This tutorials comes with no garantuee whatsoever. Be wise, test!

You can modify the variables to fit your own needs as described earlier.

Hope you like my first tutorial!


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by ivix (not registered) on Mon, 2011-05-09 16:30.

I think there still is missing at the end of this command:

chmod +x /etc/cron.daily/clean-mailfolders

Submitted by Paul Rensel (not registered) on Thu, 2010-09-09 20:09.

Changed for ispconfig 3.0 on Debian Lenny

 

#!/bin/sh
# Time to wait before removing mails from the Junk folder (Default: 7 days) Set 0 to turn off.
junk_max_hours=$((24*7))
# Time to wait before removing mails from the Trash folder (Default: 30 days) Set 0 to turn off.
trash_max_hours=$((24*30))
for domain in /var/vmail/*
do
  if [ -d "$domain" ]
  then
    for user in $domain/*
    do
      if [ "$junk_max_hours" -gt "0" ]
      then
        if [ -d "$user/.Junk" ]
        then
          tmpreaper -m $junk_max_hours $user/.Junk/{cur,new}
        fi
      fi
      if [ "$trash_max_hours" -gt "0" ]
      then
        if [ -d "$user/.Trash" ]
        then
          tmpreaper -m $trash_max_hours $user/.Trash/{cur,new}
        fi
      fi
    done
  fi
done
 

Submitted by Teiwaz (registered user) on Mon, 2009-11-30 04:27.
This script is very cool! Bye bye my old spam!