\documentclass{article}
\usepackage{ulem}
\usepackage{graphicx}
\usepackage{hyperref}
\pagestyle{headings}
\begin{document}
\part{Dynamically selecting an IMAP server for authentication}
\textbf{This information is valid for Horde 5 or later only. See <a href="https://wiki.horde.org/ImapSelectH4">ImapSelectH4</a> for Horde 4 or <a href="https://wiki.horde.org/ImapSelectH3">ImapSelectH3</a> for Horde 3.}

For Horde 5 or later there already exists an example hook for dynamic IMAP server selection in IMP (imp/config/hooks.php.dist). For the sake of completeness, here is a stripped down copy of the example:

<pre><code class="language-php">
class IMP\_Hooks
\{
    /**
     * AUTHENTICATION HOOK: pre-authentication actions.
     *
     * See horde/config/hooks.php.dist for more information.
     *
     * IMP uses the following credentials:
     *   - password: (string) The password for mail server authentication.
     *   - server: (string) [optional] Use this server key (see
     *             config/backends.php).
     *   - transparent: (boolean) If \$credentials['authMethod'] is
     *                  'transparent', and you want IMP to use the
     *                  userId/credentials generated in the preauthenticate
     *                  hook, this must be true. If false, IMP will instead
     *                  try to authenticate using hordeauth.
     *
     * The following credentials will exist in \$credentials, but changing
     * these values has no effect on authentication:
     *   - imp\_server\_key: (string; 'authenticate' only) The backend server
     *                     key selected on the login page.
     */
     public function preauthenticate(\$userId, \$credentials)
     \{
         if (\$credentials['authMethod'] == 'authenticate') \{
             // Example: Load-balance - pick IMAP server based on first
             // letter in username. Server entries 'server\_[a-z]' must
             // be defined in config/backends.local.php.
             \$credentials['server'] = 'server\_' . substr(\$userId, 0, 1);
             return array(
                 'credentials' => \$credentials,
                 'userId' => \$userId
             );
         \}
         return true;
    \}
\}
</code></pre>
\end{document}
