I wanted my users to autoconfigure their clients with mail.theirdomain.com instead of the server hostname as I tend to move accounts around when doing server maintenance or upgrades; thus the server hostname is not always the one serving the emails, but mail.theirdomain.com always points to the correct IP as I have a script to bulk update them whenever I need to.

Long story short I replaced the $autodiscover_config part of data/web/inc/vars.inc.php with

// serve autodiscover from mail subdomain
$mail_domain = 'mail.' . implode('.', array_slice(explode('.', explode(':', $_SERVER['HTTP_HOST'])[0]), -2));

$autodiscover_config = array(
  // General autodiscover service type: "activesync" or "imap"
  // emClient uses autodiscover, but does not support ActiveSync. mailcow excludes emClient from ActiveSync.
  // With SOGo disabled, the type will always fallback to imap. CalDAV and CardDAV will be excluded, too.
  'autodiscoverType' => 'activesync',
  // If autodiscoverType => activesync, also use ActiveSync (EAS) for Outlook desktop clients (>= Outlook 2013 on Windows)
  // Outlook for Mac does not support ActiveSync
  'useEASforOutlook' => 'no',
  // Please don't use STARTTLS-enabled service ports in the "port" variable.
  // The autodiscover service will always point to SMTPS and IMAPS (TLS-wrapped services).
  // The autoconfig service will additionally announce the STARTTLS-enabled ports, specified in the "tlsport" variable.
  'imap' => array(
    'server' => $mail_domain,
    'port' => (int)filter_var(substr(getenv('IMAPS_PORT'), strrpos(getenv('IMAPS_PORT'), ':')), FILTER_SANITIZE_NUMBER_INT),
    'tlsport' => (int)filter_var(substr(getenv('IMAP_PORT'), strrpos(getenv('IMAP_PORT'), ':')), FILTER_SANITIZE_NUMBER_INT)
  ),
  'pop3' => array(
    'server' => $mail_domain,
    'port' => (int)filter_var(substr(getenv('POPS_PORT'), strrpos(getenv('POPS_PORT'), ':')), FILTER_SANITIZE_NUMBER_INT),
    'tlsport' => (int)filter_var(substr(getenv('POP_PORT'), strrpos(getenv('POP_PORT'), ':')), FILTER_SANITIZE_NUMBER_INT)
  ),
  'smtp' => array(
    'server' => $mail_domain,
    'port' => (int)filter_var(substr(getenv('SMTPS_PORT'), strrpos(getenv('SMTPS_PORT'), ':')), FILTER_SANITIZE_NUMBER_INT),
    'tlsport' => (int)filter_var(substr(getenv('SUBMISSION_PORT'), strrpos(getenv('SUBMISSION_PORT'), ':')), FILTER_SANITIZE_NUMBER_INT)
  ),
  'activesync' => array(
    'url' => 'https://' . $mail_domain . ($https_port == 443 ? '' : ':' . $https_port) . '/Microsoft-Server-ActiveSync',
  ),
  'caldav' => array(
    'server' => $mail_domain,
    'port' => $https_port,
  ),
  'carddav' => array(
    'server' => $mail_domain,
    'port' => $https_port,
  ),
);

and edited mailcow.conf with ADDITIONAL_SAN=mail.*

Thanks for sharing @rallisf1 , this is awesome, I’ve been looking for this exact thing.

One question: I don’t have DNS records for ALL my client domains set up pointing mail.clientdomain.com to my mailcow server, so I think your script would break autodiscover for these clients. Instead, I’d like autodiscover to first check if mail.clientdomain.com resolves to my mailcow server correctly (/check if SSL cert is valid for mail.clientdomaim.com?), and if not, use the standard mailcow.servername.tld as a fallback for autodiscover instead.

Think this is do-able, using your existing script as a starting point?

Or better yet, maybe the script could do an MX lookup on the domain in question, and match autodiscover to the (primary) MX record, whether that’s mail.clientdomain.com or mailcow.server.tld for each domain.

Have something to say?

Join the community by quickly registering to participate in this discussion. We'd like to see you joining our great moo-community!

No one is typing