Messaging Fundamentals Series

🏘️ The Top 1% Hands-On for Building Virtual Domains to Relay Mail for Multiple Domains on a Single Postfix Server

Introduction

This article is part of the Top 1% Series Complete Article Guide.

Prerequisite Knowledge

The Big Picture

graph LR
    A["Mail addressed to sales@mailtest2.local"]
    B["Mail addressed to support@mailtest2.local"]
    Postfix["A single Postfix server<br/>(virtual_alias_maps)"]
    Alice["alice's Maildir<br/>(an existing mailtest.local user)"]
    Bob["bob's Maildir<br/>(an existing mailtest.local user)"]
    A --> Postfix --> Alice
    B --> Postfix --> Bob

Hands-On Steps

Step 1: Register the Second Domain as a Virtual Domain

Add mailtest2.local as a virtual alias domain in /etc/postfix/main.cf.

virtual_alias_domains = mailtest2.local
virtual_alias_maps = hash:/etc/postfix/virtual

What matters here is that mailtest2.local is never added to mydestination (an ordinary domain) at all. virtual_alias_domains is treated as a domain that exists purely for redirecting destinations, with no real Unix user or mailbox of its own.

Step 2: Create the Destination Mapping (virtual_alias_maps)

In /etc/postfix/virtual, write the mapping between mailtest2.local addresses and their actual delivery destination (an existing Unix user).

sales@mailtest2.local      alice
support@mailtest2.local    bob

Compile it into a format Postfix can reference.

sudo postmap /etc/postfix/virtual
sudo systemctl restart postfix

Step 3: Send Mail via telnet and Confirm It’s Actually Delivered

Send mail addressed to sales@mailtest2.local.

telnet localhost 25
HELO client.local
MAIL FROM:<test@example.com>
RCPT TO:<sales@mailtest2.local>
DATA
Subject: Test to virtual domain

This is a test message to the virtual domain.
.
QUIT

Check alice’s Maildir.

sudo ls -la /home/alice/Maildir/new/

Mail sent to sales@mailtest2.local, a destination that shouldn’t exist at all, should now be sitting right in alice’s mailbox, a real Unix user. You never had to create a new Unix user named sales for that email address.

What a Pro Sees Here (Top 1% Understanding)

Freedom From “Adding a Server for Every Domain”

The biggest real-world value of a virtual alias domain is that it eliminates the need for a separate mail server per domain, for the common need of running mail under a different domain per customer or per brand. Hosting providers and organizations running multiple brand sites commonly consolidate mail for dozens to hundreds of domains onto a small number of Postfix servers. virtual_alias_maps — a plain text-based mapping table — is the actual mechanism enabling that consolidation.

The Difference Between Virtual Aliases and Virtual Mailboxes

The virtual_alias_domains covered in this hands-on is purely a mechanism for redirecting a destination to an existing Unix user. Separately, Postfix also has virtual_mailbox_domains, a more fully-fledged mechanism for holding a dedicated mailbox entirely independent of any Unix user. It’s important to understand these serve different purposes: a virtual alias adds a delivery destination to an existing mailbox, while a virtual mailbox creates large numbers of independent mailboxes without creating any Unix account at all. A virtual alias is plenty for consolidating a small number of destinations, but for hosting mail at a scale of thousands of users, adopting virtual mailboxes is standard practice.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. Mail to a virtual domain isn’t arriving: Check whether you recompiled with postmap, and whether virtual_alias_maps’s path is correct.
  2. Postfix errors out at startup: Check whether the same domain appears in both mydestination and virtual_alias_domains.
  3. Only some destinations aren’t being delivered: Check whether that destination is correctly registered in /etc/postfix/virtual’s mapping.

Summary

Takeaways to Apply Today

  1. When handed multi-domain mail operations, consider consolidating with virtual alias domains before adding a server per domain.
  2. As the destination scale grows, consider adopting virtual mailboxes instead of virtual aliases.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).