Skip to content

Instantly share code, notes, and snippets.

@cdornsife
Last active December 24, 2024 16:13
Show Gist options
  • Save cdornsife/7f71b1eda10165f55736 to your computer and use it in GitHub Desktop.
Save cdornsife/7f71b1eda10165f55736 to your computer and use it in GitHub Desktop.
If you wish to archive all emails in postfix using always_bcc, you can use this script to tame your archive email account. This script relies on doveadm to manage the files. Add it to your daily cron.
#! /bin/bash
# change your /etc/postfix/main.cf
# always_bcc = [email protected]
DOMAIN=example.com
ARCHIVE=archive@$DOMAIN
getemail() {
doveadm fetch -u $ARCHIVE hdr mailbox-guid $1 uid $2 | grep "$3" | cut -f 2 -d "<" | cut -f 1 -d ">"
}
doveadm search -u $ARCHIVE mailbox INBOX |
while read guid uid; do
FOLDER=Sent
EMAIL=$(getemail $guid $uid "^Return-Path:")
IS_EXTERNAL=`echo $EMAIL | grep -i -e "@$DOMAIN" 1> /dev/null ; echo $?`
if [ "$IS_EXTERNAL" == "1" ]
then
EMAIL=$(getemail $guid $uid "^To:")
EMAIL=${EMAIL#"To: "}
FOLDER=Inbox
fi
NAME=`echo $EMAIL | cut -f 1 -d "@" | tr '[:upper:]' '[:lower:]'`
echo "Process $EMAIL"
BOX=INBOX.$NAME.$FOLDER
doveadm mailbox create -u $ARCHIVE -g $guid -s $BOX > /dev/null 2>&1
doveadm move -u $ARCHIVE $BOX MAILBOX INBOX UID $uid
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment