Hi!

Not really what you're asking, but sharing it anyway.

I wrote a little script to remove users mailboxes that no longer exist. (meaning: can no longer be resolved though the postfix postmap tool)

What this does:

- it traverses through all directories in the /var/vmail dir, these should all be valid mailboxes

- it asks postmap if that address can be delivered to

- if not, it writes to a bash file that you could run, to move the longer-longer valid email directories to another location.

Sharing it below, I hope it will help you, or perhaps someone else.

If anyone has something similar for the SOGo database entries for no longer existing users, I would like to see it. :-)

Here is the little script that I put under /etc/cron-daily

#!/bin/bash

# init variables, customize below
# send notifications to
EMAIL="notificati...@company.com"
# what domain to use to check deliverability
DOMAIN="domain.org"
# output for bash cleanup commands-file
COMMANDS="/var/vmail-orphaned/orphan-mailboxes"
# file to email
STATUS="/var/vmail-orphaned/orphaned-status"
# where mailboxes are stored
MAILBOXES_UNDER="/var/vmail"
# where to move mailboxes to, to generate the bash cleanup file
MAILBOXES_ORPHANED="/var/vmail-orphaned"
# postfix ldap config file
POSTFIX_LDAP="ldap:/etc/postfix/ad-mailboxes.cf"
# no need to customize below here
POSTMAP=$(which postmap)

# init the bash cleanup run file
echo "# " $(date) > $COMMANDS
echo "# remove the following safety exit, if you actually want to run the file:" 
>> $COMMANDS
echo "exit" >> $COMMANDS
echo "" >> $COMMANDS

# init the email status file
rm -f $STATUS
touch $STATUS

cd $MAILBOXES_UNDER

# traverse all email folders
for DIR in */ ; do
    # convert directory name into an emailaddress
    # first remove the trailing slash:
    LOCALPART=$(echo $DIR | sed 's:/*$::')
    # convert to email address
    TEST_EMAIL="$LOCALPART@$DOMAIN"

    # check if postfix can deliver to that mailbox, using the postmap utility
    # returns empty if undeliverable
    DELIVERABLE=$(eval "$POSTMAP -q $TEST_EMAIL $POSTFIX_LDAP")

    # then action, based on deliverability
    if [ -z "$DELIVERABLE" ] # no output: meaning not deliverable
    then
        echo "Postmap says: NOT deliverable: $TEST_EMAIL ($DIR)" >> $STATUS
        echo "mv $MAILBOXES_UNDER/$LOCALPART $MAILBOXES_ORPHANED" >> $COMMANDS
    fi
done

# send email
if [ -s $STATUS ]; then
    cat $STATUS | mail -s "Daily overview of orphaned mailboxes" $EMAIL
fi
--
users@sogo.nu
https://inverse.ca/sogo/lists

Reply via email to