1

I have been digging for an answer to my current problem for several weeks now, so I am asking here in hopes someone can shed some light or point me in the right direction.

I am attempting to configure Postfix such that:

  1. Inbound mail for user1@domain1.com is delivered to /some/custom/MailDir/.
  2. Inbound mail for user2@domain2.com is delivered to /different/custom/MailDir/.
  3. Outbound mail can be sent from either user/domain.
  4. user3@domain1.com and user3@domain2.com should have separate custom Maildirs i.e /custom/user3/domain1/mail/ and /different/user3/domain2/mail/`

I have not been able to find a clear explanation of how to achieve this online.

So far, as each domain requires a custom directory pathname, I believe I will need to use multiple postfix instances. I have been looking into postmulti. I have 2 postfix instances running, by using postmulti, but it is not clear to me how I achieve my above goals using postmulti.

I am thinking I possibly need to have a single instance listening for external mail. Based on the incoming mail recipient address, this instance then forwards the mail to a separate internal instance. This instance has its own mail_spool_directory, which I can configure to deliver where I desire.

I am running bash on Ubuntu. My postfix is version 3.9.0. I have a single instance successfully configured to deliver/receive mail in a custom directory.

1 Answers1

0

To achieve delivering incoming mail to custom directories based on recipient domains with Postfix, you don't necessarily need to use multiple Postfix instances. You can achieve this using virtual alias maps and virtual transport maps.

I'm going to explain how to do this, but I would encourage you to use a database server approach, instead of files on disk. There are even docker images ready to be deployed that are more "future-compatible".

OK.

First: backup your current configuration.

Then, configure postfix for virtual domains. I use vim for editing, you might want to use vi, nano...other.

Edit Postfix main configuration file:

sudo vim /etc/postfix/main.cf

Add or modify the following settings:

virtual_mailbox_domains = domain1.com, domain2.com
virtual_mailbox_base = /var/mail/vhosts
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_alias_maps = hash:/etc/postfix/virtual
virtual_transport = virtual

Then, define mailboxes and aliases by creating the vmailbox file to define the mailbox locations:

sudo vim /etc/postfix/vmailbox

Add the entries for the users and their corresponding mail directories:

user1@domain1.com /some/custom/MailDir/user1/
user2@domain2.com /different/custom/MailDir/user2/
user3@domain1.com /custom/user3/domain1/mail/
user3@domain2.com /different/user3/domain2/mail/

Save and close the file.

Now, create the virtual alias file (if needed) for any virtual aliasing:

sudo vim /etc/postfix/virtual

Example content:

user1@domain1.com user1@domain1.com
user2@domain2.com user2@domain2.com

Save and close the file.

Run postmap to hash these files:

sudo postmap /etc/postfix/vmailbox
sudo postmap /etc/postfix/virtual

Now, configure Dovecot for virtual mailboxes, edit dovecot configuration:

sudo vim /etc/dovecot/dovecot.conf

Make sure you've got the following:

mail_location = maildir:/var/mail/vhosts/%d/%n

Configure Dovecot to use Postfix's virtual user database:

sudo vim /etc/dovecot/conf.d/auth-passwdfile.conf.ext

Make sure you've got:

passdb {
driver = passwd-file
args = /etc/dovecot/users
}

userdb { driver = static args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n }

Create the Dovecot users file:

sudo vim /etc/dovecot/users

Add the users:

user1@domain1.com:{plain}password1
user2@domain2.com:{plain}password2
user3@domain1.com:{plain}password3
user3@domain2.com:{plain}password4

Save and close the file.

Set permissions:

sudo chown -R vmail:vmail /var/mail/vhosts

Then restart Postfix and Dovecot

sudo systemctl restart postfix
sudo systemctl restart dovecot

Explanation:

Virtual Domains: The virtual_mailbox_domains setting defines the domains handled by this Postfix setup.

Mailbox Locations: The virtual_mailbox_maps file (/etc/postfix/vmailbox) defines where the mail for each user/domain combination should be delivered.

Dovecot Configuration: Dovecot is configured to look up users in a password file and map them to their mail directories based on %d (domain) and %n (username).

There are other tutorials in case mine isn't comprehensive enough.

Good luck!

Max Haase
  • 1,123