Emacs is amazing

Emacs is amazing. I am finally appreciating the power of emacs which to me is being able to use Emacs as an operating system. One of the things you can use Emacs for is email. I will speak in particular about protonmail. For this to work you need protonmail bridge.

How does mu4e work?

Interestingly mu4e does not fetch your mail from gmail or protonmail servers; mu4e uses either programs such as fetchmail, offlinemap, isync/mbsync. And it uses smptmail to send messages; instead what mu4e is manipulate the messages when it programs such as fetchmail fetches the messages; moreover, mu4e uses mu to search for messages. mu4e communicates with mu - mu indexes the messages which is to say it puts them in a special database.. The messages are stored in a given directory; for example, in this article we will store the messages in the directory ~/.mail but it also common to store the messages in a directory called ~/Maildir

Dependencies

You will need to install mu and isync

.mbsyncrc

First you will need to an .mbsyncrc but you need to make: mkdir ~/.mbsyncrc.

Paste the following to this file:

IMAPAccount protonmail
Host 127.0.0.1
Port 1143
User <your-email>
PassCmd "gpg -q --for-your-eyes-only --no-tty -d ~/.authinfo.gpg | awk 'FNR == 2 {print $8}'"
SSLType STARTTLS
SSLVersions TLSv1.2
CertificateFile ~/.config/protonmail/bridge/cert.pem

IMAPStore remote
Account protonmail

#You can change .mail to something else
MaildirStore local
Path ~/.mail/
Inbox ~/.mail/INBOX/

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

Group protonmail
Channel inbox

For this to work you will need to make /protonmail/bridge/ inside /.config/; login to the protonmail bridge app and export cert.pem and move it inside /.config and described.

Next, you need to create an .authinfo file – i.e., mkdir ~/.authinfo and paste this:

Authentication

# -*- epa-file-encrypt-to: (<your-email>) -*-
machine 127.0.0.1 port 1143 login USERNAME@protonmail.com password BRIDGE_PASSWORD
machine 127.0.0.1 port 1025 login USERNAME@protonmail.com password BRIDGE_PASSWORD

You will need to encrypt this file and to do this you need to do this:

$ gpg -c .authinfo

And now delete .authinfo; you will now have an .authinfo.gpg

Generate gpg key

To be able to send an email though you will need to generate a gpg key associated with the email in the top of your .authinfo. Emacs will try to decrypt the file before it sends the email. To generate the key do this:

gpg --gen-key
gpg --armor --export-secret-keys <your-email> > mykey.asc

This is actually what the line # -*- epa-file-encrypt-to: (<your-email>) -*- is for on top of your .authinfo file.

Sync

Next, type this in the terminal:

Step 1. Initialize the message store:

$ mu init --maildir ~/.mail --my-address=<your-email>

Step 2. Index the messages – i.e., scan the messages in ~/.mail and store thes messages in a specical database:

$ mu index

Step 3. Fetch mail:

$ mbsync protonmail

Troubleshooting

It is possible that you stop using mu4e for a while and when you come back things are not in sync.

First, you need to put your gpg key into the Emacs buffer; you can do this by M-x epa-insert-keys; after this you may repeat the fetching step of the above.

If you type mbsync protonmail and you are not prompted to put in your password for decryption you may need to kill the gpg process; you can kill the gpg process by typing:

$ gpgconf --kill gpg-agent

You might also want to kill the mu process in some cases:

pkill -2 -u $UID mu

After you have killed the mu process you can now fetch the mail again.

mu4e emacs config

Finally, you need to configure your .emacs file; copy and paste the following:

(add-to-list 'load-path "/usr/share/emacs/site-lisp/mu4e/")
(require 'mu4e)

(setq mu4e-maildir "~/.mail"
mu4e-attachment-dir "~/Downloads")

(setq user-mail-address "<your-email>
user-full-name  <your-name>)

;; Get mail
(setq mu4e-get-mail-command "mbsync protonmail"
mu4e-change-filenames-when-moving t   ; needed for mbsync
mu4e-update-interval 120)             ; update every 2 minutes

;; Send mail
(setq message-send-mail-function 'smtpmail-send-it
smtpmail-auth-credentials "~/.authinfo.gpg"
smtpmail-smtp-server "127.0.0.1"
smtpmail-stream-type 'starttls
smtpmail-smtp-service 1025)

(add-to-list 'gnutls-trustfiles (expand-file-name "~/.config/protonmail/bridge/cert.pem"))

Basics of mu4e

To access your mu4e file you can use your package manager to tell you where mu is located. When I did this I got the file /usr/share/emacs/site-lisp/mu4e/. It could be slightly different on your machine. Now, mu4e is ready to be used which you can do by:

M-x mu4e

When you want to reply do this:

M-x mu4e-compose-reply

And to send the message:

C-c C-c to send the email

You can get a list of the mu4e commands by typing M-x mu4e- and typing enter.

You should look at the mu4e documentation here.

Also, if you want to setup mu4e for gmail check this tutorial here

Enjoy – happy programming