2

Let's assume that I make a notmuch query that returns files:

$ notmuch search --output=files tag:inbox from:love

This returns a list of files, pointing to Maildir messages. Now I pick one of these files (already in the notmuch database), for instance with

FILENAME=$(notmuch search --output=files tag:inbox from:love | fzf)

and I would like to get its message-id and thread-id in the notmuch database. From the variable $FILENAME, I would like to find the message-id in notmuch.

A very sloppy way to do this is to parse the file, read the headers from/subject/date and make a notmuch query notmuch search from:{...} subject:{...} date:{..}. But since the filenames are already stored in the database, I guess there should be a canonical and robust way to obtain the mesage-id from the filename.

Thanks!

0

I didn't find any way of searching notmuch database based on maildir filename in NOTMUCH-SEARCH-TERMS(7)

You can get Message-Id and notmuch thread id directly from Your search by iterating on Message-Ids:

for message_id in $(notmuch search --output=messages 'tag:inbox from:love')
do
  thread_id=$(notmuch search --output=threads $message_id)
  echo "$thread_id - $message_id"
done

Or you can iterate on threads and get associated Message-Ids:

for thread_id in $(notmuch search --output=threads 'tag:inbox from:love')
do
  # sed is here only to provide the output in the same format as in the first example
  notmuch search --output=messages $thread_id | sed "s/^/$thread_id - /"
done

Whatever suits your needs better. Both for loops prints result in following format:

thread:THREAD_ID - id:MESSAGE_ID
…

If you want to get From, Date, Subject headers You can also extract it directly from notmuch database using jq without need to parse maildir file using formail(1) or similar tool.

notmuch search --format=json id:MESSAGE_ID | jq -r '.[].subject'
2
  • I modified the question to make clear that I want to retrieve the message-id from a single $FILENAME. The goal is not to list the message-id and thread-id in the query.
    – jlewk
    Aug 25 '20 at 21:04
  • np. I'm glad you've found the solution on your own. Aug 26 '20 at 7:25
0

I eventually found a way through the notmuch python bindings, see https://notmuch.readthedocs.io/projects/notmuch-python/en/latest/database.html?highlight=filename#notmuch.Database.find_message_by_filename

A working bash one liner is

threadId=$(python3 -c "import notmuch; db = notmuch.Database(); print(db.find_message_by_filename('$FILENAME').get_thread_id())");

and the unpacked python3 code is

import notmuch
db = notmuch.Database()
msg = db.find_message_by_filename('filename of the maildir message')
msg.get_thread_id()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.