How to Set Up Postfix with 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 with virtual mailboxes. Let's create a folder with name virtualmailboxes for this. The name can be arbitrary, so do not forget to substitute the appropriate name if you change it 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 /var/vmail/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 /var/vmail -m
sudo chown -R virtualmail:virtualmail /var/vmail
sudo chmod -R 750 /var/vmail
Postfix configuration
Open the Postfix configuration file:
sudo nano /etc/postfix/main.cf
There are two modes available in Postfix to process 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 = /var/vmail
virtual_mailbox_maps = hash:/etc/postfix/virtualmaps
virtual_minimum_uid = 500
virtual_uid_maps = static:2000
virtual_gid_maps = static:2000
virtual_transport = virtual
Added settings:
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 the file with 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 file path to store emails in domain/folder (address and path are separated with a space). File path is relatived to the value of virtual_mailbox_base option, in our case - /home/virtualmailboxes.
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/
Added strings:
Save the file. To apply the settings, you need to run 2 commands:
sudo postmap /etc/postfix/virtualmaps
sudo 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):
Make sure that mailutils is installed:
sudo apt install mailutils
echo "Postfix virtual mailboxes test" | mail -s "Subject" -r user2@domain-name.com user1@domain-name.com
To view the messages received by user1, use the command:
sudo cat /home/virtualmailboxes/domain-name.com/user1
Output:
To view the sender's mailbox (user2), use the command:
sudo cat /home/virtualmailboxes/domain-name.com/user2
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 previous settings mail is no longer delivered to system users.
Postfix logs are stored in /var/log/mail.log, error messages in /var/log/mail.err. Inspect these files if something is going not correctly.
tail -f /var/log/mail.log
tail -f /var/log/mail.err
FAQ
- Q: Where are the received emails stored?
A: Emails are stored in the folder specified by virtual_mailbox_base, with subdirectories based on the domain and user. For example, emails for user1@domain-name.com will be saved in /var/vmail/domain-name.com/user1/. - Q: Do I need to install Dovecot to access the emails?
A: Yes, if you want users to retrieve emails via IMAP or POP3, you need to install and configure a mail delivery agent like Dovecot. Postfix only handles the sending and receiving of emails, not user access. - Q: Why isn't my test email being delivered?
A: Check that Postfix was reloaded (postfix reload) after updating the configuration and maps. Also verify the mail logs at /var/log/mail.log for errors. Ensure the domain is not listed in mydestination to avoid local user delivery conflicts. - Q: Can I use a different path instead of /var/vmail?
A: Yes, but make sure to update all related settings (virtual_mailbox_base, file paths in virtualmaps, and ownership permissions) consistently. - Q: Is Maildir format required?
A: It's strongly recommended, as most modern mail clients and tools expect it. Ensure folder paths in virtualmaps end with a trailing slash (/) to enable Maildir delivery.


