Sorting sent email

Created: 2014-11-01 — modified: 2016-06-16 — tags: e-mail

How to automate it if you host your mail yourself

There are numerous ways of sorting incoming mail into folders (personally I use procmail), but not enough ways of sorting sent mail. SquirrelMail has Per Recipient Sent Folders plugin, but that's sorting on the client-side. Most email clients have an option to save sent mail to "Sent" folder, but quite few of them can save it to other folders.

This script tries to solve this sorting problem. It runs on the server and monitors the "Sent" folder for new files (using inotifywait). When it finds one, it extracts an email address from its "To" header (using mu view, but you can also use grep, but be careful if the header wraps in two lines) and uses it to move file to an appropriate folder.

Note that you need to know some shell scripting to adjust this script to suite your taste. expr "$to" lines are used to match only a part of email address (using RegExp), while email list inside the case "$to" is used when you want to match a full email address.

Also note that rules at the bottom of the script have priority (override) over those above them.


cd Maildir/.Sent/cur/

inotifywait --quiet --monitor --format '%f' --event moved_to . | while read line; do
    to="$(mu view --summary-len=1 "$line" | sed -n '/To:/{s/.*[ <]\(.*@[^ >]*\).*/\1/;p}')"
    # sorting begins here
    case "$to" in
        # compare whole addresses
        ("") dest='.friend' ;;
    # compare partial addresses
    expr "$to" : ".*" >/dev/null && dest='.work'
    [ "$dest" ] && mv "$line" "../../$dest/cur/"