]> git.cworth.org Git - notmuch-wiki/blob - howto.mdwn
update howto regarding maildir flag sync
[notmuch-wiki] / howto.mdwn
1 # How to...
2
3 Some tips about how to do some useful things with notmuch, and the
4 various "third party" notmuch utilities.
5
6 [[!toc levels=2]]
7
8 ## <span id="receive_mail">**Receive mail**</span>
9
10 Notmuch does not fetch mail for you.  For that, you need to use an
11 external mail syncing utility.  Some recommended utilities are listed
12 below.
13
14 Notmuch requires that every individual message be in it's own file.
15 The well-supported [maildir](http://cr.yp.to/proto/maildir.html) or
16 "mh"-style storage formats are compatible with notmuch.  Basically any
17 setup in which each mail is in a file of its own will work.  The older
18 mbox mail store formats is not supported, but fortunately it is very
19 easy to [[convert mbox to maildir|howto/#mbox]] .  The following
20 utilities support these formats:
21
22 * [offlineimap](https://github.com/nicolas33/offlineimap/) -
23   quite useful and widely tested, it also offers a handy hook that
24   will come in useful a bit later in our setup.  Also supports
25   "presynchook" and "postsynchook" command that will get run
26   whenever you sync.  Point _postsynchook_ to a script that gets run
27   on every sync and that will do the automatic updating and tagging
28   of your notmuch database.
29
30 * [mbsync](http://isync.sourceforge.net/)
31
32 * [getmail](http://pyropus.ca/software/getmail/)
33
34 * [fetchmail](http://fetchmail.berlios.de/)
35
36 See the [[initial_tagging]] page for more info on initial tagging of
37 messages.
38
39 ## <span id="sync_maildir_flags">**Sync notmuch tags and maildir flags**</span>
40
41 notmuch has the ability to synchronize maildir flags and respective tags in both
42 directions. For more information on maildir flags see the [maildir
43 page](http://cr.yp.to/proto/maildir.html), and for the respective tags see your
44 notmuch configuration file. This feature is enabled by default, but if you don't
45 need it, it is simple to disable it with the 'notmuch config' command:
46
47         $ notmuch config set maildir.synchronize_flags false
48
49 The maildir flags may, in turn, be synchronized with IMAP flags by another tool,
50 such as offlineimap.
51
52 For safety reasons, and because
53 [[notmuch does not support delete operations|deleting]], notmuch does
54 not sync the "trashed" flag.  For discussion on this topic please
55 refer to the mailing list.
56
57 ## <span id="python">**Use notmuch from python**</span>
58
59 Notmuch includes python bindings to the notmuch shared library. Please
60 refer to the nice and extensive
61 [notmuch python API documentation](http://notmuch.readthedocs.org/).
62
63 The bindings are very simple to use.  As an example, given you have
64 the python bindings installed (or simply set your PYTHONPATH
65 environment variable to point to the .../bindings/python directory),
66 this snippet will produce a list of mails matching the given
67 expression:
68
69         >>> import notmuch
70         >>> db = notmuch.Database()
71         >>> query = db.create_query('tag:inbox AND NOT tag:killed')
72         >>> list(query.search_messages()) # doctest:+ELLIPSIS
73         [...]
74
75 ## <span id="print_filenames">**Using notmuch with Mutt**</span>
76
77 Notmuch is a great mail indexing tool that can also be used *in conjunction*
78 with existing Mail User Agents (MUA) instead of replacing them. The advantage
79 of such mixed solutions is that users can benefit from notmuch features (such
80 as full-text search and thread reconstruction) without *having to* change
81 MUA.
82
83 A popular geek MUA is [the Mutt e-mail client](http://www.mutt.org);
84 integrating notmuch with Mutt is not seamless, but fairly straightforward.
85 There are two principal possibilities, each with its own ups and downs:
86
87 * Using mutt-notmuch, which will create a "virtual" maildir folder with
88   search results whenever a search is made. The upside is that you can
89   search all your folders simultanously; the downside is that your
90   modifications in the results listing do not carry over, also having
91   to switch folders comes with some more annoyances.
92
93   An
94   [**how to use Notmuch with Mutt**](http://upsilon.cc/~zack/blog/posts/2011/01/how_to_use_Notmuch_with_Mutt/)
95   has been written by Stefano Zacchiroli. Check out the
96   [howto](http://upsilon.cc/~zack/blog/posts/2011/01/how_to_use_Notmuch_with_Mutt/)
97   for more information.
98
99   <small>(Note by the howto author: I've linked the howto from this wiki rather
100   than splicing it in, in order to avoid duplication of information. If you
101   think it would be better to have it here, feel free to copy the text
102   here. The howto is
103   [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)
104   in markdown syntax from the Git repository of my homepage.)</small>
105
106 * Using a simple macro that will emulate the "limit" mutt functionality
107   using notmuch.
108
109   See the [**alternative notmuch integration**](http://log.or.cz/?p=228)
110   blog post for instructions and details,
111   or simply put these two macros to your muttrc:
112
113         # 'L' performs a notmuch query, showing only the results
114         macro index L "<enter-command>unset wait_key<enter><shell-escape>read -p 'notmuch query: ' x; echo \$x >~/.cache/mutt_terms<enter><limit>~i \"\`notmuch search --output=messages \$(cat ~/.cache/mutt_terms) | head -n 600 | perl -le '@a=<>;chomp@a;s/\^id:// for@a;$,=\"|\";print@a'\`\"<enter>" "show only messages matching a notmuch pattern"
115         # 'a' shows all messages again (supersedes default <alias> binding)
116         macro index a "<limit>all\n" "show all messages (undo limit)"
117
118   The upside (if you are used to working in the context of a single folder)
119   is that this really does use the limit functionality of mutt
120   and you are still in your original folder.
121   The downside is that this approach has scaling problems
122   and works well only for reasonably specific queries.
123
124
125 ## <span id="reapply_auto">**Automatically retagging the database (e.g., when upgrading versions)**</span>
126
127 Certain versions of notmuch include new automatic tags (for example, between
128 0.3 and 0.10, automatic tagging of signed and encrypted messages was added).
129 However, for users running with databases created in older versions of
130 notmuch, these tags are missing in pre-existing messages and need to be
131 added.  One way to do this is as follows:
132
133         $ notmuch dump ~/out.nm
134         $ mv ~/Mail/.notmuch ~/.notmuch.bak
135         $ notmuch new
136         $ notmuch tag -inbox -unread '*'
137         $ notmuch restore --accumulate ~/out.nm
138
139 At this point, one should run a sanity check on the tags, and if everything
140 has merged correctly, the ~/.notmuch.bak directory is expendable, as is
141 ~/out.nm.
142
143 ## <span id="mbox">**Dealing with mbox and other formats**</span>
144
145 notmuch by itself is unable to handle non-maildir mail archives.  One tool
146 to solve this is called mb2md.  Assuming an mbox in ~/test.mbox and ones
147 mail archives to be in ~/Mail, an invocation would look like
148
149         $ mb2md -s ~/test.mbox -d ~/Mail/mynewmaildirname
150
151 Note that specifying the paths for -s and -d is necessary.  This will create
152 a new maildir in ~/Mail/mynewmaildirname from the mbox at ~/test.mbox.
153
154 Often the formats are more convoluted, however.  Many lists provide an
155 almost-but-not-quite-mbox format that mailman produces, as can be seen, for
156 example, [here](http://lists.xapian.org/pipermail/xapian-devel/).  These
157 files can be converted with some degree of success to mbox using the script
158 found
159 [here](http://www.hermann-uwe.de/blog/converting-mailman-gzipd-text-archive-files-to-proper-mbox-files),
160 and from mbox to maildir as above.
161
162 However, many of these lists also have a gmane version, which, where it
163 exists, achieves far better results than dealing with the messy mailman
164 output.  Using the instructions from [Gmane's
165 site](http://gmane.org/export.php), we can download an mbox file, which we
166 can then convert to maildir using mb2md or other utility.
167
168 ## <span id="special_tags">**Take advantage of tags that are special to notmuch**</span>
169
170 See [[tags special to notmuch|special-tags]].