Postfix
- 1
DNS records point to your server
- 2
Get a certificate from Let's Encrypt
- 3
Postfix fetches information from MariaDB
- 4
Dovecot fetches information from MariaDB
- 5
Postfix hands over emails to Dovecot
- 6
Dovecot saves the email to disk
Now that other mail servers can locate your mail server, the next step is to make sure it can receive and process incoming emails. This task is handled by Postfix, which communicates using the SMTP protocol.
Postfix knows if an email address is valid by checking for…
- Virtual Domains (am I responsible for this domain?)
- Virtual Aliases (do I have to redirect this address to another address?)
- Virtual Mailboxes (do I have a valid mailbox to store the email to?)
Virtual Domains
Section titled “Virtual Domains”There are different ways for Postfix to get that information. From text files, LDAP, MongoDB or PostgreSQL. Or via MariaDB – which is what we will use. But whatever kind of data source you configure, Postfix needs the information as a mapping. Consider it as a question and an answer.
- Question: is
example.orgone of our virtual domains? - Answer: yes
If the information is not available you just get no answer:
- Question: is
example.netone of our virtual domains? - Answer: (no answer)
In SQL language Postfix has to ask MariaDB the question:
SELECT "yes" FROM virtual_domains WHERE name='example.net';Run the following code in your shell to create a configuration file creating that mapping:
cat > /etc/postfix/mariadb-virtual-mailbox-domains.cf << EOFuser = mailserverpassword = MAILSERVER-PASSWORD-HEREhosts = 127.0.0.1dbname = mailserverquery = SELECT 1 FROM virtual_domains WHERE name='%s'EOFThis has created a configuration file called /etc/postfix/virtual-mailbox-domains.cf. The user, password, hosts
and dbname definitions tell Postfix how to connect to the database. And the query asks the question if a certain
domain is present in the database table virtual_domains. Using SELECT 1 we return just the number 1 if such an entry
was found. In fact it does not matter what we return. Any answer is fine.
Virtual Aliases
Section titled “Virtual Aliases”Now let’s deal with aliases. As shown earlier an alias redirects an email to another email address. So the question and answer game goes like this:
- Question: is there an alias for
jack@example.org? - Answer:
john@example.org
Postfix would then check if there are further aliases:
- Question: is there an alias for
john@example.org? - Answer: (no answer)
Apparently, there aren’t any more. So the email will go to john@example.org.
Run this code to create the appropriate mapping file:
cat > /etc/postfix/mariadb-virtual-alias-maps.cf << EOFuser = mailserverpassword = MAILSERVER-PASSWORD-HEREhosts = 127.0.0.1dbname = mailserverquery = SELECT destination FROM virtual_aliases WHERE source='%s'EOFThe query now gets all the destination email addresses from the database for a certain source email address.
Virtual Mailboxes
Section titled “Virtual Mailboxes”The last mapping we need is a query to find valid mailboxes. Without further ado:
cat > /etc/postfix/mariadb-virtual-mailbox-maps.cf << EOFuser = mailserverpassword = MAILSERVER-PASSWORD-HEREhosts = 127.0.0.1dbname = mailserverquery = SELECT 1 FROM virtual_users WHERE email='%s'EOFJust the same story as with virtual domains. The database returns 1 if there is a virtual_users record matching that
specific email address.
Actually the answer is usually not 1 but the path to the mailbox on disk. But that applies only if Postfix would be
saving the email to disk on its own. In our setup the email will be passed on to Dovecot in a later step. Dovecot will
then handle the files on disk. Postfix just has to know is whether a mailbox exists. And that’s why any answer is
sufficient here.
Tell Postfix
Section titled “Tell Postfix”You have created the config files for the three mappings. Now you just need to tell Postfix to use them:
postconf virtual_mailbox_domains=mysql:/etc/postfix/mariadb-virtual-mailbox-domains.cfpostconf virtual_mailbox_maps=mysql:/etc/postfix/mariadb-virtual-mailbox-maps.cfpostconf virtual_alias_maps=mysql:/etc/postfix/mariadb-virtual-alias-maps.cf
chown root:postfix /etc/postfix/mariadb-*.cfchmod o= /etc/postfix/mariadb-*.cfpostconf is a command that changes configuration in /etc/postfix/main.cf and applies them instantly. You don’t have
to restart Postfix.
The last two lines set the owner of the config files (user=root, group=postfix) and make sure that others have no access. After all a database password is found in these files.