# How to... Some tips about how to do some useful things with notmuch, and the various "third party" notmuch utilities. [[!toc levels=2]] ## **Debian squeeze packages outdated** The notmuch package(s) in Debian squeeze are very old (version 0.3). It's better to start off with an up to date version from [backports](http://backports-master.debian.org/). ## **Receive mail** Notmuch does not fetch mail for you. For that, you need to use an external mail syncing utility. Some recommended utilities are listed below. Notmuch requires that every individual message be in it's own file. The well-supported [maildir](http://cr.yp.to/proto/maildir.html) or "mh"-style storage formats are compatible with notmuch. Basically any setup in which each mail is in a file of its own will work. The older mbox mail store formats is not supported, but fortunately it is very easy to [[convert mbox to maildir|howto/#mbox]] . The following utilities support these formats: * [offlineimap](http://offlineimap.org/) - quite useful and widely tested, it also offers a handy hook that will come in useful a bit later in our setup. Also supports "presynchook" and "postsynchook" command that will get run whenever you sync. Point _postsynchook_ to a script that gets run on every sync and that will do the automatic updating and tagging of your notmuch database. * [mbsync](http://isync.sourceforge.net/) * [getmail](http://pyropus.ca/software/getmail/) * [fetchmail](http://fetchmail.berlios.de/) See the [[initial_tagging]] page for more info on initial tagging of messages. ## **Sync notmuch tags and maildir flags** notmuch has the ability to synchronize maildir flags and respective tags in both directions. For more information on maildir flags see the [maildir page](http://cr.yp.to/proto/maildir.html), and for the respective tags see your notmuch configuration file. This feature is enabled by default, but if you don't need it, it is simple to disable it with the 'notmuch config' command: $ notmuch config set maildir.synchronize_flags false The maildir flags may, in turn, be synchronized with IMAP flags by another tool, such as offlineimap. For safety reasons, and because [[notmuch does not support delete operations|deleting]], notmuch does not sync the "trashed" flag. For discussion on this topic please refer to the mailing list. ## **Use notmuch from python** Notmuch includes python bindings to the notmuch shared library. Please refer to the nice and extensive [notmuch python API documentation](http://notmuch.readthedocs.org/). The bindings are very simple to use. As an example, given you have the python bindings installed (or simply set your PYTHONPATH environment variable to point to the .../bindings/python directory), this snippet will produce a list of mails matching the given expression: >>> import notmuch >>> db = notmuch.Database() >>> query = db.create_query('tag:inbox AND NOT tag:killed') >>> list(query.search_messages()) # doctest:+ELLIPSIS [...] ## **Using notmuch with Mutt** Notmuch is a great mail indexing tool that can also be used *in conjunction* with existing Mail User Agents (MUA) instead of replacing them. The advantage of such mixed solutions is that users can benefit from notmuch features (such as full-text search and thread reconstruction) without *having to* change MUA. A popular geek MUA is [the Mutt e-mail client](http://www.mutt.org); integrating notmuch with Mutt is not seamless, but fairly straightforward. There are two principal possibilities, each with its own ups and downs: * Using mutt-notmuch, which will create a "virtual" maildir folder with search results whenever a search is made. The upside is that you can search all your folders simultanously; the downside is that your modifications in the results listing do not carry over, also having to switch folders comes with some more annoyances. An [**how to use Notmuch with Mutt**](http://upsilon.cc/~zack/blog/posts/2011/01/how_to_use_Notmuch_with_Mutt/) has been written by Stefano Zacchiroli. Check out the [howto](http://upsilon.cc/~zack/blog/posts/2011/01/how_to_use_Notmuch_with_Mutt/) for more information. (Note by the howto author: I've linked the howto from this wiki rather than splicing it in, in order to avoid duplication of information. If you think it would be better to have it here, feel free to copy the text here. The howto is [available](http://git.upsilon.cc/cgi-bin/gitweb.cgi?p=zack-homepage.git;a=history;f=blog/posts/2011/01/how_to_use_Notmuch_with_Mutt.mdwn) in markdown syntax from the Git repository of my homepage.) * Using a simple macro that will emulate the "limit" mutt functionality using notmuch. See the [**alternative notmuch integration**](http://log.or.cz/?p=228) blog post for instructions and details, or simply put these two macros to your muttrc: # 'L' performs a notmuch query, showing only the results macro index L "unset wait_keyread -p 'notmuch query: ' x; echo \$x >~/.cache/mutt_terms~i \"\`notmuch search --output=messages \$(cat ~/.cache/mutt_terms) | head -n 600 | perl -le '@a=<>;chomp@a;s/\^id:// for@a;$,=\"|\";print@a'\`\"" "show only messages matching a notmuch pattern" # 'a' shows all messages again (supersedes default binding) macro index a "all\n" "show all messages (undo limit)" The upside (if you are used to working in the context of a single folder) is that this really does use the limit functionality of mutt and you are still in your original folder. The downside is that this approach has scaling problems and works well only for reasonably specific queries. ## **Automatically retagging the database (e.g., when upgrading versions)** Certain versions of notmuch include new automatic tags (for example, between 0.3 and 0.10, automatic tagging of signed and encrypted messages was added). However, for users running with databases created in older versions of notmuch, these tags are missing in pre-existing messages and need to be added. One way to do this is as follows: $ notmuch dump --output=~/out.nm $ mv ~/Mail/.notmuch ~/.notmuch.bak $ notmuch new $ notmuch tag -inbox -unread '*' $ notmuch restore --accumulate --input=~/out.nm At this point, one should run a sanity check on the tags, and if everything has merged correctly, the ~/.notmuch.bak directory is expendable, as is ~/out.nm. ## **Dealing with mbox and other formats** notmuch by itself is unable to handle non-maildir mail archives. One tool to solve this is called mb2md. Assuming an mbox in ~/test.mbox and ones mail archives to be in ~/Mail, an invocation would look like $ mb2md -s ~/test.mbox -d ~/Mail/mynewmaildirname Note that specifying the paths for -s and -d is necessary. This will create a new maildir in ~/Mail/mynewmaildirname from the mbox at ~/test.mbox. Often the formats are more convoluted, however. Many lists provide an almost-but-not-quite-mbox format that mailman produces, as can be seen, for example, [here](http://lists.xapian.org/pipermail/xapian-devel/). These files can be converted with some degree of success to mbox using the script found [here](http://www.hermann-uwe.de/blog/converting-mailman-gzipd-text-archive-files-to-proper-mbox-files), and from mbox to maildir as above. However, many of these lists also have a gmane version, which, where it exists, achieves far better results than dealing with the messy mailman output. Using the instructions from [Gmane's site](http://gmane.org/export.php), we can download an mbox file, which we can then convert to maildir using mb2md or other utility. ## **Take advantage of tags that are special to notmuch** See [[tags special to notmuch|special-tags]].