Image

Knowledge base → Deleting mailbox folders via PostfixAdmin

[Virtual servers]
Date of publication: 31.05.2023


By default, when you create a mailbox in postfixadmin, a folder is created for each mailbox. When deleting a mailbox via postfixadmin, only the user is deleted, i.e. access is terminated, but the mailbox folder with all letters remains. If we create a mailbox with the same login, the data will remain the same as if we hadn't deleted the mailbox. In this tutorial, we will set up automatic deletion of mailbox folders when they are deleted in PostfixAdmin.

You can also order a ready-made VPS mail server, you can see more about how it is configured here:

Let's open the configuration file:

/usr/share/nginx/html/postfixadmin/config.inc.php

$CONF['mailbox_postdeletion_script']='sudo -u vmail /usr/local/bin/postfixadmin-mailbox-postdeletion.sh';

Let's create a script file that will delete mailbox folders:

#touch /usr/local/bin/postfixadmin-mailbox-postdeletion.sh

#!/bin/sh

Example script for removing a Maildir from a Courier-IMAP virtual mail

hierarchy.

The script looks at arguments 1 and 2, assuming that they

indicate username and domain, respectively.

The script will not actually delete the maildir. I moves it

to a special directory which may once in a while be cleaned up

by the system administrator.

This script should be run as the user which owns the maildirs. If

the script is actually run by the apache user (e.g. through PHP),

then you could use "sudo" to grant apache the rights to run

this script as the relevant user.

Assume this script has been saved as

/usr/local/bin/postfixadmin-mailbox-postdeletion.sh and has been

made executable. Now, an example /etc/sudoers line:

apache ALL=(courier) NOPASSWD: /usr/local/bin/postfixadmin-mailbox-postdeletion.sh

The line states that the apache user may run the script as the

user "courier" without providing a password.

Change this to where you keep your virtual mail users' maildirs.

basedir=/home/mail

Change this to where you would like deleted maildirs to reside.

trashbase=/var/spool/deleted-maildirs if [ ! -e "$trashbase" ]; then echo "trashbase '$trashbase' does not exist; bailing out."

exit 1

fi if [ echo $1 | fgrep '..' ]; then

echo "First argument contained a double-dot sequence; bailing out." exit 1

fi

if [ `echo $2 | fgrep '..'` ]; then

    echo "First argument contained a double-dot sequence; bailing out."

exit 1

fi

#subdir=`echo "$1" | sed 's/@.*//'`

subdir=`echo "$1"`

maildir="${basedir}/$2/${subdir}" trashdir="${trashbase}/$2/`date +%F_%T`_${subdir}" parent=`dirname "$trashdir"` if [ ! -d "$parent" ]; then if [ -e "$parent" ]; then echo "Strainge - directory '$parent' exists, but is not a directory." echo "Bailing out." exit 1

    else

mkdir -p "$parent" if [ $? -ne 0 ]; then

            echo "mkdir -p '$parent' returned non-zero; bailing out."

            exit 1

        fi

    fi

fi

if [ ! -e "$maildir" ]; then

    echo "maildir '$maildir' does not exist; nothing to do."

    exit 1

fi

if [ -e "$trashdir" ]; then

    echo "trashdir '$trashdir' already exists; bailing out."

    exit 1

fi

Move or delete (Move enebled by Default)

mv $maildir $trashdir #rm -rf $maildir

exit $?

Create a folder to store deleted mailboxes  

mkdir /var/spool/deleted-maildirs 

For this folder, you can configure the frequency of cleaning, this option allows you to avoid loss if the mailbox is accidentally deleted or in case the data may be needed after some time.

Now we need to grant rights to the vmail user to run the script in /etc/sudoers 

# User privilege specification
root ALL=(ALL:ALL) ALL
www-data ALL=(vmail) NOPASSWD: /usr/local/bin/postfixadmin-mailbox-postdeletion.sh 

Now mailbox folders are moved to a separate folder /var/spool/deleted-maildirs when deleted:

mv $maildir $trashdir 
rm -rf $maildir 

Done.





No Comments Yet