As we have seen, we can do all of the mail retrieval outside of
Emacs/mu4e. However, you can also do it from within
mu4e.
To set up mail-retrieval from within mu4e, set the variable
mu4e-get-mail-command to a shell command you want to use for retrieving
mail. It can also be a function which returns such a shell-command.
You can then get your e-mail using M-x mu4e-update-mail-and-index, or
C-S-u in all mu4e-views; alternatively, you can use C-c C-u,
which may be more convenient if you use emacs in a terminal.
You can kill the (foreground) update process with q.
It is possible to update your mail and index periodically in the
background or foreground, by setting the variable
mu4e-update-interval to the number of seconds between these
updates. If set to nil, it won’t update at all. After you make
changes to mu4e-update-interval, mu4e must be restarted
before the changes take effect. By default, this will run in
background and to change it to run in foreground, set
mu4e-index-update-in-background to nil.
After updating has completed, mu4e keeps the output in a buffer
*mu4e-last-update*, which you can use for diagnosis if needed.
If the mail-retrieval process returns with a non-zero exit code,
mu4e shows a warning (unless mu4e-index-update-error-warning
is set to nil), but then try to index your maildirs anyway
(unless mu4e-index-update-error-continue is set to nil).
Reason for these defaults is that some of the mail-retrieval programs may return
non-zero, even when the updating process succeeded; however, it is hard to tell
such pseudo-errors from real ones like login failed.
If you need more refinement, it may be useful to wrap the mail-retrieval
program in a shell-script, for example fetchmail returns 1 to
indicate ‘no mail’; we can handle that with:
(setq mu4e-get-mail-command "fetchmail -v || [ $? -eq 1 ]")
A similar approach can be used with other mail retrieval programs, although not all of them have their exit codes documented.
The value of mu4e-get-mail-command can also be a function that
returns a shell command. This allows for more sophistication. For
instance, you may want to attempt to retrieve mail only when online.
Let us look at an example.
On many GNU/Linux systems the online-status an be checked using Network
Manager’s nm-online, which returns non-zero when you are online.
Thus, we could use something like:
(setq mu4e-get-mail-command
(lambda ()
(if (zerop (shell-command "nm-online -q"))
"mbsync -a"
"true")))
Of course, the can be customized to the particulars of your system.
Not everyone needs to run a shell command for getting mail. For example, you may be running your own mail-server, or you already have some other way to periodically retrieve messages.
In that case, you can leave mu4e-get-mail-command at "true" (the
default), in which case mu4e does not attempt to retrieve new mail, but
still re-indexes your messages as per mu4e-update-interval.
If you have a large number of e-mail messages in your store, (re)indexing might take a while. The defaults for indexing are to ensure that we always have correct, up-to-date information about your messages, even if other programs have modified the Maildir.
The downside of this thoroughness is that it is relatively slow, something that can be especially noticeable with large e-mail corpora on slow file-systems. For a faster approach, you can use the following:
(setq mu4e-index-cleanup nil ;; don't do a full cleanup check mu4e-index-lazy-check t) ;; don't consider up-to-date dirs
In many cases, the mentioned thoroughness might not be needed, and
these settings give a very significant speed-up. If it does not work
for you (e.g., mu4e fails to find some new messages), simply leave
at the default.
Note that you can occasionally run a thorough indexing round using
mu4e-update-index-nonlazy.
For further details, please refer to the mu-index manpage; in
particular, see .noindex and .noupdate which can help reducing
the indexing time.
A simple setup could look something like:
(setq mu4e-get-mail-command "offlineimap" ;; or fetchmail, or ... mu4e-update-interval 300) ;; update every 5 minutes
To influence the details, a hook mu4e-update-pre-hook is available, which
is called right before starting the process. It is also possible to set
mu4e-get-mail-command to a function which returns a command.
It is possible to get notifications when the indexing process does any
updates — for example when receiving new mail. See
mu4e-index-updated-hook and some tips on its usage in the
FAQ — Frequently Asked Questions.