Space Panda

Identity Management

This is the seventh part in the series on Reading Mail in the Terminal.

Usually a person has multiple email addresses and when receiving email on one of them, it’s a good idea to have the email program automatically use that address as the sender when replying.

Thanks to (neo)mutt’s scripting and hooks it is fairly easy to have a similar behaviour.

Each identity is stored in its own file in the ~/.config/neomutt/profiles.d/ folder, so it is sufficient to source that file (for example in a reply-hook) to switch to another identity.

# ~/.config/neomutt/profiles.d/default.rc

source "~/.config/neomutt/profiles.d/clear.rc"
set from = ""
set realname = "John McClane"
alias current-from (John McClane)

The first thing in any profile file is to source another file which resets all values that you might change between your identities (from, realname, but also pgp_sign_as and custom headers).

Then you can set all the things you want to have in this identity. For example, if you have multiple mailboxes you could set your record, postponed, and trash to that other mailbox.

Finally there is a profiles.rc to dispatch between the various identities and as the central place for all the hooks and shortcuts for identity switching.

# ~/.config/neomutt/profiles.rc

macro index <Esc>1 '<enter-command>source ~/.config/neomutt/profiles.d/default.rc<enter>'
macro compose <Esc>1 '<enter-command>source ~/.config/neomutt/profiles.d/default.rc<enter><edit-from><kill-line>current-from<enter>'
reply-hook '~C ^$' 'source ~/.config/neomutt/profiles.d/default.rc'

alternates '^$'

source "~/.config/neomutt/profiles.d/default.rc'

The first two macros define Esc 1 as the shortcut to switch to the McClane identity in both the index and the compose screens. In the compose screen the alias current-from is being used to immediately also set up the From for this email.

reply-hook sets (neo)mutt up to automatically switch to that identity when you reply to an email to McClane.

alternates is a helper to determine what addresses are yours and it should be a list of all your email aliases.

As the last step we load the default profile right away, because you probably want to have one identity set up from the start.

All this is very tedious and annoying, so I wrote a python script to generate the profiles.rc as well as all the profile files based on a single YAML file which might look like this:

    name: John McClane
    shortcut: 1

    name: Hans Gruber
        - "0xC0FFEE"
    signature: |
        Hans Gruber
        "You _will_ pay for this."
    shortcut: 2

When you run the script, it will not only generate the profiles.rc file and the other profile files, but also the necessary clear.rc to reset all values when switching between profiles.

Here is a summary of all the possible attributes per identity:

  • name, your real name as it should appear in the email header

  • address, your address as it will be used by mutt

  • aliases (optional), a list of additional aliases that you consider belong to this address

  • sent-mail, trash, drafts (optional), the location where the sent mail, trash, and drafts should be saved

  • shortcut (optional), a single letter that should be used to switch to this identity. If you leave this empty, only the hooks can switch to this profile.

  • signature (optional), a single line or multiline signature that will show up below every mail of this identity.

  • pgp (optional), either just your PGP key ID or a list of your key ID and a URL from which the key can be obtained (will be used in your emails as the X-PGP-Key header)

  • organization (optional), set the Organization header

  • headers (optional), a list of additional headers you want to set

The script itself is called and can be found here. You might want to change some of its default values, like MUTT_PATH and DEFAULTS to meet your requirements.

Tagged as neomutt