Search code examples
emailemacsmu4e

mu4e reply from the correct account


I have several accounts associated with mu4e and I can both receive and send - using smtpmail-multi - from all them without problems. I am not using contexts. The relevant parts in my configuration look like this:

(setq message-citation-line-function 'message-insert-formatted-citation-line)
(setq message-citation-line-format "On %a, %b %d %Y, %f wrote:\n")
(setq smtpmail-multi-accounts
      (quote
       ((yahoo . ("[email protected]"
                 "smtp.mail.yahoo.com"
                 587
                 "[email protected]"
                 nil nil nil nil))
    (other . ("[email protected]"
                 "smtp.other.com"
                 465
         "[email protected]"
                 ssl nil nil nil))
    (etc . ("[email protected]"
                 "smtp.etc.com"
                 465
         "[email protected]"
                 ssl nil nil nil))
     ;; ... 
    )))

(setq smtpmail-multi-associations
      (quote
       (("[email protected]" yahoo)
    ("[email protected]" other)
    ("[email protected]" etc)
     ;; ....
    )))

(setq smtpmail-multi-default-account (quote yahoo))
(setq message-send-mail-function 'smtpmail-multi-send-it)

(setq mu4e-compose-dont-reply-to-self t)
(setq mu4e-user-mail-address-list '("[email protected]"
                    "[email protected]"
                    "[email protected]"
                     ;; ...
                     ))

(setq user-full-name "XXXXXX")
(setq user-mail-address "[email protected]")

This way I can properly send emails from the right account just by typing the address of my email in the FROM: field.

When replying, I want mu4e to identify the account to which the message has been send and update FROM: field accordingly so that I don't have to type in the proper account all the time myself. I am looking for a way to do this without having to upgrade my entire configuration to using contexts.


Solution

  • After a lot of tweaking I came up with this:

    (defun my-mu4e-set-account ()
      "Set the account for composing a message."
      (if mu4e-compose-parent-message
          (let ((mail (cdr (car (mu4e-message-field mu4e-compose-parent-message :to)))))
        (if (member mail mu4e-user-mail-address-list)
            (setq user-mail-address mail)
          (setq user-mail-address "[email protected]")))
        (helm :sources
          `((name . "Select account: ")
            (candidates . mu4e-user-mail-address-list)
            (action . (lambda (candidate) (setq user-mail-address candidate)))))))
    
    (add-hook 'mu4e-compose-pre-hook 'my-mu4e-set-account)
    

    where mu4e-user-mail-address-list is, obviously, a list with my mail accounts

    This will reply with the proper account and, more, will open a helm mini menu asking me which account to use every time I compose a new message.