Rotating identities with Mutt

Introduction

In mutt, at the compose menu , just before I click ‘y’ to send the mail, I would like to choose the identity under which I send the mail.

I use a ~/.mutt directory containing a muttrc file rather than using a ~/.muttrc file because I have split my mutt configuration into several files and I would rather make it clear that they all belong together by putting them together in one directory; if you don’t do this then you’ll need to adjust some of the paths below to suit how you do things.

Procedure

  1. Create ~/.mutt/identity_1 containing the mutt configuration for your default identity; typically this will include setting the ‘From:’ address and maybe the SMTP server to use. Here is an example:
    push <edit-from>^U'Alexis Huxley <ahuxley@gmx_no_spam_please.net>'\n
    set smtp_url=smtps://my_gmx_login:my_gmx_password@mail.gmx.net/
  2. Create ~/.mutt/identity_2 containing the mutt configuration for your second identity, and so on.
  3. Edit ~/.mutt/muttrc and add the following macro definition:
    macro compose v ":source ~/.mutt/identity_\`echo $(($(date +%s) % \
        $(ls ~/.mutt/identity_[0-9]* | wc -l) + 1))\`\n"

    alert Use of $((... % ... + 1)) and of $(...) are bash specific; if you’re not using bash then you’ll need to use expr and multi-escaped backticks respectively.

  4. You can now hold down ‘v’ to rotate between your different identities; note that said ‘hold down’ not ‘press’; can you see why?

Unfortunately pushing keypresses into the buffer is the only scriptable way to change the ‘From:’ address once you have already started writing a mail, and, since there is no way to tell mutt “Push these keypresses if we’re at the compose menu, but not at any other time!”, then these identity files cannot be loaded when mutt first starts up. Therefore there’s still a bit to do to set the default identity to be used when you don’t want to press ‘v’.

  1. Create an init-time-loadable version of your first identity by running the following command:
    grep -v ^push identity_1 > identity_default
  2. Edit ~/.mutt/muttrc and add the following commands to load that file:
    source ~/.mutt/identity_default

See also