JoaoESmoreira

Mu4e

Hello there!

In this blog I will show you how I configured my email accounts on Emacs and a simple use case, using mu4e. Of course, you can find the complete source code of my emacs configuration on my GitHub repository https://github.com/JoaoESmoreira/emacsphere/ . There is also a dedicated tab for the project at Emacsphere.

Requirements

Let's start by installing all required packages. In my case, I am installing it on Arch Linux by the way.

sudo pacman -S isync msmtp 
yay -S mu

Configuring Mbsync

Encripting Passwords

First thing first, let’s securely store and encrypt your passwords.

mkdir -p .password-store/
cd .passwrod-store

touch "your-password" > gmail
touch "your-password" > dei

gpg -c .password-store/gmail
gpg -c .password-store/dei

rm gmail dei

If you’re using Gmail, you must create an App Password. You can do this by visiting: https://myaccount.google.com/apppasswords

Setting up mbsyncrc

Create the configuration file at ~/.mbsyncrc and past the following content. Make sure you replace email addresses with your actual ones.

# #############
# # gmail
# #############
IMAPAccount gmail
Host imap.gmail.com
User your-email-address@gmail.com
PassCmd "gpg --no-tty --for-your-eyes-only -dq ~/.password-store/gmail.gpg"
TLSType IMAPS

IMAPStore gmail-remote
Account gmail

MaildirStore gmail-local
Path ~/Mail/gmail/
Inbox ~/Mail/gmail/INBOX

Channel gmail
Far :gmail-remote:
Near :gmail-local:
Patterns *
Create Both
Expunge Both
SyncState *

#############
# DEI
#############
IMAPAccount dei
Host student.dei.uc.pt
User your-email-address
PassCmd "gpg --no-tty --for-your-eyes-only -dq ~/.password-store/dei.gpg"
TLSType IMAPS
Port 993

IMAPStore dei-remote
Account dei

MaildirStore dei-local
Path ~/Mail/dei/
Inbox ~/Mail/dei/INBOX
Subfolders Verbatim

Channel dei
Far :dei-remote:
Near :dei-local:
Patterns INBOX mail/Drafts mail/Junk mail/sent-mail mail/Trash
Create Both
Expunge Both
SyncState *

Before syncing your emails, make sure to create the local mail directories:

mkdir -p ~/Mail/gmail
mkdir -p ~/Mail/dei

Setting up MU and Indexing

Now, sync your emails and index them using mu.

mbsync -a
mu init --maildir=~/Mail --my-address=your_address@gmail.com --my-address=your_address@student.dei.uc.pt
mu index                                                                                                 

Configuring mu4e in Emacs

Paste the following configuration in your Emacs config file:

