Notmuch logo

Approaches to initial tagging of messages

This page collects scripts and strategies for organizing mail using notmuch and doing automated initial tagging.

The [new] config section allows you to control which tags new messages receive. By default, notmuch config will use the tags inbox and unread.

If maildir.synchronize_flags is true (which is the default), Maildir flags have precedence over the initial tags. Thus an already read mail gets its initial unread tag correctly removed.

The new tag approach

Here's another very general and ad-hoc approach to initial message tagging, which sets all new messages to get the new tag:


After running notmuch new, all new messages will be marked new. You can then do various tag post-processing by just acting on messages with that tag. For instance, a post-processing script might do the following:

# immediately archive all messages from "me"
notmuch tag -new -- tag:new and

# delete all messages from a spammer:
notmuch tag +deleted -- tag:new and

# tag all message from notmuch mailing list
notmuch tag +notmuch -- tag:new and

# finally, retag all "new" messages "inbox" and "unread"
notmuch tag +inbox +unread -new -- tag:new

Note that the command above will mark a new but already-read mail as unread.

Since the post-processing is only acting on a few messages, it is generally extremely fast.

You can use the post-new hook, which is automatically run after notmuch new, to do post-processing. See man notmuch-hooks for details on hooks.

Tagging based on content

Since notmuch currently does not index arbitrary headers, it can be useful to tag based on content. Here is a snippet that would fit with the 'new' tag approach discussed above.

for mid in $(notmuch search --output=messages tag:new); do
    if notmuch show --format=raw "$mid" 2>/dev/null | awk '!NF{exit 1} /^X-Spam_bar: \+\+\+\+\+\+\+\+/ {exit 0}'; then
    notmuch tag +spam "$mid"

Other solutions

Notmuch MDA -- notmuch-insert

The notmuch insert command is a tool for delivering emails to maildir, indexing them to the Notmuch database, and tagging them as desired.