Why does mutt keep asking for imap username and password

imapmutt

Unsetting mutt's configuration variables imap_user, imap_pass (and perhaps preconnect, imap_authenticators as well) via an account-hook . "unset ... " call, seems to be common practice, if not a necessity, for handling multiple imap accounts (see Managing multiple IMAP/POP accounts (OPTIONAL), Mutt imap multiple account, mutt: gmail IMAP unresponsive, an account-hook related configuration file in funtoo.org).

Currently I handle only one account via IMAP. Plans for multiple account handling lead me to follow the instructions found in the last of the above mentioned links (someone's example of mutt configuration). Therefore, in a similar way, I used the following:

account-hook . 'unset imap_user; unset imap_pass; unset tunnel'
account-hook 'imaps://mail.domain.net:993/INBOX/' "set imap_user=UserName@domain.net"
account-hook 'imaps://mail.domain.net:993/INBOX/' "set imap_pass=${my_password}"

This is stored in a separate file (named account_hooks) and sourced from inside muttrc. For reasons I don't understand, mutt keeps asking for the username and the password. However, if the variables imap_user and imap_pass are set directly in muttrc, e.g.

set my_password="`gpg --decrypt ~/.mutt/password.gpg`"
set imap_authenticators='login'
set imap_login = 'UserName@domain.net'
set imap_user = 'UserName@domain.net'
set imap_pass ="${my_password}"

everything works fine. The account_hooks file is the first one sourced and no other account-hook . "unset ..." call(s) exist(s) anywhere else.

Update, The folder-hooks file is (and was, I think) as follows:

#--------------------------------------------------------------------------
# Folders and hooks
#--------------------------------------------------------------------------
# folder-hook 'imaps://UserName%domain.net@mail.domain.net:993/'
set folder = "~/.maildir"       # IMAP: local, using offlineimap -- folder="imaps://mail.domain.net:993/INBOX/"
source ~/.mutt/mailboxes        # source automatically generated mailboxes
set spoolfile = "+INBOX"        # spoolfile='imaps://mail.domain.net:993/'
set postponed = "+INBOX/Drafts"

# Sending -----------------------------------------------------------------
set smtp_url="smtp://UserName@domain.net@mail.domain.net:587/"
set smtp_pass=${my_password}
set record = "+INBOX/Sent"
set copy=yes

# Index format ----------------------------------------------------------------
folder-hook *[sS]ent* 'set sort=threads'
folder-hook *[sS]ent* 'set sort_browser=reverse-date'
folder-hook *[sS]ent* 'set sort_aux=reverse-last-date-received'
folder-hook *[sS]ent* 'set index_format="%2C | %Z [%d] %-30.30t (%-4.4c) %s"'
folder-hook ! *[sS]ent* 'set index_format="%2C | %Z [%d] %-30.30F (%-4.4c) %s"':

Why does, the separate file account_hooks, not feed properly the variables of interest in this case (i.e. imap_user and imap_pass)?

Best Answer

You can set username and password directly, but it doesn't work when you use an account-hook, so probably the account-hook doesn't work.

An account-hook consists of a regexp for the mailboxes, and those commands which should be executed if a mailbox matches the regexp.

Since the commands (set imap_user, set imap_pass) are not executed, we can assume that the regexp didn't match your mailboxes.

You are using 'imaps://mail.domain.net:993/INBOX/' which is very specific. Probably your mailboxes are named slightly different.

Is this the only mail account from mail.domain.net you are using? If so, reducing the regexp to 'mail.domain.net' should be enough to match your mailboxes.

account-hook . 'unset imap_user; unset imap_pass; unset tunnel
account-hook mail.domain.net "set imap_user=UserName@domain.net"
account-hook mail.domain.net "set imap_pass=${my_password}"
Related Question