emacs.d/emacs_mu4e.org

64 KiB
Raw Blame History

Collection of interesting articles related to emacs and email.

Manage o365 mail with emacs, mbsync, and mu4e | Unl0ckd   website

[2021-12-05 Sun 21:45]

Article

One of these days, this blog will be used for more than just notes to myself again.

Why?

For the past year, I have been enraptured with Emacs. I've embraced the idea of extending it into as many facets of my workflow as possible. This post details how I was able to get my work email setup in mu4e for easy task creation via org-mode.

For several years, I've been using my inbox as a todo list, filing email into a complex folder hierarchy. Once I discovered org-mode, I realized that I should use email as an interface for correspondence only. If a message came in that I should act on at some future point, it should be captured in my org todo and then discarded. I believe this follows in the principles of GTD.

I'm using:

mbsync

mbsync is a mailbox synchronizer that retrieves messages via IMAP from a remote mailstore and saves them as flat files locally.

mbsync Configuration

mbsync configuration is performed in ~/.mbsyncrc (and in fact requires this file to run). Here's my ~/.mbsyncrc:

 IMAPAccount work
Host outlook.office365.com
User  [email protected]
PassCmd "security find-generic-password -s NoMAD -w"
SSLType IMAPS
SSLVersion TLSv1.2
AuthMechs PLAIN
# Increase timeout to avoid o365 IMAP hiccups
Timeout 120
PipelineDepth 50

IMAPStore work-remote
Account work

MaildirStore work-local
# Note the trailing slash on the Path statement!
Path ~/.mail/work/
Inbox ~/.mail/work/Inbox
SubFolders Legacy

Channel work
Master :work-remote:
Slave :work-local:
#Include everything
Patterns *
# Sync changes (creations/deletions) with the server
Create Both
Expunge Both
Sync All

Verify mbsync is working correctly with $ mbsync work. This will pull down work mail to ~/.mail/work/ with a folder layout mimicking Exchange's mail folder structure.

Some items to note:

  • Create Both and Expunge Both means mbsync can delete messages on your mailserver. If you want to try this configuration out in read-only mode, set these values to Create Slave and Expunge Slave instead.
  • The trailing slash on the local MaildirStore path statement is critical!
  • My experience with Exchange 365 has been chaotic. I've set a Timeout 120 value to try to ensure there are no sync hiccups. This value has proved useful to me, but you can change it or remove it as you see fit.
  • mbsync will not delete mail folders on the server. Before you use this tool, it might be wise to ensure your Exchange folder hierarchy is as flat as possible. This can be done using the Outlook or OWA client.
  • PassCmd allows you to retrieve credentials from a CLI password manager tool

Now email can be synced and retrieved from the mailserver.

mu4e

mu is a command-line mail client that provides superior mail search capabilities. Installing this package will automatically pull down mu4e (mu 4 Emacs) as well.

mu4e Prerequisites

On macOS, install mu (which includes mu4e) and mbsync. Note that mbsync is part of the isync homebrew collection. These are both installed in the terminal using homebrew:

 brew install mu
brew install isync

Installing from homebrew should place the required files in /usr/local/share/emacs/site-lisp/mu/mu4e that will be loaded in the configuration below.

Run mu

Before using mu4e it's a good idea to verify that mu works as expected, after all, mu4e uses mu as its engine.

To validate, mu must first create a mail index. Run:

$ mu index --maildir=~/.mail/work

Once this completes, give mu a spin:

$ mu find timecard

$ mu find from:myboss

At this point, mail is synced, indexed, and searchable from Exchange.

Doom Emacs

I am thoroughly impresed with Doom Emacs, and use it as my base. Configuring this distribution is slightly different from configuring mu4e in vanilla Emacs.

First, security:

Securely Store SMTP Credentials

SMTP is used to transfer outbound messages. I store my o365 creds in a gpg-encrypted file, ~/.emacs.d/.authinfo.gpg

Create authinfo file

Enter the credentials for the SMTP server in ~/.authinfo using the format:

machine mail.example.com login myusername port 587 password mypassword

Use quotes to contain the password, for instance:

machine smtp.office365.com login [email protected] password "mypassword" port 587

Encrypt authinfo file

Use gpg to encrypt the authinfo file. (macOS users, install https://gpgtools.org/. This will place a symlink to the gpg CLI tool in your /usr/local/bin so make sure that's in your shell's $PATH.

I won't cover the process of creating a keypair in this article, but you can find more information here and here.

Find the gpg key you want to encrypt this file with using $ gpg --list-keys:

 ----------------------------------
...
pub   rsa4096 2019-01-22 [SC] [expires: 2023-01-22]
      315998993D8B8B1BA4AD5D209332E13A9A79C3D5
uid           [ultimate] Chris Lockard < [email protected]>
sub   rsa4096 2019-01-22 [E] [expires: 2023-01-22]
sub   rsa4096 2019-09-09 [S] [expires: 2023-09-08]
sub   rsa4096 2019-09-09 [E] [expires: 2023-09-08]
sub   rsa4096 2019-09-09 [A] [expires: 2023-09-08]

Now encrypt ~/.authinfo using the following:

$ gpg -se ~/.authinfo

This prompts for the key to use, so either enter = [email protected]= or the key signature - A9A79C3D5. The output of this program is an encrypted file, ~/.authinfo.gpg. For added security, set the permissions on this file to chmod 600 ~/.authinfo.gpg.

Finally, move this file with mv .authinfo.gpg ~/.emacs.d and cleanup the file containing cleartext credentials with rm .authinfo. Emacs will automatically know to look for ~/.emacs.d/authinfo.gpg which will help later when configuring SMTP.

Doom Emacs Configuration

I store my Doom configuration files in my github and link them thusly:

ln -s ~/Documents/dotfiles/Emacs/.doom.d ~/.doom.d

Doom defines packages in ~/.doom.d/init.el with user configuration in ~/.doom.d/config.el (or config.org for literate config-ers like me :))

~/.doom.d/init.el

As mu4e is a package only available on the local filesystem, Doom needs to know from where to load it. The following line is added at the top of the file:

 ;; enabled and in what order they will be loaded. Remember to run 'doom refresh'
;; after modifying it.
;;
;; More information about these modules (and what flags they support) can be
;; found in modules/README.org.

