From 21ab425b4b3ebeb082af3b888dedb5f7c571d990 Mon Sep 17 00:00:00 2001 From: Derek Taylor Date: Sat, 19 Dec 2020 00:46:34 -0600 Subject: [PATCH] Configuring multiple emails in mu4e. --- .doom.d/config.el | 91 +++++++++++++++++++++++++++++------- .doom.d/config.org | 112 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 171 insertions(+), 32 deletions(-) diff --git a/.doom.d/config.el b/.doom.d/config.el index 35f990f..6dae012 100644 --- a/.doom.d/config.el +++ b/.doom.d/config.el @@ -180,26 +180,85 @@ ;; Be sure that the following command is: "mbsync -c ~/.config/mu4e/mbsyncrc -a" mu4e-get-mail-command "mbsync -c ~/.config/mu4e-dt/mbsyncrc -a" mu4e-update-interval 300 - mu4e-compose-signature - (concat - "Derek Taylor\n" - "http://www.youtube.com/DistroTube\n") + mu4e-main-buffer-hide-personal-addresses t message-send-mail-function 'smtpmail-send-it starttls-use-gnutls t smtpmail-starttls-credentials '(("smtp.1and1.com" 587 nil nil)) - smtpmail-smtp-user "derek@distrotube.com" - smtpmail-default-smtp-server "smtp.1and1.com" - smtpmail-smtp-server "smtp.1and1.com" - smtpmail-smtp-service 587 - mu4e-sent-folder "/Sent" - mu4e-drafts-folder "/Drafts" - mu4e-trash-folder "/Trash" - mu4e-refile-folder "/All Mail" + mu4e-sent-folder "/account-1/Sent" + mu4e-drafts-folder "/account-1/Drafts" + mu4e-trash-folder "/account-1/Trash" mu4e-maildir-shortcuts - '(("/derek-distrotube/Inbox" . ?i) - ("/derek-distrotube/Sent" . ?s) - ("/derek-distrotube/All Mail" . ?a) - ("/derek-distrotube/Trash" . ?t))) + '(("/account-1/Inbox" . ?i) + ("/account-1/Sent Items" . ?s) + ("/account-1/Drafts" . ?d) + ("/account-1/Trash" . ?t))) + +;; (defvar my-mu4e-account-alist +;; '(("acc1-domain" +;; (mu4e-sent-folder "/acc1-domain/Sent") +;; (mu4e-drafts-folder "/acc1-domain/Drafts") +;; (mu4e-trash-folder "/acc1-domain/Trash") +;; (mu4e-compose-signature +;; (concat +;; "Ricky Bobby\n" +;; "acc1@domain.com\n")) +;; (user-mail-address "acc1@domain.com") +;; (smtpmail-default-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-user "acc1@domain.com") +;; (smtpmail-stream-type starttls) +;; (smtpmail-smtp-service 587)) +;; ("acc2-domain" +;; (mu4e-sent-folder "/acc2-domain/Sent") +;; (mu4e-drafts-folder "/acc2-domain/Drafts") +;; (mu4e-trash-folder "/acc2-domain/Trash") +;; (mu4e-compose-signature +;; (concat +;; "Suzy Q\n" +;; "acc2@domain.com\n")) +;; (user-mail-address "acc2@domain.com") +;; (smtpmail-default-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-user "acc2@domain.com") +;; (smtpmail-stream-type starttls) +;; (smtpmail-smtp-service 587)) +;; ("acc3-domain" +;; (mu4e-sent-folder "/acc3-domain/Sent") +;; (mu4e-drafts-folder "/acc3-domain/Drafts") +;; (mu4e-trash-folder "/acc3-domain/Trash") +;; (mu4e-compose-signature +;; (concat +;; "John Boy\n" +;; "acc3@domain.com\n")) +;; (user-mail-address "acc3@domain.com") +;; (smtpmail-default-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-user "acc3@domain.com") +;; (smtpmail-stream-type starttls) +;; (smtpmail-smtp-service 587)))) + +(load "~/.doom.d/email") + +(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 (caar 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) (after! neotree (setq neo-smart-open t diff --git a/.doom.d/config.org b/.doom.d/config.org index cb35df5..702f934 100644 --- a/.doom.d/config.org +++ b/.doom.d/config.org @@ -20,6 +20,9 @@ - [[#line-settings][LINE SETTINGS]] - [[#mastodon][MASTODON]] - [[#mu4e][MU4E]] + - [[#setting-up-environment-with-main-account][Setting Up Environment With Main Account]] + - [[#setting-up-multiple-accounts][Setting Up Multiple Accounts]] + - [[#function-to-facilitate-switching-between-accounts][Function To Facilitate Switching Between Accounts]] - [[#neotree][NEOTREE]] - [[#open-specific-files][OPEN SPECIFIC FILES]] - [[#org-mode][ORG MODE]] @@ -329,6 +332,9 @@ Mastodon.el is a mastodon client for Emacs. Note that I wrapped my settings wit * MU4E Setting up mu4e which is an email client that works within emacs. You must install mu4e and mbsync through your Linux distribution's package manager. Setting up smtp for sending mail. Make sure the gnutls command line utils are installed. Package 'gnutls-bin' in Debian/Ubuntu, and 'gnutls' in Arch. +** Setting Up Environment With Main Account +I will be setting up mu4e to use three different email addresses. This source block contains default settings and fallback settings. + #+BEGIN_SRC emacs-lisp (add-to-list 'load-path "/usr/local/share/emacs/site-lisp/mu4e") ;;(require 'smtpmail) @@ -339,26 +345,100 @@ Setting up mu4e which is an email client that works within emacs. You must inst ;; Be sure that the following command is: "mbsync -c ~/.config/mu4e/mbsyncrc -a" mu4e-get-mail-command "mbsync -c ~/.config/mu4e-dt/mbsyncrc -a" mu4e-update-interval 300 - mu4e-compose-signature - (concat - "Derek Taylor\n" - "http://www.youtube.com/DistroTube\n") + mu4e-main-buffer-hide-personal-addresses t message-send-mail-function 'smtpmail-send-it starttls-use-gnutls t smtpmail-starttls-credentials '(("smtp.1and1.com" 587 nil nil)) - smtpmail-smtp-user "derek@distrotube.com" - smtpmail-default-smtp-server "smtp.1and1.com" - smtpmail-smtp-server "smtp.1and1.com" - smtpmail-smtp-service 587 - mu4e-sent-folder "/Sent" - mu4e-drafts-folder "/Drafts" - mu4e-trash-folder "/Trash" - mu4e-refile-folder "/All Mail" + mu4e-sent-folder "/account-1/Sent" + mu4e-drafts-folder "/account-1/Drafts" + mu4e-trash-folder "/account-1/Trash" mu4e-maildir-shortcuts - '(("/derek-distrotube/Inbox" . ?i) - ("/derek-distrotube/Sent" . ?s) - ("/derek-distrotube/All Mail" . ?a) - ("/derek-distrotube/Trash" . ?t))) + '(("/account-1/Inbox" . ?i) + ("/account-1/Sent Items" . ?s) + ("/account-1/Drafts" . ?d) + ("/account-1/Trash" . ?t))) + +#+END_SRC + +** Setting Up Multiple Accounts +The settings below are specific to each of three different email accounts. These settings are fictional and are here for documentation purposes. Hence, I have this source block entirely commmented out. Now, I do use a source code block similar to the one below, but I don't want to share it publicly. Keep reading to see how I handle this. + +#+BEGIN_SRC emacs-lisp +;; (defvar my-mu4e-account-alist +;; '(("acc1-domain" +;; (mu4e-sent-folder "/acc1-domain/Sent") +;; (mu4e-drafts-folder "/acc1-domain/Drafts") +;; (mu4e-trash-folder "/acc1-domain/Trash") +;; (mu4e-compose-signature +;; (concat +;; "Ricky Bobby\n" +;; "acc1@domain.com\n")) +;; (user-mail-address "acc1@domain.com") +;; (smtpmail-default-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-user "acc1@domain.com") +;; (smtpmail-stream-type starttls) +;; (smtpmail-smtp-service 587)) +;; ("acc2-domain" +;; (mu4e-sent-folder "/acc2-domain/Sent") +;; (mu4e-drafts-folder "/acc2-domain/Drafts") +;; (mu4e-trash-folder "/acc2-domain/Trash") +;; (mu4e-compose-signature +;; (concat +;; "Suzy Q\n" +;; "acc2@domain.com\n")) +;; (user-mail-address "acc2@domain.com") +;; (smtpmail-default-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-user "acc2@domain.com") +;; (smtpmail-stream-type starttls) +;; (smtpmail-smtp-service 587)) +;; ("acc3-domain" +;; (mu4e-sent-folder "/acc3-domain/Sent") +;; (mu4e-drafts-folder "/acc3-domain/Drafts") +;; (mu4e-trash-folder "/acc3-domain/Trash") +;; (mu4e-compose-signature +;; (concat +;; "John Boy\n" +;; "acc3@domain.com\n")) +;; (user-mail-address "acc3@domain.com") +;; (smtpmail-default-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-server "smtp.domain.com") +;; (smtpmail-smtp-user "acc3@domain.com") +;; (smtpmail-stream-type starttls) +;; (smtpmail-smtp-service 587)))) +#+END_SRC + +I'm sourcing an elisp file (~/.doom.d/email) that contains the above source block but with my actual email settings. I do this so I don't have to share my email addresses (other than derek@distrotube.com) publicly. If you uncommented the above source block to use, then you should comment out or delete this block below. +#+BEGIN_SRC emacs-lisp +(load "~/.doom.d/email") + +#+END_SRC + +** Function To Facilitate Switching Between Accounts +The following function can be used to select an account. This function then needs to be added to mu4e-compose-pre-hook, + +#+BEGIN_SRC emacs-lisp +(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 (caar 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) #+END_SRC * NEOTREE