If you ever watched me register on a website, you might have noticed that I always pick the sites top level domain as the local part of my email. So, on example.com, I would register as example.com@bascht.com - that way I can make sure I have proper filters in place and in case there is spam delivered to any of those addresses, I have reasonable suspicion who dropped the ball. (Also gopass can defer the user name if I just abide by my own conventions).

To keep things tidy, I also want to also reply from example.com@bascht.com and this is where it gets interesting:

Sadly not all email clients (and by that, I mean almost none) are able to send emails from a varying address. I remember Thunderbird let me edit the From field in the olden days, but I don't know if it is still able to.

Since I use mu4e for all my email needs – with a smidgen of Emacs Lisp - it can be taught to pick the correct sender when replying.

First, let's define a function that will process the mu4e message (msg):

(defun bascht/mu4e-change-from-to-catchall (msg)
  "Set the From address based on the To address of the original message if I reply."
  (setq user-mail-address
    (if (and msg (mu4e-message-contact-field-matches msg :to "bascht.com"))
      (plist-get (car-safe (mu4e-message-field msg :to)) :email)
      (cdr-safe (assoc 'user-mail-address (mu4e-context-vars (mu4e-context-current)))))))

To set the From address, I found no other way than to directly modify user-email=address, so in case the domain of :to matches bascht.com, I will just use it as my sender address. In any other case, I want mu4e just to fall back to the default address of the current context.

⚠️ That last bit is important. Otherwise mu4e will stick to the user-email-address and you will compose new emails with the wrong sender.

Let's hook into mu4e-compose-pre-hook:

(add-hook! 'mu4e-compose-pre-hook
  (bascht/mu4e-change-from-to-catchall mu4e-compose-parent-message) ;; ← our function is called here.
  (spell-fu-mode)                                                   ;; parent message is the "original"
  (bascht/switch-spellcheck "de_DE")                                ;; message we reply to
  (evil-insert-state))

I left the rest of the hook in place in case you want to copy or adapt it, but it's not strictly needed.

The rest of the body will:

  • Switch on spell-fu-mode. I don't have a global spell mode enabled since it tends to get annyoing and slows down most editors. I rather opt-in.

  • I switch to de_DE per default, as most of my emails are composed in German.

  • I directly switch to (evil-insert-state) since I want to start typing. 😁

And that's it. Upon composing a reply to a message, Emacs will now pick the correct address for From.

(Many thanks to everyone who replied to my Question on Mastodon!)