;; This is needed because emacs won't pick up mu4e otherwise:
(add-to-list 'load-path "/usr/local/share/emacs/site-lisp/mu/mu4e/")
(doom! :input

       :completion
...

This mu4e path is where Homebrew installed mu4e (from the mu package) by default on macOS Mojave.

Further down init.el, uncomment mu4e from the :email block:

 ...
       :email
       mu4e       ; WIP
...
~/.doom.d/config.org{el}

mu4e configuration is placed in config.org or config.el. Mine looks like this:

 (after! mu4e
  (setq! mu4e-maildir (expand-file-name "~/.mail/work") ; the rest of the mu4e folders are RELATIVE to this one
         mu4e-get-mail-command "mbsync -a"
         mu4e-index-update-in-background t
         mu4e-compose-signature-auto-include t
         mu4e-use-fancy-chars t
         mu4e-view-show-addresses t
         mu4e-view-show-images t
         mu4e-compose-format-flowed t
         ;mu4e-compose-in-new-frame t
         mu4e-change-filenames-when-moving t ;; http://pragmaticemacs.com/emacs/fixing-duplicate-uid-errors-when-using-mbsync-and-mu4e/
         mu4e-maildir-shortcuts
         '( ("/Inbox" . ?i)
            ("/Archive" . ?a)
            ("/Drafts" . ?d)
            ("/Deleted Items" . ?t)
            ("/Sent Items" . ?s))

         ;; Message Formatting and sending
         message-send-mail-function 'smtpmail-send-it
         message-signature-file "~/Documents/dotfiles/Emacs/.doom.d/.mailsignature"
         message-citation-line-format "On %a %d %b %Y at %R, %f wrote:\n"
         message-citation-line-function 'message-insert-formatted-citation-line
         message-kill-buffer-on-exit t

         ;; Org mu4e
         org-mu4e-convert-to-html t
         ))
(set-email-account! " [email protected]"
                    '((user-mail-address      . " [email protected]")
                      (user-full-name         . "Chris Lockard")
                      (smtpmail-smtp-server   . "smtp.office365.com")
                      (smtpmail-smtp-service  . 587)
                      (smtpmail-stream-type   . starttls)
                      (smtpmail-debug-info    . t)
                      (mu4e-drafts-folder     . "/Drafts")
                      (mu4e-refile-folder     . "/Archive")
                      (mu4e-sent-folder       . "/Sent Items")
                      (mu4e-trash-folder      . "/Deleted Items")
                      (mu4e-update-interval   . 1800)
                      ;(mu4e-sent-messages-behavior . 'delete)
                      )
                    nil)
Usage

Everything is in place to use Doom Emacs as a mail client!

Start Emacs and run M-x mu4e:

/images/2019/11-14-1.png

Compose a message

Press C to bring up the message composition window:

/images/2019/11-14-2.png

To send a message, place the cursor in the header section ( gg =) and then =SPC m s. You'll be prompted to enter the passphrase for your gpg key (sometimes twice) and then your message will send once Emacs decrypts your ~/.emacs.d/authinfo.gpg to retrieve SMTP credentials.

Reply to a message

From the inbox view, press R to reply to a message. Fill out your response, then send the message by again placing point in the message header section ( gg =) and then =SPC m s.

Capture a message as a task in orgmode

From the inbox view, select a message with = to open the message view. With point in the header section (it should be there by default) and press =SPC X or M-x org-capture. With an appropriate capture template, this message will be linked into org mode for use in your GTD workflow.

Don't manage work email with Emacs ·   website

[2021-12-05 Sun 21:44]

Article

I do a lot of work in Emacs and at the command line, and I get quite a few emails so it would be great if I could handle my emails there too. Email in Emacs can be surprisingly featureful and handles HTML markup, images and can even send org markup with images and equations all from the comfort of an Emacs buffer. However it can be a whole heap of work, and as you get deeper into the features your mail client provides the amount of custom integration required grows very rapidly. It's a good way to appreciate all the features of your current mail client, but you may be able to find a better use of your time.

Getting the basics of synchronising emails from an IMAP or Exchange server may take some time to setting up (and in some circumstances take a lot of time), but once they're working it will be pretty smooth. Dealing with HTML and images and attachments works pretty well out of the box, unless you get a lot of custom office drawings in your email. Building an address book of frequent contacts is a bit of a pain, but with some work is possible. Synchronising email addresses from the server can be difficult, and may need to be done in batches - but you might be able to manually. Getting calendar invites is possible with a bit of hacking, but seeing other people's calendars is very difficult. Finally if the server configuration is changed (like changing an authentication provider) you may have to spend a lot of time setting it up all over again.

The benefits are that it tends to be faster to get through emails (because they are on the local filesystem), you don't need to change environments to use them and you can use all your favourite CLI tools on them. But unless email is a very large part of your working life (and it seems to be slowly losing out to instant messaging clients) it's probably not worth the investment (unless you want to build a custom email automation tool one day!).

I'll share some of my experience doing this for those who are hard to discourage.

Operating Environment

If you want to set up email from the command line or Emacs you'll want to be working in a POSIXy environment, because that's where all the tooling is. If your working environment supports Linux or Mac computers then it's happy times. However if you're working in Windows you have a few options.

The best Windows option is Windows Subsystem for Linux - it lets you run a whole Linux environment and works pretty well with Windows. There are a couple of creaky edges, mainly the filesystem is slow (which should be fixed in WSL2), but overall it's the best solution if you can get the feature enabled.

If you can't then working in a Virtualbox VM for Linux is the next best option; and you can configure it to be fairly seamless. However you can't use it if there's any other virtualisation on your machine like Docker for Windows. In fact some organisations use security software that uses virtualisation making it impossible to install Virtualbox. But if you can get Virtualbox running (or your organisation supports another virtualisation product) then it's generally a good solution.

When all else fails there is good ol' Cygwin. It doesn't require any special permissions, so as long as you can run external applications on your computer it should work. It's a bit clunky, and you may need to build some utilities (like isync, see below) yourself, but with a bit of work you can get a usable environment. I've heard msys2 is better but have never taken the time to understand it.

Synchronising email

You now need a way to pull email to your local filesystem and push emails back out. For pushing emails Postfix works great (and has a sendmail interface) and I've never needed anything else. For pulling emails you can run a email server like Dovecot, but it's quite a bit of effort to set up. The easiest solution is to use isync/mbsync, or it's slower cousin offlineimap.

Both mbsync and offlineimap have gnarly configuration options that will make you learn quite a bit about the low level details of email authentication and Maildir. And if you set the wrong options you might accidentally delete your whole email; so spend a lot of time reading through them and try it out on a test account before you run it on your precious emails. To get authentication details for your email provider the easiest thing to do is to search the web, and if you have a common email provider (like Gmail, Office365, Fastmail) you'll likely find a blog with a sample configuration. If you've got an uncommon provider look in their documentation/settings for SMTP and IMAP; if the Auth method isn't clear it's best to try to set it up with Thunderbird first because it has some magic to automatically detect these settings and is more likely to work out of the box.

If your email provider doesn't have IMAP enabled then you're probably out of luck - unless it's an exchange server. If you can't get app passwords and need to use two factor authentication you may spend a lot of time trying to get this set up (and may have to implement the feature!).

If you're on an Exchange server or on Office365 but can't access IMAP then you can use the fantastic Davmail. Davmail also supports CalDAV and CardDAV for calendar and contacts (more on this later). The only issue is finding the Exchange server settings can take some sleuthing (or a beer with your local sysadmin). As before it's best to get it working in Thunderbird before trying another synchronising tool, because it's easiest to get working there.

Setting up a mail interface

So now you've got all your emails sitting in a maildir folder it'd be handy to have a tool for reading and writing email.

If you're a serious vim user mutt may be a good option. notmuch has a powerful tag system, but you have to manually sync it yourself between servers (and the Emacs mode has too many special screens for my liking). In Emacs gnus is built-in, but has a byzantine configuration system that you could spend the rest of your life tweaking (like this John Wiegley's). But gnus is apparently good if you're on a lot of mailing lists. However for me mu and it's Emacs counterpart mu4e work fantastically well - you just have to take the time to learn yet another query language (and if you use evil-mode there are mu4e keybindings in evil-collection).

