Mu4e: More robust Gmail account checking

Emails sent to an @gmail.com account may have been forwarded to a non-gmail mailbox.
Thus the current approach is over-eager.

Only checking @gmail on send, and then matching a user's maildir (not email) with
received messages is a more robust approach.

To accommodate this we switch `+mu4e-gmail-addresses' out for `+mu4e-gmail-accounts'.

Along the way `mu4e-index-cleanup' and `mu4e-index-lazy-check' customisations were
moved into the readme as a recommendation for Gmail-only users, to avoid causing
adverse effects for users who have non-gmail accounts too.
This commit is contained in:
TEC 2021-01-17 13:49:10 -06:00
parent 611e532d38
commit 2307ae5644
No known key found for this signature in database
GPG key ID: 779591AFDB81F06C
2 changed files with 39 additions and 32 deletions

View file

@ -173,10 +173,30 @@ Then configure Emacs to use your email address:
With the =+gmail= flag, integrations are applied which account for the different
behaviour of Gmail.
The integrations are applied when using addresses which contain =@gmail.com= or
have =gmail= in the maildir name. You can use ~+mu4e-gmail-addresses~ when you want
an address to be treated as such but it meets neither conditions (e.g. with
Gsuite).
The integrations are applied to addresses with /both/ "@gmail.com" in the
account address and "gmail" in the account maildir, as well as accounts listed
in ~+mu4e-gmail-accounts~. Any domain can be specified, so G Suite accounts can
benefit from the integrations:
#+begin_src emacs-lisp
;; if "gmail" is missing from the address or maildir, the account must be listed here
(setq +mu4e-gmail-accounts '(("hlissner@gmail.com" . "/hlissner")
("example@example.com" . "/example")))
#+end_src
If you only use Gmail, you can improve performance due to the way Gmail
presents messages over IMAP:
#+begin_src emacs-lisp
;; don't need to run cleanup after indexing for gmail
(setq mu4e-index-cleanup nil
;; because gmail uses labels as folders we can use lazy check since
;; messages don't really "move"
mu4e-index-lazy-check t)
#+end_src
Also, note that Gmail's IMAP settings must have "When I mark a message in IMAP
as deleted: Auto-Expunge off - Wait for the client to update the server." and
"When a message is marked as deleted and expunged from the last visible IMAP
folder: Move the message to the trash" for the integrations to work as expected.
** OrgMsg
With the =+org= flag, =org-msg= is installed, and ~org-msg-mode~ is enabled before

View file

@ -387,45 +387,32 @@ Must be set before org-msg is loaded to take effect.")
(when (featurep! +gmail)
(after! mu4e
(defvar +mu4e-gmail-addresses nil
"A list of email addresses which, despite not:
- having '@gmail.com' in them, or
- being in a maildir where the name includes 'gmail'
(defvar +mu4e-gmail-accounts nil
"Gmail accounts that do not contain \"gmail\" in address and maildir.
Should be treated as a gmail address.")
An alist of Gmail addresses of the format \((\"username@domain.com\" . \"account-maildir\"))
to which Gmail integrations (behind the `+gmail' flag of the `mu4e' module) should be applied.
See `+mu4e-msg-gmail-p' and `mu4e-sent-messages-behavior'.")
;; don't save message to Sent Messages, Gmail/IMAP takes care of this
(setq mu4e-sent-messages-behavior
(lambda () ;; TODO make use +mu4e-msg-gmail-p
(if (or (string-match-p "@gmail.com\\'" (message-sendmail-envelope-from))
(member (message-sendmail-envelope-from) +mu4e-gmail-addresses))
'delete 'sent))
;; don't need to run cleanup after indexing for gmail
mu4e-index-cleanup nil
;; because gmail uses labels as folders we can use lazy check since
;; messages don't really "move"
mu4e-index-lazy-check t)
(member (message-sendmail-envelope-from)
(mapcar #'car +mu4e-gmail-accounts)))
'delete 'sent)))
(defun +mu4e-msg-gmail-p (msg)
(or
(string-match-p "@gmail.com"
(cond
((member (mu4e-message-field msg :to)
(append (mu4e-personal-addresses)
+mu4e-gmail-addresses))
(mu4e-message-field msg :to))
((member (mu4e-message-field msg :from)
(append (mu4e-personal-addresses)
+mu4e-gmail-addresses))
(mu4e-message-field msg :from))
(t "")))
(string-match-p "gmail" (mu4e-message-field msg :maildir))))
(let ((root-maildir
(replace-regexp-in-string "/.*" ""
(substring (mu4e-message-field msg :maildir) 1))))
(or (string-match-p "gmail" root-maildir)
(member root-maildir (mapcar #'cdr +mu4e-gmail-accounts)))))
;; In my workflow, emails won't be moved at all. Only their flags/labels are
;; changed. Se we redefine the trash and refile marks not to do any moving.
;; However, the real magic happens in `+mu4e|gmail-fix-flags'.
;; However, the real magic happens in `+mu4e-gmail-fix-flags-h'.
;;
;; Gmail will handle the rest.
(defun +mu4e--mark-seen (docid _msg target)