07.06.2023

How to Set Up Postfix to Use Virtual Mailboxes on Ubuntu 20.04

After the installation and basic configuration of Postfix, it can send and receive mail sent to the names of users registered in the system. In this tutorial, we will set up Postfix to use virtual mailboxes on Ubuntu 20.04.

System configuration

First of all, some preparation of the system is required for the operation of virtual mailboxes. Let's create a folder virtualmailboxes for this, its name can be arbitrary, but if you change it, do not forget to substitute the appropriate option in further configuration. It will contain a folder for your domain name. Inside it, Postfix will add files corresponding to each virtual user after first receiving mail addressed to him.

sudo mkdir -p /home/virtualmailboxes/domain-name.com

Now let's configure the user virtualmail, group and rights to work with these folders.

sudo groupadd -g 2000 virtualmail
sudo useradd -g virtualmail -u 2000 virtualmail -d /home/virtualmailboxes -m
chown -R virtualmail:virtualmail /home/virtualmailboxes

Postfix configuration

Open the Postfix configuration file:

sudo nano /etc/postfix/main.cf

Postfix can operate in one of two modes when processing mail for a single domain:

  • deliver mail to system users as configured earlier;
  • work with virtual mailboxes and deliver mail for virtual users.

To configure the first option, the target domain is added to the variable mydestination. Therefore, now we will remove it from there and bring it to the following form:

mydestination = localhost.com, localhost

The next step is to add the following settings to the config file:

virtual_mailbox_domains = domain-name.com
virtual_mailbox_base = /home/virtualmailboxes
virtual_mailbox_maps = hash:/etc/postfix/virtualmaps
virtual_minimum_uid = 500
virtual_uid_maps = static:2000
virtual_gid_maps = static:2000

The presence of the variable virtual_mailbox_domains indicates that Postfix is working in virtual mailbox mode. It also contains the domain for which the mail is being processed. virtual_mailbox_base indicates the path for storing mail, virtual_mailbox_maps indicates a file with a list of virtual users. virtual_minimum_uid, virtual_uid_maps, virtual_gid_maps - settings of the system user who will manage the mail folder.

Now let's create a list of virtual mailboxes, the path to which was specified in virtual_mailbox_maps. Each line of this file must contain a mail address in the user@domain format and separated by a space domain/folder - file path for storing mail relative to virtual_mailbox_base.

sudo nano /etc/postfix/virtualmaps
user1@domain-name.com domain-name.com/user1
user2@domain-name.com domain-name.com/user2
support@domain-name.com domain-name.com/support

Save the file. To apply the settings, you need to run 2 commands:

postmap /etc/postfix/virtualmaps
postfix reload

Testing virtual mailboxes

To test the receipt of an email by a virtual user, let's send him an email. We will do this on behalf of another user created by us, and indicate his mail as a return address (parameters -u and -r):

sudo echo "Postfix virtual mailboxes test" | mail -r user2@domain-name.com -u user2@domain-name.com -s "Subject" user1@domain-name.com

To view the messages received by user1, use the command:

sudo cat /home/virtualmailboxes/domain-name.com/user1

Output:

From user2@domain-name.com Wed Mar 31 12:55:19 2021
Return-Path: <user2@domain-name.com>
X-Original-To: user1@domain-name.com
Delivered-To: user1@domain-name.com
Received: by domain-name.com (Postfix, from userid 0)
id 260A481421; Wed, 31 Mar 2021 12:55:19 +0000 (UTC)
Subject: Subject
To: <user1@domain-name.com>
X-Mailer: mail (GNU Mailutils 3.7)
Message-Id: <20210331125519.260A481421@domain-name.com>
Date: Wed, 31 Mar 2021 12:55:19 +0000 (UTC)
From: root <user2@domain-name.com>
Postfix virtual mailboxes test

If the letter is not there, try looking at the mail sending logs using the command:

sudo cat /var/log/mail.log

In addition, if an error occurs, the letter can be returned to the sender. That is why we sent it on behalf of the previously created virtual user. After all, mail is no longer delivered to system users. To view the sender's mailbox (user2), use the command:

sudo cat /home/virtualmailboxes/domain-name.com/user2