1# Multiple domains configuration23By default, maddy uses email addresses as account identifiers for both4authentication and storage purposes. Therefore, account named `user@example.org`5is completely independent from `user@example.com`. They must be created6separately, may have different credentials and have separate IMAP mailboxes.78This makes it extremely easy to setup maddy to manage multiple otherwise9independent domains.1011Default configuration file contains two macros - `$(primary_domain)` and12`$(local_domains)`. They are used to used in several places thorough the13file to configure message routing, security checks, etc.1415In general, you should just add all domains you want maddy to manage to16`$(local_domains)`, like this:17```18$(primary_domain) = example.org19$(local_domains) = $(primary_domain) example.com20```21Note that you need to pick one domain as a "primary" for use in22auto-generated messages.2324With that done, you can create accounts using both domains in the name, send25and receive messages and so on. Do not forget to configure corresponding SPF,26DMARC and MTA-STS records as was recommended in27the [introduction tutorial](tutorials/setting-up.md).2829Also note that you do not really need a separate TLS certificate for each30managed domain. You can have one hostname e.g. mail.example.org set as an MX31record for multiple domains.3233**If you want multiple domains to share username namespace**, you should change34several more options.3536You can make "user@example.org" and "user@example.com" users share the same37credentials of user "user" but have different IMAP mailboxes ("user@example.org"38and "user@example.com" correspondingly). For that, it is enough to set `auth_map`39globally to use `email_localpart` table:40```41auth_map email_localpart42```43This way, when user logs in as "user@example.org", "user" will be passed44to the authentication provider, but "user@example.org" will be passed to the45storage backend. You should create accounts like this:46```47maddy creds create user48maddy imap-acct create user@example.org49maddy imap-acct create user@example.com50```5152**If you want accounts to also share the same IMAP storage of account named53"user"**, you can set `storage_map` in IMAP endpoint and `delivery_map` in54storage backend to use `email_locapart`:55```56storage.imapsql local_mailboxes {57 ...58 delivery_map email_localpart # deliver "user@*" to "user"59}60imap tls://0.0.0.0:993 {61 ...62 storage &local_mailboxes63 ...64 storage_map email_localpart # "user@*" accesses "user" mailbox65}66```6768You also might want to make it possible to log in without69specifying a domain at all. In this case, use `email_localpart_optional` for70both `auth_map` and `storage_map`.7172You also need to make `authorize_sender` check (used in `submission` endpoint)73accept non-email usernames:74```75authorize_sender {76 ...77 user_to_email chain {78 step email_localpart_optional # remove domain from username if present79 step email_with_domain $(local_domains) # expand username with all allowed domains80 }81}82```8384## TL;DR8586Your options:8788**"user@example.org" and "user@example.com" have distinct credentials and89distinct mailboxes.**9091```92$(primary_domain) = example.org93$(local_domains) = example.org example.com94```9596Create accounts as:9798```shell99maddy creds create user@example.org100maddy imap-acct create user@example.org101maddy creds create user@example.com102maddy imap-acct create user@example.com103```104105**"user@example.org" and "user@example.com" have same credentials but106distinct mailboxes.**107108```109$(primary_domain) = example.org110$(local_domains) = example.org example.com111auth_map email_localpart112```113114Create accounts as:115```shell116maddy creds create user117maddy imap-acct create user@example.org118maddy imap-acct create user@example.com119```120121**"user@example.org", "user@example.com", "user" have same credentials and same122mailboxes.**123124```125 $(primary_domain) = example.org126 $(local_domains) = example.org example.com127 auth_map email_localpart_optional # authenticating as "user@*" checks credentials for "user"128129 storage.imapsql local_mailboxes {130 ...131 delivery_map email_localpart_optional # deliver "user@*" to "user" mailbox132 }133134 imap tls://0.0.0.0:993 {135 ...136 storage_map email_localpart_optional # authenticating as "user@*" accesses "user" mailboxes137 }138139 submission tls://0.0.0.0:465 {140 check {141 authorize_sender {142 ...143 user_to_email chain {144 step email_localpart_optional # remove domain from username if present145 step email_with_domain $(local_domains) # expand username with all allowed domains146 }147 }148 }149 ...150 }151```152153Create accounts as:154```shell155maddy creds create user156maddy imap-acct create user157```