Auto delete trash and junk emails.

Discussion in 'Installation/Configuration' started by mikechacra, Mar 22, 2014.

  1. mikechacra

    mikechacra New Member

  2. tahunasky

    tahunasky New Member

    I have no idea if it works or not, however i have been using this script for a couple of years now...

    Code:
    #!/bin/bash
    # this script deletes junk and trash mail older than +(days)
    
    MAILDIRS=$(find /var/vmail/*/*/Maildir/ -maxdepth 0 -type d)
    for basedir in $MAILDIRS; do
    	for dir in .Trash .Junk ; do
    		for dir2 in cur new; do
    			[ -e "$basedir/$dir/$dir2" ] && (
    			#echo "Processing $basedir/$dir/$dir2..."
    			#echo "Clean $dir - deleting from $dir2"
    			if [ "$dir" = ".Trash" ]; then
    				find "$basedir/$dir/$dir2/" -type f -mtime +28 -delete
    			else
    				find "$basedir/$dir/$dir2/" -type f -mtime +7 -delete
    			fi
    			)
    		done
    	done
    done
    
    I then have a cron job that runs it a couple of times a week.
    Trash is deleted after 28 days, Junk after 7.
     
  3. mikechacra

    mikechacra New Member

    hello tahunasky

    thanks for the script.

    can we test it on 1 mailbox to make sure its deleting the correct folders. and what are $dir1 & $dir2 variables ?
     
  4. tahunasky

    tahunasky New Member

    yes it can be tested on one mail box.. just modify this line:

    MAILDIRS=$(find /var/vmail/*/*/Maildir/ -maxdepth 0 -type d)

    and replace the */* with the domain and user names ( for email address [email protected] ) like this:

    MAILDIRS=$(find /var/vmail/mydomain.com/user1/Maildir/ -maxdepth 0 -type d)

    $dir holds the names of directories after Maildir - eg Inbox, Sent, Trash, Junk... etc
    so if $dir = Trash or Junk in the for loop, script will then do the next for loop, which looks at $dir2.
    $dir2 holds the names of directories under $dir - eg cur, new
    so if any files in $dir2 are older than specified they are deleted.
     
    Last edited: Mar 26, 2014

Share This Page