How to Set Up Multiple Domains on One Mail Server

A single Postfix/Dovecot mail server can host email for multiple domains — a common, efficient setup for agencies, businesses with multiple brands, or anyone consolidating email hosting.

Understanding Virtual Domains

Rather than the server's system users directly corresponding to email addresses, "virtual domains" let you define arbitrary domains and mailboxes independent of actual system user accounts — the standard approach for multi-domain mail hosting.

Prerequisites

  • Postfix and Dovecot already installed (see How to Install and Configure Postfix as a Mail Transfer Agent and How to Install and Configure Dovecot for IMAP/POP3)
  • A database (MySQL/PostgreSQL) for virtual domain/mailbox management (recommended for multiple domains)

Step 1 — Set Up a Database for Virtual Mail Configuration

CREATE DATABASE mailserver;
CREATE TABLE virtual_domains (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    PRIMARY KEY (id)
);
CREATE TABLE virtual_users (
    id INT NOT NULL AUTO_INCREMENT,
    domain_id INT NOT NULL,
    email VARCHAR(100) NOT NULL,
    password VARCHAR(150) NOT NULL,
    PRIMARY KEY (id)
);

Step 2 — Add Your Domains

INSERT INTO virtual_domains (name) VALUES ('domain1.com'), ('domain2.com');

Step 3 — Configure Postfix to Query the Database

sudo nano /etc/postfix/mysql-virtual-mailbox-domains.cf
user = mailuser
password = CHANGE_ME
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_domains WHERE name='%s'

Step 4 — Reference This in main.cf

virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf

Step 5 — Configure Mailbox Lookup Similarly

sudo nano /etc/postfix/mysql-virtual-mailbox-maps.cf
query = SELECT CONCAT(domain_id, '/', email, '/') FROM virtual_users WHERE email='%s'

Step 6 — Configure Dovecot for the Same Database

sudo nano /etc/dovecot/dovecot-sql.conf.ext
driver = mysql
connect = host=127.0.0.1 dbname=mailserver user=mailuser password=CHANGE_ME
password_query = SELECT email as user, password FROM virtual_users WHERE email='%u'

Step 7 — Verify DNS for Each Domain

Each hosted domain needs its own correct MX record pointing to your mail server, and its own SPF/DKIM/DMARC records (see How to Configure SPF, DKIM, and DMARC (Complete Guide)) — deliverability configuration is per-domain, not shared automatically across all hosted domains.

Step 8 — Set Up Reverse DNS Appropriately

See How to Set Up Reverse DNS (PTR/rDNS) for Email Deliverability — reverse DNS is tied to the server's IP, not per-domain, so this is configured once for the server regardless of how many domains it hosts.

Restart Services

sudo systemctl restart postfix dovecot

Managing Multiple Domains at Scale

For managing many domains/mailboxes, consider a dedicated mail server management panel rather than direct database manipulation, simplifying ongoing administration considerably as the number of hosted domains/users grows.

Common Errors

Mail for a specific domain bounces despite correct MX records — verify the domain was actually added to the virtual_domains table and Postfix's database query configuration is correctly retrieving it.

Continue Reading

Browse more articles in Email Hosting & Deliverability.

  • multi domain mail server, postfix virtual domains, hosting multiple domains email, postfix mysql virtual
  • 0 brukere syntes dette svaret var til hjelp
Var dette svaret til hjelp?

Relaterte artikler

How to Set Up a VPS for Email Marketing (Complete Guide)

Running your own email marketing infrastructure on a VPS gives you full control over...

How to Configure SPF, DKIM, and DMARC (Complete Guide)

SPF, DKIM, and DMARC are the three DNS-based email authentication standards that Gmail, Outlook,...

How to Set Up Reverse DNS (PTR/rDNS) for Email Deliverability

Reverse DNS (PTR record) maps your server's IP address back to a hostname — the opposite of...

IP Warm-Up: A Safe 30-Day Schedule for New Sending IPs

Mailbox providers like Gmail and Outlook monitor sending behavior closely and treat sudden...

Best Email Marketing Software for a VPS (MailWizz, Mautic, Listmonk & More)

Choosing the right self-hosted email marketing platform affects deliverability, ease of...