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 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.
Now let's configure the user virtualmail, group and rights to work with these folders.
sudo useradd -g virtualmail -u 2000 virtualmail -d /home/virtualmailboxes -m
chown -R virtualmail:virtualmail /home/virtualmailboxes
Postfix configuration
Open the Postfix configuration file:
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:
The next step is to add the following settings to the config file:
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
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.
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:
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):
To view the messages received by user1, use the command:
Output:
To view the sender's mailbox (user2), use the command:
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.