Skip to content

Instantly share code, notes, and snippets.

@vincenzopalazzo
Forked from kidd/emacs-email-setup.md
Created November 30, 2022 16:05
Show Gist options
  • Save vincenzopalazzo/6d869536a05cd00005ec8c077f1a5215 to your computer and use it in GitHub Desktop.
Save vincenzopalazzo/6d869536a05cd00005ec8c077f1a5215 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 = [email protected]
remotepasseval = get_password_emacs("imap.gmail.com", "[email protected]", "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 [email protected] port 993 password blabla123bla456
machine smtp.gmail.com login [email protected] port 587 password blabla123bla456

With emacs, to encrypt this file:

  • M-x epa-encrypt-file (generate ~/.authinfo.gpg and remove original). If you don't have a pgp key, create one with
gpg --gen-key

If you have issues with "gpg: Sorry, no terminal at all requested - can't get input", try to remove the passphrase on your key. It may help.

Take a look at the .offlineimap.py code, and make sure that the regex will match.

If you already had your .authinfo, it might be a bit different, like not having the port number 587 but 'smtp'. or 'areina0' instead of [email protected]. it works both ways, but make it consistent :).

Original code in http://quotenil.com/OfflineIMAP-with-Encrypted-Authinfo.html

launch

$ offlineimap (here you can take a beer) (EDIT: Or 17 beers).

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 "[email protected]"
 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment