Note: for mu4e version 0.9.16 and higher, the recommended way to deal with multiple accounts is through mu4e’s built-in Contexts system. For older versions, the below still works.
Using mu4e with multiple email accounts is fairly easy. Although
variables such as
smtpmail-*, etc. typically only take one value,
it is easy to change their values using
The setup described here is one way of doing this (though certainly not
the only way).
This setup assumes that you have multiple mail accounts under
mu4e-maildir. As an example, we’ll use ~/Maildir/Account1
and ~/Maildir/Account2, but the setup works just as well if
mu4e-maildir points to something else.
First, you need to make sure that all variables that you wish to change based on user account are set to some initial value. So set up your environment with e.g., your main account:
(setq mu4e-sent-folder "/Account1/Saved Items" mu4e-drafts-folder "/Account1/Drafts" user-mail-address "email@example.com" smtpmail-default-smtp-server "smtp.account1.example.com" smtpmail-local-domain "account1.example.com" smtpmail-smtp-server "smtp.account1.example.com" smtpmail-stream-type 'starttls smtpmail-smtp-service 25)
Then create a variable
my-mu4e-account-alist, which should
contain a list for each of your accounts. Each list should start with
the account name, (which must be identical to the account’s
directory name under ~/Maildir), followed by
(defvar my-mu4e-account-alist '(("Account1" (mu4e-sent-folder "/Account1/Saved Items") (mu4e-drafts-folder "/Account1/Drafts") (user-mail-address "firstname.lastname@example.org") (smtpmail-default-smtp-server "smtp.account1.example.com") (smtpmail-local-domain "account1.example.com") (smtpmail-smtp-user "username1") (smtpmail-smtp-server "smtp.account1.example.com") (smtpmail-stream-type starttls) (smtpmail-smtp-service 25)) ("Account2" (mu4e-sent-folder "/Account2/Saved Items") (mu4e-drafts-folder "/Account2/Drafts") (user-mail-address "email@example.com") (smtpmail-default-smtp-server "smtp.account2.example.com") (smtpmail-local-domain "account2.example.com") (smtpmail-smtp-user "username2") (smtpmail-smtp-server "smtp.account2.example.com") (smtpmail-stream-type starttls) (smtpmail-smtp-service 587))))
You can put any variable you want in the account lists, just make sure
that you put in all the variables that differ for each account.
Variables that do not differ need not be included. For example, if you
use the same SMTP server for both accounts, you don’t need to include
the SMTP-related variables in
Note that some SMTP servers (such as Gmail) require the SMTP username to match the user mail address. In this case, your mail appears to originate from whichever SMTP account you use. Thus unless you are certain your SMTP server does not have this requirement, you should generally use different SMTP account credentials for each mail account.
Now, the following function can be used to select an account and set the
my-mu4e-account-alist to the correct values:
(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"))))
This function then needs to be added to
(add-hook 'mu4e-compose-pre-hook 'my-mu4e-set-account)
my-mu4e-set-account is called every time you edit a
message. If you compose a new message, it simply asks you for the
account you wish to send the message from (TAB completion works). If
you’re replying or forwarding a message, or editing an existing draft,
the account is chosen automatically, based on the first component of the
maildir of the message being replied to, forwarded or edited (i.e., the
directory under ~/Maildir).