summaryrefslogtreecommitdiffstats
path: root/resend-mails.sh
blob: c463c0eeaa3823263800f5ebe18bcd1859603f20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash

set -e
shopt -s nullglob

resend_mails() {
        target_dir="$1"; shift
        date=$(date +%s)
        searchquery=(all)

        for user in "$@"; do
                mailcount=$(doveadm search -u "$user" "${searchquery[@]}" | wc -l)
                if ((mailcount > 0)); then
                        echo "dumping $mailcount mails from $user ..." >&2
                        doveadm search -u "$user" "${searchquery[@]}" | while read guid uid; do
                                doveadm fetch -u "$user" text mailbox-guid "$guid" uid "$uid" | sed '/^text:$/d; s#^\f$##' | grep -v "^Delivered-To: " | sendmail -oi -f 'root@archlinux.org' "$user@archlinux.org" #> "$target_dir/$user-$date-$uid.msg"
                                doveadm expunge -u "$user" mailbox-guid "$guid" uid "$uid"
                        done
                fi
        done
}


resend_mails "UNUSED" userfoo userbar