Skip to content

Instantly share code, notes, and snippets.

@areina
Created October 12, 2012 15:00
Show Gist options
  • Save areina/3879626 to your computer and use it in GitHub Desktop.
Save areina/3879626 to your computer and use it in GitHub Desktop.
Manage your email in emacs with mu4e

Manage your gmail account in emacs with mu4e

There're a lot of combinations to manage your email with emacs, but this works for me. I've a backup and I can manage my daily email.

The stack:

  • emacs
  • offlineimap
  • mu
  • mu4e

offlineimap

install

$ pacman -S offlineimap

configure

~/.offlineimaprc

[general]
accounts = Gmail
maxsyncaccounts = 1
pythonfile = ~/.offlineimap.py

[Account Gmail]
localrepository = Local
remoterepository = Remote

[Repository Local]
type = Maildir
localfolders = ~/Maildir

[Repository Remote]
type = Gmail
remoteuser = areina0@gmail.com
remotepasseval = get_password_emacs("imap.gmail.com", "areina0@gmail.com", "993")
realdelete = no

folderfilter = lambda foldername: foldername not in ['[Gmail]/Spam', '[Gmail]/All Mail', '[Gmail]/Starred', '[Gmail]/Important']

holdconnectionopen = true
keepalive = 60
sslcacertfile = /etc/ssl/certs/ca-certificates.crt

~/.offlineimap.py

#!/usr/bin/python
import re, os

def get_password_emacs(machine, login, port):
    s = "machine %s login %s port %s password ([^ ]*)\n" % (machine, login, port)
    p = re.compile(s)
    authinfo = os.popen("gpg -q --no-tty -d ~/.authinfo.gpg").read()
    return p.search(authinfo).group(1)

~/.authinfo

machine imap.gmail.com login areina0@gmail.com port 993 password blabla123bla456
machine smtp.gmail.com login areina0@gmail.com port 587 password blabla123bla456

With emacs, to encrypt this file:

  • M-x epa-encrypt-file (generate ~/.authinfo.gpg and remove original).

launch

$ offlineimap (here you can take a beer).

mu

Install

$ yaourt -S mu

Launch

$ mu index --maildir=~/Maildir

mu4e

mu4e is installed by default with mu package. Only that you needs is load it in emacs.

Configure

in your .emacs, ~/emacs.d/init.el or whatever.

Note:

  • To send mails with smtpmail.el and use gnutls, we need install the package (pacman -S gnutls)
(require 'mu4e)

;; default
(setq mu4e-maildir (expand-file-name "~/Maildir"))

(setq mu4e-drafts-folder "/[Gmail].Drafts")
(setq mu4e-sent-folder   "/[Gmail].Sent Mail")
(setq mu4e-trash-folder  "/[Gmail].Trash")

;; don't save message to Sent Messages, GMail/IMAP will take care of this
(setq mu4e-sent-messages-behavior 'delete)

;; setup some handy shortcuts
(setq mu4e-maildir-shortcuts
      '(("/INBOX"             . ?i)
        ("/[Gmail].Sent Mail" . ?s)
        ("/[Gmail].Trash"     . ?t)))

;; allow for updating mail using 'U' in the main view:
(setq mu4e-get-mail-command "offlineimap")

;; something about ourselves
;; I don't use a signature...
(setq
 user-mail-address "areina0@gmail.com"
 user-full-name  "Toni Reina"
 ;; message-signature
 ;;  (concat
 ;;    "Foo X. Bar\n"
 ;;    "http://www.example.com\n")
)

;; sending mail -- replace USERNAME with your gmail username
;; also, make sure the gnutls command line utils are installed
;; package 'gnutls-bin' in Debian/Ubuntu, 'gnutls' in Archlinux.

(require 'smtpmail)

(setq message-send-mail-function 'smtpmail-send-it
      starttls-use-gnutls t
      smtpmail-starttls-credentials
      '(("smtp.gmail.com" 587 nil nil))
      smtpmail-auth-credentials
      (expand-file-name "~/.authinfo.gpg")
      smtpmail-default-smtp-server "smtp.gmail.com"
      smtpmail-smtp-server "smtp.gmail.com"
      smtpmail-smtp-service 587
      smtpmail-debug-info t)
@kuremu
Copy link

kuremu commented Oct 1, 2013

If you use Homebrew on Mac OS X you can install curl-ca-bundle and then use the following

sslcacertfile = /usr/local/opt/curl-ca-bundle/share/ca-bundle.crt

@Eagleeye777
Copy link

I have a question here: Trying to set this up as you suggested. Can't run the pythonfile with the option no-tty though.

Gpg complains and the passphrase can of course not be enterd. Clearing that allows me to use offlneimap from shell, but does not run when called by mu4e, because I cannot confirm the entered passphrase for some weird reason.

Any idea how to fix this?

@justyns
Copy link

justyns commented Oct 8, 2013

Also using Homebrew, I had to install mu with this:
EMACS=$(which emacs) brew install mu --with-emacs --HEAD

Otherwise it was not installing mu4e, just mu.

@cedricpinson
Copy link

Hi I have the same problem for the Can't run the pythonfile with the option no-tty though. So I cant enter the passphrase. Any idea how to fix this ?

@ayman
Copy link

ayman commented Feb 18, 2014

On Mac OS X, you can use the system keychain to store the passphrase; so the python line becomes:

authinfo = os.popen("gpg -q -d --no-mdc-warning --no-tty --passphrase `security find-generic-password -a authinfo -s offlineimap -w` ~/.authinfo.gpg").read()

@jwintz
Copy link

jwintz commented Feb 19, 2014

@ayman, that solves that mac issue indeed ! Thanks ! Just to make it clear, that involves to create a password in the key chain app with offlineimap as service and authinfo as authorisation, plus the gpg encryption key as a password.

@bhuvankrishna
Copy link

I used this method to configure offlineimap on debian. Keeping --no-tty gives an error "gpg: Sorry, no terminal at all requested - can't get input" so I removed --no-tty and I get paraphrase prompt solving the issue. I thought this will help some one trying to configure in debian.

@thomas-louvigne
Copy link

Hi, do you know how to get the email in html view ?

@peterwvj
Copy link

peterwvj commented Jun 5, 2016

@cedricpinson I had the same problem. If you change the python script to use gpg2 instead then the system will ask you for the password.

gpg2 -q --no-tty -d ~/.authinfo.gpg

To install it:

sudo apt-get install gnupg2

@dadair-ca
Copy link

dadair-ca commented Sep 9, 2016

Following these instructions, I've had to change (on OSX 10.11.6, with gpg installed using brew)

authinfo = os.popen("gpg -q -d --no-mdc-warning --no-tty --passphrase `security find-generic-password -a authinfo -s offlineimap -w` ~/.authinfo.gpg").read()

to

authinfo = os.popen("gpg -q -d --no-mdc-warning --no-tty --batch --passphrase `security find-generic-password -a authinfo -s offlineimap -w` ~/.authinfo.gpg").read()

As per man gpg, the --passphrase flag can only be used if --batch has also been provided.

@raksugi
Copy link

raksugi commented May 30, 2017

@peterwvj Thanks for the tip. That worked for me.

@charignon
Copy link

@paulodder
Copy link

thanks for the guide! really helpful.
mu index --maildir=~/Maildir does not work as of mu 1.3.8, instead first run:
mu init --maildir=~/Maildir and then mu index

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment