概述

用 offlineimap 收取邮件,用 mu4e 在 emacs 中管理邮件,本教程配置多账户。

offlineimap 设置

安装 offlineimap

本人用 archlinux,直接 sudo pacman -S offlineimap 即可

配置 offlineimap

在 home 目录下建立 .offlineimaprc 文件,加入以下代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- mode: python2 -*-
[general]
# accounts = Gmail, Foxmail, Lengyue-163
accounts = Gmail, Foxmail
maxsyncaccounts = 2
pythonfile = ~/.offlineimap.py

[Account Gmail]
localrepository = Gmail-Local
remoterepository = Gmail-Remote

[Repository Gmail-Local]
type = Maildir
localfolders = ~/Documents/Mu4e/Gmail

[Repository Gmail-Remote]
type = Gmail
auth_mechanisms = LOGIN
remotehost = imap.gmail.com
remoteuser = xxxxx@gmail.com
remotepass = xxxxx
ssl = true
sslcacertfile = /etc/ssl/certs/ca-certificates.crt
nametrans = lambda foldername: foldername.decode('imap4-utf-7').encode('utf-8')
folderfilter = lambda folder: not re.search('(^Spam$|All)', folder)
maxconnections = 2
realdelete = yes


[Account Foxmail]
localrepository = Foxmail-Local
remoterepository = Foxmail-Remote

[Repository Foxmail-Local]
type = Maildir
localfolders = ~/Documents/Mu4e/Foxmail

[Repository Foxmail-Remote]
type = IMAP
remotehost = imap.qq.com
remoteuser = xxxxx@qq.com
remotepass = xxxxx
ssl = true
sslcacertfile = /etc/ssl/certs/ca-certificates.crt
nametrans = lambda foldername: foldername.decode('imap4-utf-7').encode('utf-8')
folderfilter = lambda foldername: foldername in ['INBOX','Drafts', 'Sent Messages', 'Deleted Messages']
maxconnections = 2
realdelete = no

设置 gmail 和 qq 两个账户,按照上面配置,邮箱和密码改成自己的(首先开通 gmail 和 qq 邮箱的 imap 功能,设置授权码,可参照后面参考资料)。 其中,比较重要的有以下几项:

localfolders 是下载邮件的位置

nametrans = lambda foldername: foldername.decode('imap4-utf-7').encode('utf-8') 防止中文乱码

folderfilter = lambda foldername: foldername in ['INBOX','Drafts', 'Sent Messages', 'Deleted Messages'] 选择自己同步的文件夹

folderfilter = lambda folder: not re.search('(^Spam$|All)', folder) 不选择 Spam 和有 All 字样的文件夹同步 2

同时,在 home 目录下家里 .offlineimap.py 文件,加入以下内容,防止乱码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python2

import binascii
import codecs

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

def modified_base64(s):
    s = s.encode('utf-16be')
    return binascii.b2a_base64(s).rstrip('\n=').replace('/', ',')

def doB64(_in, r):
    if _in:
        r.append('&%s-' % modified_base64(''.join(_in)))
        del _in[:]

def encoder(s):
    r = []
    _in = []
    for c in s:
        ordC = ord(c)
        if 0x20 <= ordC <= 0x25 or 0x27 <= ordC <= 0x7e:
            doB64(_in, r)
            r.append(c)
        elif c == '&':
            doB64(_in, r)
            r.append('&-')
        else:
            _in.append(c)
    doB64(_in, r)
    return (str(''.join(r)), len(s))

# decoding

def modified_unbase64(s):
    b = binascii.a2b_base64(s.replace(',', '/') + '===')
    return unicode(b, 'utf-16be')

def decoder(s):
    r = []
    decode = []
    for c in s:
        if c == '&' and not decode:
            decode.append('&')
        elif c == '-' and decode:
            if len(decode) == 1:
                r.append('&')
            else:
                r.append(modified_unbase64(''.join(decode[1:])))
            decode = []
        elif decode:
            decode.append(c)
        else:
            r.append(c)
    if decode:
        r.append(modified_unbase64(''.join(decode[1:])))
    bin_str = ''.join(r)
    return (bin_str, len(s))

class StreamReader(codecs.StreamReader):
    def decode(self, s, errors='strict'):
        return decoder(s)

class StreamWriter(codecs.StreamWriter):
    def decode(self, s, errors='strict'):
        return encoder(s)

def imap4_utf_7(name):
    if name == 'imap4-utf-7':
        return (encoder, decoder, StreamReader, StreamWriter)
codecs.register(imap4_utf_7)

用 offlineimap 下载邮件

在终端用运行命令 offlineimap ,下载邮件

Mu4e 设置

我用 spacemacs,先将 mu4e layer 加入 spaemacs 配置文件中,启用 mu4e layer。

安装 mu4e 是 mu 软件的一部分,因此安装 mu 软件(yaourt mu)。

安装完成之后,建立索引,在终端运行 mu index --rebuild --maildir=~/Documents/Mu4e ,文件夹的位置根据自己的更改。

在 spacemacs 的配置文件中加入以下代码,注意:根据自己的邮箱个数等更改相应位置即可。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
(add-to-load-path "~/.spacemacs.d/package/mu4e")