(use-package mu4e
  :ensure nil
  :load-path "/usr/share/emacs/site-lisp/mu4e/"
  :config
  (setq mu4e-change-filenames-when-moving t
        mu4e-update-interval (* 3 60)
        mu4e-get-mail-command "mbsync -a"
        mu4e-maildir "~/Mail"
        message-send-mail-function 'smtpmail-send-it
        auth-sources '("~/.authinfo.gpg"))

  ;; sort emails by date
  (setq mu4e-headers-sort-field :date)
  (setq mu4e-headers-sort-direction 'descending)

  ;; Shortcuts
  (setq mu4e-maildir-shortcuts
        '(("/gmail/INBOX"             . ?g)
          ("/gmail/Sent"              . ?G)
          ("/gmail/Trash"             . ?x)
          ("/gmail/Drafts"            . ?d)
          ("/dei/INBOX"               . ?i)
          ("/dei/mail/sent-mail"      . ?s)
          ("/dei/mail/Trash"          . ?t)
          ("/dei/mail/Drafts"         . ?D)))

  ;; Context for multiple accounts
  (setq mu4e-contexts
        `( ,(make-mu4e-context
             :name "Gmail"
             :enter-func (lambda () (mu4e-message "Loading Gmail..."))
             :match-func (lambda (msg)
                           (when msg
                             (string-prefix-p "/gmail" (mu4e-message-field msg :maildir))))
             :vars '((user-mail-address . "your-address@gmail.com")
                     (user-full-name . "username1")
                     (mu4e-sent-folder   . "/gmail/Sent")
                     (mu4e-drafts-folder . "/gmail/Drafts")
                     (mu4e-trash-folder  . "/gmail/Trash")
                     (mu4e-refile-folder . "/gmail/Archive")
                     (smtpmail-smtp-server . "smtp.gmail.com")
                     (smtpmail-smtp-service . 465)
                     (smtpmail-stream-type . ssl)))

           ,(make-mu4e-context
             :name "DEI"
             :enter-func (lambda () (mu4e-message "Loading DEI..."))
             :match-func (lambda (msg)
                           (when msg
                             (string-prefix-p "/dei" (mu4e-message-field msg :maildir))))
             :vars '((user-mail-address . "your-address@student.dei.uc.pt")
                     (user-full-name . "username2")
                     (mu4e-sent-folder   . "/dei/mail/sent-mail")
                     (mu4e-drafts-folder . "/dei/mail/Drafts")
                     (mu4e-trash-folder  . "/dei/mail/Trash")
                     (mu4e-refile-folder . "/dei/mail/Archive")
                     (smtpmail-smtp-server . "smtp.dei.uc.pt")
                     (smtpmail-smtp-service . 465)
                     (smtpmail-stream-type . ssl)))))
)

Configuring msmtp

Msmtp is a simple and effective SMTP client we use to send emails from Emacs through mu4e.

Create the configuration file

Create a file called ~/.msmtprc with the following content. Replace all placeholder values with your actual information:

# Set default values
defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log

# Gmail account
account gmail
host smtp.gmail.com
port 465
from your-address@gmail.com
user your-address@gmail.com
passwordeval gpg --no-tty --for-your-eyes-only -dq ~/.password-store/gmail.gpg
tls_starttls off
tls on

# DEI account
account dei
host smtp.dei.uc.pt
port 465
from your-address@student.dei.uc.pt
user your-address@student.dei.uc.pt
passwordeval gpg --no-tty --for-your-eyes-only -dq ~/.password-store/dei.gpg
tls_starttls off
tls on

# Set default account
account default : gmail

Secure the file

Msmtp expects the configuration file to be only readable by the user for security reasons. So, change the permissions:

chmod 600 ~/.msmtprc

Using Mu4e - Use Case

Once everything is configured, you are ready to start managing your emails directly from Emacs using Mu4e.

Launch Mu4e

Open Emacs and launch Mu4e by typing:

M-x mu4e

This will open the mu4e main dashboard.

Interface Overview

Mu4e is divided into sections:

  • Jump to maildir: Quickly go to a mailbox (e.g., inbox, sent).
  • Compose a new message: Start writing a new email.
  • Update mail & index: Syncs emails using mbsync and re-indexes them with mu.
  • Search mail: Allows you to perform complex searches.
  • Bookmarks: Custom queries for quick access.

Basic Shortcuts

Here is a table with essential mu4e commands and keybindings:

Action Keybinding
Open Mu4e M-x mu4e
Sync & index mail U
Compose a new email C
Reply to a message R
Forward a message f
Mark for deletion d
Move to another folder m
Search mails /
View attachment V
Toggle context (account) ;
Send C-c C-c

Writing and Sending Emails

To compose a new email:

  1. Press c inside mu4e.
  2. Fill in the To:, Subject: and message body.
  3. Press C-c C-c to send.

Mu4e will send your message using msmtp based on the current context (Gmail or DEI).

Switching Contexts

If you configured mu4e-contexts, mu4e automatically chooses the context based on the folder or message. But you can also manually switch:

; Switch context manually
M-x mu4e-context-switch

You can also press ; while inside mu4e to switch between configured contexts.

Searching Emails

You can search all indexed mail using /:

from:someone@example.com subject:meeting date:today

You can use advanced queries like:

  • flag:unread – Unread emails
  • maildir:/gmail/INBOX – Mails in Gmail inbox
  • to:joao – Emails sent to João

Syncing Mail

To manually sync your mail (run mbsync and reindex):

U

Alternatively, mu4e is configured to sync automatically every 3 minutes based on:

(setq mu4e-update-interval (* 3 60)) ; every 3 minutes

Deleting and Archiving

  • Press d to mark an email for trash.
  • Press r to refile (archive) a message.
  • Execute marked actions with x.

Final Thoughts

And that's it! You now have a fully working setup of mu4e with multiple email accounts in Emacs. This configuration is both minimal and powerful, giving you a streamlined workflow integrated right into your Emacs environment.

If you have any questions or suggestions, feel free to open an issue or pull request on the GitHub repository.

Footer

Copyright © 2025 Joao ES Moreira

The contents of this website are licensed under the Creative Commons Attribution-NoDerivatives 4.0 International License (CC-BY-ND 4.0).

The source code of this website is licensed under the MIT license, and available in GitHub repositor. User-submitted contributions to the site are welcome, as long as the contributor agrees to license their submission with the CC-BY-ND 4.0 license.