Doveadm scripting

Below is an approach to scripting automatic processing of the Inbox.

Delete old emails

The script below is to purge emails from certain senders, and/or certain subject and after a certain number of days. The scripts are installed on /home/vmail/bin folder.

poe
#!/bin/bash
#
POE=/home/vmail/bin/purgeoldmail
COE=/home/vmail/bin/countemail
#
$COE
#
# Format
# $POE <From> [Subject] <# of days old>
#
# Delete all emails from HelloFresh after 9 days
$POE "HelloFresh"                "" 9
# Delete all emails from SendGrid with subject "Daily SendGrid Statistics Report" after 2 days 
$POE "SendGrid"                  "Daily SendGrid Statistics Report" 2
</code>
 
Purge email
<file bash purgeoldmail>
#!/bin/bash
#
NOW=$(date +"%Y-%m-%d-%H-%M")
DOVEADM="/usr/bin/doveadm"
MAIL_USER="user@example.org"
MAIL_BOX="inbox"
#
E_FROM="$1"
E_SUBJ="$2"
E_SENT="${3}d"
#E_SENT="0d"
#
#$DOVEADM search -u $MAIL_USER mailbox $MAIL_BOX from "$E_FROM" subject "$E_SUBJ" sentbefore $E_SENT >> \
#    /tmp/doveadm_${NOW}.log
#
$DOVEADM expunge -u $MAIL_USER mailbox $MAIL_BOX from "$E_FROM" subject "$E_SUBJ" sentbefore $E_SENT
#
exit

Produces a count of email in the /tmp directory

countemail
#!/bin/bash
#
NOW=$(date +"%Y-%m-%d-%H-%M")
DOVEADM="/usr/bin/doveadm"
MAIL_USER="user@example.org"
MAIL_BOX="inbox"
#
#E_FROM="$1"
#E_SUBJ="$2"
#E_SENT="${3}d"
#E_SENT="0d"
#
#echo "$E_FROM --- $E_SUBJ --- $E_SENT"
#echo "$DOVEADM search -u $MAIL_USER mailbox $MAIL_BOX from '$E_FROM' subject '$E_SUBJ' sentbefore $E_SENT"
#$DOVEADM search -u $MAIL_USER mailbox $MAIL_BOX from "$E_FROM" subject "$E_SUBJ" sentbefore $E_SENT > /tmp/doveadm_${NOW}.log
#
ECOUNT=`$DOVEADM search -u $MAIL_USER mailbox $MAIL_BOX | wc -l`
echo "Email count: $ECOUNT"  >> /tmp/doveadm_${NOW}.log
#
exit

Fetch specific emails

This script can be used to display the subject for all emails sent to a specific user and is unseen (unread).

#!/bin/bash
#
NOW=$(date +"%Y-%m-%d-%H-%M")
DOVEADM="/usr/bin/doveadm"
MAIL_USER="user@example.org"
MAIL_BOX="inbox"
#
E_TO="someone@example.org"
 
#
$DOVEADM search -u $MAIL_USER mailbox $MAIL_BOX TO $E_TO UNSEEN |
while read guid uid; do
    $DOVEADM fetch -u $MAIL_USER hdr.subject mailbox-guid $guid uid $uid
done
#
exit

QR Code
QR Code tech:linux:doveadm_scripting (generated for current page)