(require 'mu4e)

(setq mu4e-account-alist
      '(("Gmail"
         ;; Under each account, set the account-specific variables you want.
         (mu4e-sent-messages-behavior delete)
         (mu4e-sent-folder "/Gmail/[Gmail].Sent Mail")
         (mu4e-drafts-folder "/Gmail/[Gmail].Drafts")
         (user-mail-address "xxxxx@gmail.com")
         (user-full-name "xxxxx"))
        ("Foxmail"
         (mu4e-sent-messages-behavior sent)
         (mu4e-sent-folder "/Foxmail/Sent Messages")
         (mu4e-drafts-folder "/Foxmail/Drafts")
         (user-mail-address "xxxxx@foxmail.com")
         (user-full-name "xxxxx"))
        ;; ("Lengyue-163"
        ;;  (mu4e-sent-messages-behavior sent)
        ;;  (mu4e-sent-folder "/Lengyue-163/Sent Items")
        ;;  (mu4e-drafts-folder "/Lengyue-163/Drafts")
        ;;  (user-mail-address "zanghuahong@163.com")
        ;;  (user-full-name "Mao Xiaowei"))
       )
)

(mu4e/mail-account-reset)

;;; Set up some common mu4e variables
(setq mu4e-maildir "~/Documents/Mu4e"
      mu4e-trash-folder "/Gmail/Trash"
      mu4e-refile-folder "/Gmail/Archive"
      ;; mu4e-get-mail-command "mbsync -a"
      mu4e-update-interval nil
      mu4e-compose-signature-auto-include nil
      mu4e-view-show-images t
      mu4e-view-show-addresses t)

;;; Mail directory shortcuts
(setq mu4e-maildir-shortcuts
      '(
        ("/Foxmail/INBOX" . ?f)
        ("/Foxmail/Drafts" . ?d)
        ("/Foxmail/Sent Messages" . ?s)
        ("/Gmail/INBOX" . ?g)
        ;; ("/Gmail/[Gmail].All Mail" . ?a)
        ("/Gmail/[Gmail].Drafts" . ?r)
        ("/Gmail/[Gmail].Sent Mail" . ?m)
        ("/Gmail/[Gmail].Trash" . ?t)
        ;; ("/Lengyue-163/INBOX" . ?i)
        ))

;;; Bookmarks
(setq mu4e-bookmarks
      `(("flag:unread AND NOT flag:trashed" "Unread messages" ?u)
        ("date:today..now" "Today's messages" ?t)
        ("date:7d..now" "Last 7 days" ?w)
        ("mime:image/*" "Messages with images" ?p)
        (,(mapconcat 'identity
                     (mapcar
                      (lambda (maildir)
                        (concat "maildir:" (car maildir)))
                      mu4e-maildir-shortcuts) " OR ")
         "All inboxes" ?i)))

(setq mu4e-enable-notifications t)

(with-eval-after-load 'mu4e-alert
  ;; Enable Desktop notifications
  ;; (mu4e-alert-set-default-style 'notifications)) ; For linux
  (mu4e-alert-set-default-style 'libnotify))  ; Alternative for linux
  ;; (mu4e-alert-set-default-style 'notifier))   ; For Mac OSX (through the
                                        ; terminal notifier app)
;; (mu4e-alert-set-default-style 'growl))      ; Alternative for Mac OSX

(setq mu4e-enable-mode-line t)

(setq mu4e-get-mail-command "offlineimap")
;; Fetch mail in 60 sec interval
(setq mu4e-update-interval 60)

(require 'mu4e-contrib)
(setq mu4e-html2text-command 'mu4e-shr2text)
;; try to emulate some of the eww key-bindings
(add-hook 'mu4e-view-mode-hook
          (lambda ()
            (local-set-key (kbd "<tab>") 'shr-next-link)
            (local-set-key (kbd "<backtab>") 'shr-previous-link)))

;; something about ourselves
(require 'smtpmail)
(setq user-mail-address "xxxxx@foxmail.com"
      user-full-name "xxxxx"
      smtpmail-stream-type 'starttls
      starttls-use-gnutls t
      mu4e-compose-signature
      (concat
       "xxxx\n"
       "Blog: http://lengyueyang.github.io\n"
       "\n")
      mu4e-compose-signature-auto-include t
      )

(setq send-mail-function            'smtpmail-send-it
      message-send-mail-function    'smtpmail-send-it
      smtpmail-auth-credentials     (expand-file-name "~/.authinfo")
      smtpmail-stream-type          'tls
      smtpmail-smtp-server          "smtp.qq.com"
      smtpmail-smtp-service         465
      smtpmail-smtp-user "xxxxx@qq.com")

(setq message-kill-buffer-on-exit t)

;; save attachment to my desktop (this can also be a function)
(setq mu4e-attachment-dir "/home/lengyue/Documents/Mu4e/Attachment")

几点说明:

mu 更新后,发现系统不能自己找到 mu4e 文件夹,因此手动链接 (add-to-load-path "~/.spacemacs.d/package/mu4e")

contact 内容是自己的邮件签名

Mu4e 简单使用小结

Keybindings to remember of Mu4e

offlineimap and mu index --maildir=~/Documents/Mu4e

相关快捷键请看官方文档

参考资料