This is generally pretty straightforward (especially if you can crib a configuration file that is close to what you need), but there are some traps like if you use mbsync and mu4e you need to set mu4e-change-filenames-when-moving to true or you'll get all sorts of errors when trying to sync. You can then spend a bunch of time configuring how HTML is rendered, storing links to emails in org-mode and sending email from org-mode.

Dealing with contacts

Now you can write email you may want to store the addresses of people you want to contact. Many email providers support the CardDav format and you can synchronise it locally with a tool like ASynK or vdirsyncer. You can then import them into org contacts with org-vcard and configure mu4e to use them for auto-completion. Or use them with BBDB for Emacs email clients that support them, or write a script to convert them to Mutt aliases.

One problem is if you work for an enterprise with thousands of people that's going to be a lot of email addresses, and the syncing or the interface may choke. You can probably get away with just manually copying the addresses of the people you email most often into the configuration of whatever tool you use. But once in a while you'll want to email someone in your organisation and you may have to fall back to another tool to get the address book.

Calendars and meeting invites

Calendars is something where Outlook groupware really shines. You can see everyone's calendar and schedule a meeting in free time using the scheduling assistant (and book meeting rooms!). While this can lead to the problem of calendar tetris where other people unilaterally fill the blanks in your Calendar, it's generally a useful organisational feature and can sometimes even be used to list and book available meeting rooms.

/images/outlook_scheduling_assistant.png

I haven't found anything that quite substitutes for it in an office environment. You could manage your calendar with a command line tool in khal or in Emacs with diary/calendar or org-agenda or calfw and synchronise it over iCal. You can probably even get meeting invites into your calendar and respond to the invitation ( mu4e supports this). But I doubt there's anything like the scheduling assistant and if you organise a lot of meetings in an Outlook office you'll be falling back to Outlook a lot.

If you get this far you can spend a lot more time smoothing out the rough edges. It's certainly possible to do, but worth thinking about whether it's really going to pay off for the time investment. But maybe you can walk the path and enjoy the journey as much as I did.

mbsync and Office 365 | kdecherf ~ %   website

[2021-12-05 Sun 21:43]

Article

kdecherf ~ %

data:image/svg+xml;base64,PHN2ZyBpZD0ibW9vbiIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdib3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgc3Ryb2tlPSJjdXJyZW50Q29sb3IiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIj4gPHBhdGggZD0iTTIxIDEyLjc5QTkgOSAwIDEgMSAxMS4yMSAzIDcgNyAwIDAgMCAyMSAxMi43OXoiPjwvcGF0aD48L3N2Zz4= data:image/svg+xml;base64,PHN2ZyBpZD0ic3VuIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld2JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiPiA8Y2lyY2xlIGN4PSIxMiIgY3k9IjEyIiByPSI1Ij48L2NpcmNsZT4gPGxpbmUgeDE9IjEyIiB5MT0iMSIgeDI9IjEyIiB5Mj0iMyI+PC9saW5lPiA8bGluZSB4MT0iMTIiIHkxPSIyMSIgeDI9IjEyIiB5Mj0iMjMiPjwvbGluZT4gPGxpbmUgeDE9IjQuMjIiIHkxPSI0LjIyIiB4Mj0iNS42NCIgeTI9IjUuNjQiPjwvbGluZT4gPGxpbmUgeDE9IjE4LjM2IiB5MT0iMTguMzYiIHgyPSIxOS43OCIgeTI9IjE5Ljc4Ij48L2xpbmU+IDxsaW5lIHgxPSIxIiB5MT0iMTIiIHgyPSIzIiB5Mj0iMTIiPjwvbGluZT4gPGxpbmUgeDE9IjIxIiB5MT0iMTIiIHgyPSIyMyIgeTI9IjEyIj48L2xpbmU+IDxsaW5lIHgxPSI0LjIyIiB5MT0iMTkuNzgiIHgyPSI1LjY0IiB5Mj0iMTguMzYiPjwvbGluZT4gPGxpbmUgeDE9IjE4LjM2IiB5MT0iNS42NCIgeDI9IjE5Ljc4IiB5Mj0iNC4yMiI+PC9saW5lPjwvc3ZnPg==

Home » Blog

mbsync and Office 365

May 1, 2017 · 1 min

I observed that offlineimap stops working correctly and starts seeing UID validity issues quite often when syncing my Office 365 account. Considering that a full folder resync is necessary to get rid of these issues I decided to give mbsync a try.

After making the configuration of the tool, which is pretty straightforward, I started it and… It failed with cryptic and random error messages like these:

 IMAP error: bogus FETCH response

and

 IMAP command 'UID FETCH x (BODY.PEEK[])' returned an error: UID
FETCH x (BODY.* y FETCH (BODY[] {z}

While trying to find any resource about these errors I found a note on the isync page of ArchLinux's Wiki. It says that Microsoft Exchange 2003 server is unable to handle concurrent IMAP commands, which is the default behavior of mbsync. You must add the following line to the mbsync configuration to disable this feature:

 PipelineDepth 1

It appears that this solution also solves the issue with Office 365.

Enjoy!

Content under license CC BY-NC-SA 3.0 Powered by Hugo & PaperMod

data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdib3g9IjAgMCAxMiA2IiBmaWxsPSJjdXJyZW50Q29sb3IiPiA8cGF0aCBkPSJNMTIgNkgwbDYtNnoiPjwvcGF0aD48L3N2Zz4=

Trying out mu4e with mbsync   website

[2021-12-05 Sun 21:42]

Article

Álvaro Ramírez

17 June 2018 Trying out mu4e with mbsync

The email fun in Emacs continues. After a few weeks since I started using mu4e and offlineimap, I'm sold. Both are awesome. Mbsync is an offlineimap alternative. Despite resyncing all my mail, the transition was fairly smooth. Here's how…

Install isync (for mbsync)
brew install isync
Configure mbsync

Mbsync uses ~/.mbsyncrc for configuration. Migrating ~/.offlineimaprc to ~/.mbsyncrc looks like:

IMAPAccount Personal
Host some.imap.host.com
User your_user_name
PassCmd  "gpg --quiet --batch -d ~/.offlineimap_accountname.gpg"
Port 993
SSLType IMAPS
AuthMechs Login
CertificateFile  ~/.offlineimapcerts.pem
 #  My IMAP provider doesn't handle concurrent IMAP commands.
PipelineDepth 1

IMAPStore Personal-remote
Account Personal

MaildirStore Personal-local
Path ~/IMAP/Personal/
Inbox ~/IMAP/Personal/INBOX

Channel Personal
Master :Personal-remote:
Slave :Personal-local:
Patterns *
Create Slave
Sync All
Expunge Both
SyncState *
No concurrent IMAP commands supported

My IMAP provider doesn't handle concurrent IMAP commands. mbsync and Office 365 had the answer:

PipelineDepth 1
Initial sync

Run initial from the command line sync:

mbsync -Va

While syncing my largest inbox, it sometimes received an unexpected EOF error:

IMAP error: unexpected EOF from some.imap.host.com (1.2.3.4:993)

First few times, I restarted the syncing manually, but then used a loop to automatically restart it.

Bash loops:

 while true;  do mbsync -V Personal; sleep 5;  done
 for i  in {1..5};  do mbsync -V Personal; sleep 5 done

Eshell loop:

 for i  in (number-sequence 1 10) {mbsync -V Personal; sleep 5}
Create mu index

Reindex using mu, but first remove existing index for offlineimap messages:

rm -rf ~/.mu

Ok, do index now:

mu index --maildir=~/IMAP
Mu4e tweaks

The get mail command should now point to mbsync.

(csetq mu4e-get-mail-command  "mbsync -Va")

I had issues with duplicate IDs after moving and deleting messages from mu4e. Migrating from offlineimap to mbsync for mu4e had the answer:

(csetq mu4e-change-filenames-when-moving t)

A Complete Guide to Email in Emacs using Mu and Mu4e   website

[2021-12-18 Sat 09:41]

Article

Part 4 of

Emacs For Productivity

Thu 2 Mar 2017

Gregory J Stein

Category

General Computing

Tags

Emacs Productivity

*

A Complete Guide to Email in Emacs using Mu and Mu4e

Thu 2 Mar 2017 Gregory J Stein

Category General Computing

Tags Emacs Productivity

Part 4 of Emacs For Productivity

I get a lot of email. I'm also pretty sure you get a lot of email. However, email is still not a solved problem This is evidenced by the fact that a quick Google search yields no less than ten viable options for email clients on my Mac. . Each potential email client is acceptable on it's own, yet none of them satisfied all of my desired features:

This is evidenced by the fact that a quick Google search yields no less than ten viable options for email clients on my Mac.

  • The ability to access my email without an internet connection I travel quite a lot, so this was very important to me. .
  • Easily move messages between different folders, which is how I keep all of my emails organized by project.
  • Quick yet powerful search of all my mail messages.
  • Having an auto-updating status indicator that shows me how many unread messages I have.
  • Managing multiple accounts (Gmail for personal emails and Microsoft Exchange for work emails) and syncing local changes so that my phone can still be up-to-date.

If you follow this blog, you'll recognize that I've gotten a bit carried away with migrating the different aspects of my life to operate within the Emacs environment. So it was only a matter of time until I finally decided to give it a shot, and I converged upon a solution which happily satisfies all of the above constraints. Every email service is a bit different so YMMV, but this setup works for me.

Here's a screenshot of what we'll be setting up:

A screenshot of the mu4e interface after searching for recent emails from Amazon. Notice that I've "marked" a number of messages for deletion d, archiving r, and moving m. I also have an icon at the bottom right corner that shows I have 17 unread messages.

/media/photologue/photos/cache/Screen_Shot_2017-03-01_at_8.55.28_AM_display.png

A screenshot of the mu4e interface after searching for recent emails from Amazon. Notice that I've "marked" a number of messages for deletion d, archiving r, and moving m. I also have an icon at the bottom right corner that shows I have 17 unread messages.

My brief adventure with Gnus

There are a ton of tutorials available for reading one's email with Gnus, so it was a natural starting point for my quest. After setting it up, Gnus starts into the standard group summary list, which will display all of the folders it discovered in your various mail accounts. To me, this seemed a bit much, since I have a ton of folders, but this alone wasn't enough to deter me. Unfortunately, I also found that it was quite slow In fact, the slowness of Gnus is rather well documented on the Emacs Wiki and that the interface was rather cumbersome. The suggested solution to this problem is to host a local email server ( groan), which syncs with the Gmail and your other email accounts.

In fact, the slowness of Gnus is rather well documented on the Emacs Wiki

In order to get Gnus working properly, Sacha Chua recommends installing two tools: offlineimap for the email synchronization and dovecot for hosting a local IMAP server, since that's how Gnus is able to read the messages. I was able to get offlineimap working relatively quickly (more on that later), and before too long I had a local copy of all my emails since the dawn of time. By contrast, dovecot had me scratching my head. Not only could I not get it to work, but it seemed like an unnecessary amount of complexity; I was hosting a local webserver so that my Emacs mail client could read emails that were already saved to my system. So it was at this point that I moved on in search of a better way.

An introduction to mu

After a bit of searching around, I came across a fantastic tool called mu. At it's core, mu is a simple command line tool for searching through emails See the mu "cheatsheet" for examples of more powerful search features within mu. . Simply type mu find $SEARCH into the terminal to query your emails.

See the mu "cheatsheet" for examples of more powerful search features within mu.

It's a cute little tool, and is especially nice for allowing you to quickly check for any new email You can easily search for unread emails with flag:unread. without leaving the terminal. Yet this still doesn't solve all my problems; sure I have an offline copy of my messages and I can search them with ease, but how do I read them, move them around, or interact with them in other ways?

You can easily search for unread emails with flag:unread.

This is where mu4e comes in, the Emacs email client included with mu. It's this that provides me with all of the functionality that I desire: being able to search an offline copy of my emails, easily move them around, and send/reply to different mail servers.

In addition, mu4e has the ability to auto-complete email addresses from names, follow rules about where to archive mail that matches certain filters (like keywords in the subject line) and, via an Emacs package, display a status icon in the modeline when I have new mail messages. In the next few sections, I'll describe how I got everything to work, and any pitfalls I encountered along the way.

Getting set up with mu and OfflineIMAP

As advertised, mu is really just for indexing and searching emails, and relies on other software to maintain a local copy of your messages, which it can then use. To do this, I chose to use the popular OfflineIMAP On macOS, I installed this with brew install offlineimap; on Ubuntu, this can be done with apt. , since it's relatively easy to get setup. I have my OfflineIMAP manage two different accounts, Gmail and Exchange, and sync changes between the online services every 5 minutes. Rather than ramble on about how everything should be set up, I'll just reproduce some of the important parts of my configuration file here (taken from my ~/.offlineimaprc):

On macOS, I installed this with brew install offlineimap; on Ubuntu, this can be done with apt.

And example OfflineIMAP configuration python

And example OfflineIMAP configuration

python

 [general]
accounts = Gmail, Exchange
maxsyncaccounts = 2

[Account Gmail]
localrepository = LocalGmail
remoterepository = RepositoryGmail
autorefresh = 5
quick = 10
postsynchook = mu index --maildir ~/Maildir
status_backend = sqlite

[Reposiroty LocalGmail]
type = Maildir
localfolders = ~/Maildir/Gmail

[Reposiroty RepositoryGmail]
type = Gmail
maxconnections = 2
remoteuser = YOUR_GMAIL_USERNAME
remotepass = YOUR_GMAIL_PASSWORD
folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail', '[Gmail]/Important']
sslcacertfile = /usr/local/etc/openssl/cert.pem  # This will only work for macOS

## Try one of the following for Ubuntu or Arch:
# sslcacertfile = /etc/ssl/certs/ca-certificates.crt
# sslcacertfile = OS-DEFAULT

# These are effectively the same as the above
[Account Exchange]
[Repository LocalExchange]
[Repository RemoteExchange]

You'll notice a few things about this configuration You may also notice that arbitrary python code can be specified as part of the configuration. . First, as I have it listed above, you have to enter your password directly into this file, which you probably don't want to do; there's a great Stack Exchange post on how to use GPG and python to encrypt your password. Second, I have included a folderfilter to avoid storing the All Mail and Important folders that Gmail annoyingly creates. Finally, I call mu index whenever the sync is complete, via postsynchook, to ensure that my mu database is as up-to-date as much as possible By default, mu looks to ~/Maildir for mail, but I like to include it for clarity. .

You may also notice that arbitrary python code can be specified as part of the configuration.

By default, mu looks to ~/Maildir for mail, but I like to include it for clarity.

Once this is setup, calling offlineimap from the command line will sync with the remote repositories every 5 minutes. However, this requires keeping the terminal window open. This can be solved by creating a daemon process. On macOS, this is built in to brew, and calling brew services start offlineimap will get everything started; for Linux, you can follow these instructions on the Arch Linux wiki.

With this step complete, the command line version of mu should now be syncing with the remote server(s) without any issues.

Configuring mu4e

Before even getting to the Emacs configuration file, you should ensure that mu4e is properly installed. Since mu4e is included with the installation of mu On macOS, this is only partially true. See to insure that the install includes mu4e. you need to include mu4e. This can be done with something like (add-to-list 'load-path "/usr/local/share/emacs/site-lisp/mu/mu4e"). Now, upon reopening Emacs, M-x mu4e should open a simple window with some shortcuts. Typing J will bring up a menu for selecting a mail folder. Chose one, and you should be presented with something resembling the screenshot above.

On macOS, this is only partially true. See to insure that the install includes mu4e.

When in the headers view, which displays your email messages, you can easily navigate through different messages using n and p and hitting return will open a message, allowing you to read it. In addition, mu4e includes some very useful marking capabilities: d marks a message for deletion, r for refiling/archiving, and m for moving (after a target directory is specified). Simply press x to "execute" the marks. In addition, with * you can "bulk mark" emails; pressing x after some messages have been marked with x will allow you to perform an action to all of them. See the mu4e user manual for more details.

I mentioned above that I have two different email addresses and rely on mu4e to manage them both. In the previous screenshot, you can see that I've marked messages for archiving with r and deletion See the section below for a caveat about deletion, to avoid premeturely deleting your messages! with d yet the behavior for the different messages changes depending on their mu4e context. By setting the mu4e-contexts variable, mu4e will search through the list of options, see if the message of interest matches :match-func and sets some local variables, like the mu4e-refile-folder. In the snippet below, I check to see if the mail directory ( :maildir) includes /Gmail and, if it does, sets the trash and refile folders accordingly.

See the section below for a caveat about deletion, to avoid premeturely deleting your messages!

Defining mu4e contexts lisp

Defining mu4e contexts

lisp

 (setq mu4e-contexts
 `( ,(make-mu4e-context
     :name "Gmail"
     :match-func (lambda (msg) (when msg
       (string-prefix-p "/Gmail" (mu4e-message-field msg :maildir))))
     :vars '(
       (mu4e-trash-folder . "/Gmail/[Gmail].Trash")
       (mu4e-refile-folder . "/Gmail/[Gmail].Archive")
       ))
   ,(make-mu4e-context
     :name "Exchange"
     :match-func (lambda (msg) (when msg
       (string-prefix-p "/Exchange" (mu4e-message-field msg :maildir))))
     :vars '(
       (mu4e-trash-folder . "/Exchange/Deleted Items")
       (mu4e-refile-folder . exchange-mu4e-refile-folder)
       ))
   ))

For my Exchange server, I have a slightly more complicated procedure; rather than including a specific refile folder, I define a function exchange-mu4e-refile-folder which does some more filtering. Apparently I don't want any emails from this fictitious going to the typical archive folder. So, whenever I get a message which includes "[some-mailing-list]" in the subject, I can still refile the message with r and know that it will go to the correct folder.

A custom refiling function lisp

A custom refiling function

lisp

 (defun exchange-mu4e-refile-folder (msg)
  "Function for chosing the refile folder for my Exchange email.
   MSG is a message p-list from mu4e."
  (cond
   ;; FLA messages
   ((string-match "\\[some-mailing-list\\]"
          (mu4e-message-field msg :subject))
    "/Exchange/mailing-list")
   (t "/Exchange/Archive")
   )
  )
Alerts for new mail

Now that we can receive email, move it around and keep everything in sync with our different IMAP servers, the next task is to ensure that we're alerted whenever new mail arrives. Fortunately, there's another Emacs package for doing just this: mu4e-alert. The procedure for using mu4e-alert is relatively simple. Whenever you call mu4e-alert-enable-mode-line-display, your modeline will be updated to include a little envelope icon and the current count of unread messages The format of the modeline display can be changed by customizing mu4e-alert-modeline-formatter. .

The format of the modeline display can be changed by customizing mu4e-alert-modeline-formatter.

You're expectedly a bit annoyed, thinking I thought the icon would update itself! Fortunately, Emacs has the run-with-timer for just this purpose. However, there remains a small issue: whenever mu4e is open, it maintains a connection to the server. This means that mu index cannot be run by the OfflineIMAP process whenever mu4e is left open, and new mail will not appear. This is far from ideal. Again, I have a slightly hacky solution. By calling mu4e~proc-kill periodically, we can sever mu4e's connection to the server. The only consequence of this is that I may occasionally try to archive messages in my inbox that I've already moved on my phone, an issue which is easily remedied by refreshing my mu4e buffer.

My complete mu4e-alert configuration, which relies on John Wiegley's use-package, is as follows:

Mu4e-alert configuration lisp

Mu4e-alert configuration

lisp

 (use-package mu4e-alert
  :ensure t
  :after mu4e
  :init
  (setq mu4e-alert-interesting-mail-query
    (concat
     "flag:unread maildir:/Exchange/INBOX "
     "OR "
     "flag:unread maildir:/Gmail/INBOX"
     ))
  (mu4e-alert-enable-mode-line-display)
  (defun gjstein-refresh-mu4e-alert-mode-line ()
    (interactive)
    (mu4e~proc-kill)
    (mu4e-alert-enable-mode-line-display)
    )
  (run-with-timer 0 60 'gjstein-refresh-mu4e-alert-mode-line)
  )

There's one other hiccup that I haven't yet mentioned; some email servers ( cough Gmail cough) will mark messages as unread whenever they are moved to other folders, including the trash. As a result, I've customized my mu4e-alert-interesting-mail-query variable to check for unread messages in only my inbox folders.

Using mu4e to send mail

Unfortunately IMAP, the protocol for checking email and moving them around, cannot be used to send emails: for that you need to configure SMTP. This process isn't particularly difficult, but it does include a bunch of code, most of which is adapted from the mu4e documentation If you only have a single account, most of this is unnecessary. . After setting the default values for many of the SMTP parameters, we create a list of account-specific parameter values which are loaded upon composing a message by the my-mu4e-set-account function. I've included most of my configuration here for the sake of completeness.

If you only have a single account, most of this is unnecessary.

Configuration for sending mail lisp

Configuration for sending mail

lisp

 ;; I have my "default" parameters from Gmail
(setq mu4e-sent-folder "/Users/Greg/Maildir/sent"
      ;; mu4e-sent-messages-behavior 'delete ;; Unsure how this should be configured
      mu4e-drafts-folder "/Users/Greg/Maildir/drafts"
      user-mail-address "gregory.j.stein@gmail.com"
      smtpmail-default-smtp-server "smtp.gmail.com"
      smtpmail-smtp-server "smtp.gmail.com"
      smtpmail-smtp-service 587)

;; Now I set a list of
(defvar my-mu4e-account-alist
  '(("Gmail"
     (mu4e-sent-folder "/Gmail/sent")
     (user-mail-address "YOUR.GMAIL.USERNAME@gmail.com")
     (smtpmail-smtp-user "YOUR.GMAIL.USERNAME")
     (smtpmail-local-domain "gmail.com")
     (smtpmail-default-smtp-server "smtp.gmail.com")
     (smtpmail-smtp-server "smtp.gmail.com")
     (smtpmail-smtp-service 587)
     )
     ;; Include any other accounts here ...
    ))

(defun my-mu4e-set-account ()
  "Set the account for composing a message.
   This function is taken from:
     https://www.djcbsoftware.nl/code/mu/mu4e/Multiple-accounts.html"
  (let* ((account
    (if mu4e-compose-parent-message
        (let ((maildir (mu4e-message-field mu4e-compose-parent-message :maildir)))
    (string-match "/\\(.*?\\)/" maildir)
    (match-string 1 maildir))
      (completing-read (format "Compose with account: (%s) "
             (mapconcat #'(lambda (var) (car var))
            my-mu4e-account-alist "/"))
           (mapcar #'(lambda (var) (car var)) my-mu4e-account-alist)
           nil t nil nil (caar my-mu4e-account-alist))))
   (account-vars (cdr (assoc account my-mu4e-account-alist))))
    (if account-vars
  (mapc #'(lambda (var)
      (set (car var) (cadr var)))
        account-vars)
      (error "No email account found"))))
(add-hook 'mu4e-compose-pre-hook 'my-mu4e-set-account)
Pitfalls and additional tweaks

I already touched upon a few of the minor issues I encountered when getting everything here to work properly, including how moved messages will occasionally be marked as unread. The biggest uh oh I had to deal with stemmed from some unexptected behavior with OfflineIMAP. Apparently, whenever a message is marked with the trash label T, which happens whenever you 'delete' a message with d, OfflineIMAP won't sync it back to the server and, worse still, may delete it entirely. Even though I've marked an item for deletion, I'm comforted by the fact that I can recover a message if I accidentally move it to the trash.

Avoiding this issue requires modifying the way the delete mark d operates. I simply replaced +T-N with -N in the definition of the trash mark. It was a simple (if rather verbose) fix, so I've included it here in its entirety.

Avoid trashing when deleting lisp

Avoid trashing when deleting

lisp

 (defun remove-nth-element (nth list)
  (if (zerop nth) (cdr list)
    (let ((last (nthcdr (1- nth) list)))
      (setcdr last (cddr last))
      list)))
(setq mu4e-marks (remove-nth-element 5 mu4e-marks))
(add-to-list 'mu4e-marks
     '(trash
       :char ("d" . "▼")
       :prompt "dtrash"
       :dyn-target (lambda (target msg) (mu4e-get-trash-folder msg))
       :action (lambda (docid msg target)
                 (mu4e~proc-move docid
                    (mu4e~mark-check-target target) "-N"))))

Finally, here are a few more tweaks to the mu4e settings that I frequently use.

Other tweaks lisp

Other tweaks

lisp

 ;; Include a bookmark to open all of my inboxes
(add-to-list 'mu4e-bookmarks
       (make-mu4e-bookmark
        :name "All Inboxes"
        :query "maildir:/Exchange/INBOX OR maildir:/Gmail/INBOX"
        :key ?i))

;; This allows me to use 'helm' to select mailboxes
(setq mu4e-completing-read-function 'completing-read)
;; Why would I want to leave my message open after I've sent it?
(setq message-kill-buffer-on-exit t)
;; Don't ask for a 'context' upon opening mu4e
(setq mu4e-context-policy 'pick-first)
;; Don't ask to quit... why is this the default?
(setq mu4e-confirm-quit nil)
Wrapping Up

I'll try to keep this document up-to-date as I experiment more, however I'm already quite happy with my setup after a couple of weeks of trying it out. There are plenty of features that I haven't touched upon as well, including the ability to link to email messages via org-mode, in which I do much of my work. At any rate, it's just another excuse for me to never leave my Emacs environment.


Liked this post? Subscribe to our RSS feed or add your email to our newsletter:


all posts in series

Emacs For Productivity

A collection of tips and guides for using Emacs to master your life.


https://twitter.com/intent/tweet?url=http%3A%2F%2Fcachestocaches.com%2F2017%2F3%2Fcomplete-guide-email-emacs-using-mu-and-%2F&text=Great%20post%20at https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fcachestocaches.com%2F2017%2F3%2Fcomplete-guide-email-emacs-using-mu-and-%2F https://plus.google.com/share?url=http%3A%2F%2Fcachestocaches.com%2F2017%2F3%2Fcomplete-guide-email-emacs-using-mu-and-%2F http://www.linkedin.com/shareArticle?mini=true&url=http%3A%2F%2Fcachestocaches.com%2F2017%2F3%2Fcomplete-guide-email-emacs-using-mu-and-%2F&title=Caches%20To%20Caches&source=http%3A//cachestocaches.com /feed


+ Show Comments From Disqus

Please enable JavaScript to view the comments powered by Disqus.

Sending mail using multiple Mu4e contexts in Emacs | Ivan Tomica   website

[2021-12-18 Sat 09:41]

Article

Ivan Tomica

data:image/svg+xml;base64,PHN2ZyBpZD0ibW9vbiIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdib3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgc3Ryb2tlPSJjdXJyZW50Q29sb3IiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIj4gPHBhdGggZD0iTTIxIDEyLjc5QTkgOSAwIDEgMSAxMS4yMSAzIDcgNyAwIDAgMCAyMSAxMi43OXoiPjwvcGF0aD48L3N2Zz4= data:image/svg+xml;base64,PHN2ZyBpZD0ic3VuIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld2JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9ImN1cnJlbnRDb2xvciIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiPiA8Y2lyY2xlIGN4PSIxMiIgY3k9IjEyIiByPSI1Ij48L2NpcmNsZT4gPGxpbmUgeDE9IjEyIiB5MT0iMSIgeDI9IjEyIiB5Mj0iMyI+PC9saW5lPiA8bGluZSB4MT0iMTIiIHkxPSIyMSIgeDI9IjEyIiB5Mj0iMjMiPjwvbGluZT4gPGxpbmUgeDE9IjQuMjIiIHkxPSI0LjIyIiB4Mj0iNS42NCIgeTI9IjUuNjQiPjwvbGluZT4gPGxpbmUgeDE9IjE4LjM2IiB5MT0iMTguMzYiIHgyPSIxOS43OCIgeTI9IjE5Ljc4Ij48L2xpbmU+IDxsaW5lIHgxPSIxIiB5MT0iMTIiIHgyPSIzIiB5Mj0iMTIiPjwvbGluZT4gPGxpbmUgeDE9IjIxIiB5MT0iMTIiIHgyPSIyMyIgeTI9IjEyIj48L2xpbmU+IDxsaW5lIHgxPSI0LjIyIiB5MT0iMTkuNzgiIHgyPSI1LjY0IiB5Mj0iMTguMzYiPjwvbGluZT4gPGxpbmUgeDE9IjE4LjM2IiB5MT0iNS42NCIgeDI9IjE5Ljc4IiB5Mj0iNC4yMiI+PC9saW5lPjwvc3ZnPg==

Sending mail using multiple Mu4e contexts in Emacs

December 24, 2020 · 3 min · Ivan Tomica

My current email client of choice is Mu4e within Emacs. There's plethora of reasons why I have settled for it instead of some other client and why in the end, I transitioned to it from mutt, but that's perhaps a topic for some future article.

Setting up Mu4e in the beginning was pretty simple as I had only one email account and used asynchronous email function to queue emails and then flush them all in one go. Whole asynchronous process relied on using smtpmail-send-it function.

Unfortunately, I had to ditch the whole process of queuing email and sending it asynchronously due to the fact that I had to configure multiple accounts in Mu4e.

Mu4e configuration #

Setting up email account with context is pretty straight-forward. For simplicity sake here's an example with single account context, to add one more, you'd basically only copy/paste ,(make-mu4e-context part N times

 (setq mu4e-contexts
      `(,(make-mu4e-context
          :name "Private"
          :match-func (lambda (msg) (when msg
                                      (string-prefix-p "/Private" (mu4e-message-field msg :maildir))))
          :vars '(
                  (user-full-name . "Ivan Tomica")
                  (user-mail-address . "MYEMAIL")
                  (mu4e-compose-signature . (concat
                                             "Ivan Tomica\n"
                                             "https://www.tomica.net\n"))
                  (mu4e-sent-folder . "/Sent")
                  (mu4e-drafts-folder . "/Drafts")
                  (mu4e-trash-folder .  "/Trash")
                  (mu4e-refile-folder . "/Archive")
                  (mu4e-maildir-shortcuts . (("/INBOX" . ?i)
                                             ("/Archive" . ?a)
                                             ("/Sent" . ?s)
                                             ("/Trash" . ?t)
                                             ("/Junk" . ?j)))))))

That's basically enough for reading, searching and managing messages locally.

The problem starts with sending email.

If you're familiar with mu4e you know it is just an interface for mu, meaning it has no sending capabilities, instead, you're using smtpmail-send-it function which is configured with following variables

 (setq smtpmail-smtp-server "MAILSERVERADDRESS"
      smtpmail-stream-type 'starttls
      smtpmail-smtp-service 587)

But as you can see, there's no way to switch those easily depending on which account you use for sending email.

My first instinct was to just configure those within the mu4e context, but unfortunately, for some weird reason, that didn't work. Setting up variables works just fine, but when you try to send the message, smtpmail-send-it function fails with no clear explanation why. My attempts to debug it were, unsuccessful to say the least…

So, after two days of banging my head against the wall and trying multiple approaches and hacks I finally decided to transition to msmtp. Configuring Emacs was easy as

 (setq sendmail-program "/usr/bin/msmtp"
      message-sendmail-f-is-evil t
      message-sendmail-extra-arguments '("--read-envelope-from")
      send-mail-function 'smtpmail-send-it
      message-send-mail-function 'message-send-mail-with-sendmail)
msmtp #

And configuring .msmtprc was even simpler

 defaults
auth  on
tls   on
tls_trust_file /etc/ssl/certs/ca-bundle.crt
logfile ~/.cache/msmtp.log

# Private
account MYACCOUNT
host    MYSERVER
port    587
from    MYEMAIL
user    MYEMAIL
passwordeval "secret-tool lookup MYEMAIL password"

account default : MYACCOUNT

And there's also native way to use GNOME Keyring, so instead of using passwordeval command as I do, you could use something similar to what is mentioned in Arch Wiki

 secret-tool store --label=msmtp host smtp.your.domain service smtp user yourusername

and omit password and passwordeval options all together.


Please enable JavaScript to load the comments.

© 2021 Ivan Tomica · Powered by Hugo · Theme PaperMod

data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdib3g9IjAgMCAxMiA2IiBmaWxsPSJjdXJyZW50Q29sb3IiPiA8cGF0aCBkPSJNMTIgNkgwbDYtNnoiPjwvcGF0aD48L3N2Zz4=

Contexts example (Mu4e 1.6.0 user manual)   website

[2021-12-18 Sat 09:42]

Article

<<Contexts-example>>

Previous: Contexts and special folders, Up: Contexts [ Contents]


<<Example>>

9.4 Example

Let's explain how contexts work by looking at an example. We define two contexts, Private' and Work' for a fictional user Alice Derleth.

Note that in this case, we automatically switch to the first context when starting; see the discussion in the previous section.

  (setq mu4e-contexts
     `( ,(make-mu4e-context
           :name "Private"
           :enter-func (lambda () (mu4e-message "Entering Private context"))
           :leave-func (lambda () (mu4e-message "Leaving Private context"))
           ;; we match based on the contact-fields of the message
           :match-func (lambda (msg)
                         (when msg
                           (mu4e-message-contact-field-matches msg
                             :to "aliced@home.example.com")))
           :vars '( ( user-mail-address      . "aliced@home.example.com"  )
                    ( user-full-name     . "Alice Derleth" )
                    ( mu4e-compose-signature .
                      (concat
                        "Alice Derleth\n"
                        "Lauttasaari, Finland\n"))))
        ,(make-mu4e-context
           :name "Work"
           :enter-func (lambda () (mu4e-message "Switch to the Work context"))
           ;; no leave-func
           ;; we match based on the maildir of the message
           ;; this matches maildir /Arkham and its sub-directories
           :match-func (lambda (msg)
                         (when msg
                           (string-match-p "^/Arkham" (mu4e-message-field msg :maildir))))
           :vars '( ( user-mail-address       . "aderleth@miskatonic.example.com" )
                    ( user-full-name      . "Alice Derleth" )
                    ( mu4e-compose-signature  .
                      (concat
                        "Prof. Alice Derleth\n"
                        "Miskatonic University, Dept. of Occult Sciences\n"))))

        ,(make-mu4e-context
           :name "Cycling"
           :enter-func (lambda () (mu4e-message "Switch to the Cycling context"))
           ;; no leave-func
           ;; we match based on the maildir of the message; assume all
           ;; cycling-related messages go into the /cycling maildir
           :match-func (lambda (msg)
                         (when msg
                           (string= (mu4e-message-field msg :maildir) "/cycling")))
           :vars '( ( user-mail-address       . "aderleth@example.com" )
                    ( user-full-name      . "AliceD" )
                    ( mu4e-compose-signature  . nil)))))

   ;; set `mu4e-context-policy` and `mu4e-compose-policy` to tweak when mu4e
  should
   ;; guess or ask the correct context, e.g.

   ;; start with the first (default) context;
   ;; default is to ask-if-none (ask when there's no context yet, and none match)
   ;; (setq mu4e-context-policy 'pick-first)

   ;; compose with the current context is no context matches; default is to ask
   ;; (setq mu4e-compose-context-policy nil)

A couple of notes about this example:

  • You can manually switch the context use M-x mu4e-context-switch, by default bound to ; in headers, view and main mode. The current context appears in the mode-line.
  • Normally, M-x mu4e-context-switch does not call the enter or leave functions if the 'new' context is the same as the old one. However, with a prefix-argument ( C-u), you can force mu4e to invoke those function even in that case.
  • The function mu4e-context-current returns the current-context; the current context is also visible in the mode-line when in headers, view or main mode.
  • You can set any kind of variable; including settings for mail servers etc. However, settings such as mu4e-mu-home are not changeable after they have been set without quitting mu4e first.
  • leave-func (if defined) for the context we are leaving, is invoked before the enter-func (if defined) of the context we are entering.
  • enter-func (if defined) is invoked before setting the variables.
  • match-func (if defined) is invoked just before mu4e-compose-pre-hook.
  • See the variables mu4e-context-policy and mu4e-compose-context-policy to tweak what mu4e should do when no context matches (or if you always want to be asked).
  • Finally, be careful to get the quotations right — backticks, single quotes and commas and note the '.' between variable name and its value.

Previous: Contexts and special folders, Up: Contexts [ Contents]