Jump to content
Aerosol

[Emacs] mu4e + offlineimap multiply accounts

Recommended Posts

Posted

I'm using Emacs every day, almost for all, in fact i need only two user-level applications at my computer, they are Emacs and web browser (despite on eww in Emacs 25). I use Emacs almost for all, for text editing, for chatting in irc, email reading/writing, files manipulations, as my TODO list and many many more (and this blog post was written in Emacs too). Today i want to tell how to use Emacs for email handling. I will show how to install mu4e and offlineimap and configure Eemacs for handling emails from multiply accounts with this tools in ubuntu-14.10. If you're interesting read next.

Installing

Emacs

Of course first of all you must have installed Emacs on your computer. You can download it from here and build it from source. Or you just can use apt-get package manager and install Emacs with:

sudo apt-get install Emacs

mu4e

mu4e it is a emacs-based e-mail client. You can install it with following commands:

sudo apt-get install html2text xdg-utils libgmime-2.6-dev libxapian-dev
git clone https://github.com/djcb/mu
cd mu && autoreconf -i && ./configure && make
sudo make install

After these commands execution mu and mu4e should be installed.

offlineimap

OfflineIMAP is a Python utility to sync mail from IMAP servers. We can install it with:

sudo apt-get install offlineimap

offlineimap configuration

After we have installed all software, we can start to configurate it. Let's start from offlineimap configuration. As i wrote about this post will be about multiply accounts configuration, I personally have two email accounts for work and personal. Let's call it Work and Personal. For offlineimap configuration open/create ~/.offlineimaprc file. Offlineimap configuration file consists from sections and key value records separated with = symbol. Let's add first section to ~/.offlieimaprc:

[general]
accounts = Work, Personal
maxsyncaccounts = 3

First sectrion is general. It contains two values: first is 'accounts', we declare names for our accounts. Second is 'maxsyncaccounts', it controls how many accounts may be synced simultaneously. Next we'll define sections for first 'Work' account:

[Repository WorkLocal]
type = Maildir
localfolders = ~/Maildir/Work

[Repository WorkRemote]
remotehost = host
remoteuser = user@email.com
remotepass = password
type = IMAP
ssl = yes
sslcacertfile = /etc/ssl/certs/ca-certificates.crt
holdconnectionopen = true
keepalive = 120
realdelete = yes

There are 3 sections for remote and local repository. First [Account Work] defines local and remote repository. Second section defines type of mail directory, it is a Maildir and path to it. Third section defines remote host parameters as user email, user host, protocol type (IMAP), ssl and etc... Of course you will need to change some parameters to your own values. After this we must define the same 3 sections for Personal account, it will contain the same fields as first, like this:

[Account Personal]
localrepository = Personal
remoterepository = Personal

[Repository PersonalMailLocal]
type = Maildir
localfolders = ~/Maildir/Personal

[Repository PersonalMailRemote]
type = IMAP
remotehost = imap.gmail.com
remoteuser = email@gmail.com
remotepass = password
ssl = yes
sslcacertfile = /etc/ssl/certs/ca-certificates.crt
keepalive = 120
realdelete = yes
holdconnectionopen = true

Here we can see the same sections and fields as for first account, little difference that it configured for gmail IMAP server. After this go to you terminal and execute:

mkdir -p ~/Maildir/Work
mkdir -p ~/Maildir/Personal
cd /home/user
offlineimap

It will fetches all mail from your mail servers, so you can work with it in Emacs.

Emacs configuration

As we have installed Emacs, mu4e and also we have configured offlineimap, we can go to Emacs configuration. First of all you need to tell Emacs where to load mu4e and enable it and smtpmail package with:

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

(require 'mu4e)
(require 'smtpmail)

Now we define some general variables for both accounts with:

(setq
;; general
mu4e-get-mail-command "offlineimap"
mu4e-update-interval 300

;; smtp
message-send-mail-function 'smtpmail-send-it
smtpmail-stream-type 'starttls

;; keybindings
mu4e-maildir-shortcuts
'(("/Personal/INBOX" . ?k)
("/Personal/[Gmail].Trash" . ?t)

("/Work/INBOX" . ?w)
("/Work/Trash" . ?f)

; attachment dir
mu4e-attachment-dir "~/Downloads"

; insert sign
mu4e-compose-signature-auto-include 't
)

As i said here we define general parameters for our both accounts, they are program for fetching email (offlineimap in our case), updating interval, what we'll use for email sending (smtpmail in our case), maildir shortcuts (we'll describe it later), directory for attachment, and email message signature auto-inserting. Now we must add yet variables to define fields which are different for our 2 accounts:

(defvar my-mu4e-account-alist
'(("Personal"
;; about me
(user-mail-address "user@gmail.com")
(user-full-name "Name")
(mu4e-compose-signature "Best regards.\n0xAX")
;; smtp
(smtpmail-stream-type starttls)
(smtpmail-starttls-credentials '(("smtp.gmail.com" 587 nil nil)))
(smtpmail-auth-credentials '(("smtp.gmail.com" 587 "user@gmail.com" nil)))
(smtpmail-default-smtp-server "smtp.gmail.com")
(smtpmail-smtp-server "smtp.gmail.com")
(smtpmail-smtp-service 587))
("Work"
;; about me
(user-mail-address "0xAX@work.com")
(user-full-name "0xAX")
(mu4e-compose-signature "0xAX")

;; smtp
(smtpmail-stream-type starttls)
(smtpmail-starttls-credentials '(("imap.work.net" 25 nil nil)))
(smtpmail-auth-credentials '(("imap.work.net" 25 "0xAX@work.com" nil)))
(smtpmail-default-smtp-server "imap.work.net")
(smtpmail-smtp-service 25)
)))

They are following fields: settings for you smtp servers, signatures and user info. And add one function for choosing account before message composing:

;;
;; Found here - http://www.djcbsoftware.nl/code/mu/mu4e/Multiple-accounts.html
;;
(defun my-mu4e-set-account ()
"Set the account for composing a message."
(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 (car 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)

Technically that's all, but you can add some more customization like date formating, image displaying and etc...:

(setq message-citation-line-format "%N @ %Y-%m-%d %H:%M %Z:\n")
(setq message-citation-line-function 'message-insert-formatted-citation-line)
(setq mu4e-view-show-addresses 't)

(setq mu4e-headers-fields
'( (:date . 25)
(:flags . 6)
(:from . 22)
(:subject . nil)))

(setq mu4e-show-images t)

(when (fboundp 'imagemagick-register-types)
(imagemagick-register-types))

(setq mu4e-view-prefer-html t)
(setq mu4e-html2text-command "html2text -utf8 -width 72")
(setq mail-user-agent 'mu4e-user-agent)

Usage

Ok, we configured emacs, mu4e and offlineimap. Time to use it. First of all i have added keybinding for openning mu4e:

(global-set-key [f1]  'mu4e)

Of course you can choose another key combination. Now after pressing F1, i see mu4e main window:

emacs.png

It is mu4e main view in Emacs. You can do following actions here:

  • U - update email for both accounts
  • q - exit from mu4e
  • j-k - go to personal inbox (remember we defined keybindings in mu4e-maildir-shortcuts
  • j-w - go to work inbox
  • C - compose new message, it will ask you from what account you want to send message

Conclusion

This is the end. In this post i told how to configure Emacs + mu4e + offlineimap for email handling. As a longtime Emacs user I'm very interesting how and for what do you using Emacs, what extensions and what tasks Emacs helps to solve you, write me a comment about it. Hope this post was useful for you.

Source

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...