I use mu4e
for email with multiple accounts. I have multiple accounts in mu4e
and followed the manual with a function my-mu4e-set-account
and a hook to it:
(add-hook 'mu4e-compose-pre-hook 'my-mu4e-set-account)
I can start a message with
(compose-mail "To <to@to>" "subject" '(("From" . "me <me@me>")))
I get queried twice about which account to use:
[mu4e] Select context: [A]ccount1 [S]omeOther
Compose with account: (Account1/SomeOther)
and then the "From" of my choice is overwritten by the selection from mu4e.
How can I set the "From" header programmatically without mu4e
asking for the context twice? And how can I pre-fill the body of the email, since compose-mail
only takes headers in the other-headers
argument and the body is not a header?
Update: Following the comments, here is a scenario. I have a list of 20 email addresses to which I want to send a similar email with a custom To:
and the name in the body. I would like to send those twenty emails programmatically from mu4e
:
- initiate the email
- set the right context with the
From:
address - set the subject
- set the custom recipient
- set the body, customized to the recipient
- send the email
At the moment, the interactive prompts interrupt me at step 2 of setting the From:
address. The second prompt was my blunder from adding a function from the manual:
(defun my-mu4e-set-account ()
"Set the account for composing a message.
This function is taken from:
https://www.djcbsoftware.nl/code/mu/mu4e/Multiple-accounts.html"
(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)
From the comments, I can set the body with message-goto-body
and insert
. So the only other question is how to set the right context programmatically. I disabled the context switch hooks and tried this code:
(compose-mail "" "" '(("From" . "me <me@domain.com>")))
and I get a draft from the last used context email address and a body with a mysterious 158
in it.
Second update: I read the Contexts part of the manual and am still unclear on how to set the right context. The variables mu4e-contexts
and my-mu4e-account-alist
are set to my list of accounts, e.g. Account1
and SomeOther
, and I don't know how to tell mu4e
to pick the one that matches a string such as SomeOther
. I tried
(setq mu4e-compose-pre-hook nil)
(mu4e-context-switch nil "SomeOther")
I compose a new message with C-x m
and still get a draft from Account1
.
Although the suggestion of auto-answer
in a comment could work, I believe a more straightforward solution exists.