postfix - Serving Up e-mail for Virtual Users


Table of Contents:


Introduction

[Ref: OpenBSD 4.0 release, postfix-2.3.2]

OpenBSD ships preconfigured with Sendmail http://www.Sendmail.org as the mail server (MTA.)

We wanted to use virtual user accounts for email for a number of reasons, and chose Postfix. This collection of notes will hopefully assist the new mail administrator into installing and verifying a virtual user accounts mail service.

Objectives

We've installed a few virtual user mail servers, through trial and error, even with the better guides out there, and hopefully these notes adds useful tests, log reviews during the install process to confidently reach a successful install every-time.

These guides will therefore install and test a

This installation exercise we are going to install three virtual domains on a single host, with three virtual accounts for each virtual domain:

Host myhost.example.org
Virtual Mail Base Directory /var/spool/postfix/vmail
Virtual Domain alpha.example.org ~ users: alfred, bob, charlie
Virtual Domain beta.example.org ~ users: auntie, bill, chou
Virtual Domain gamma.example.org ~ users: alistair, ben, cinder

Using OpenBSD's Postfix configuration, we will store the virtual mailboxes in: /var/spool/postfix/vmail

Requirements

An operational mail server requires a number of applications/services working together to provide the final 'product' that users take for granted as 'e-mail.' MOst of these tools are documented together with this documentation on installing, testing Postfix. The tools we are putting together for a smooth mail operation are:

The DNS Server

One infrastructure that is very key to ensuring that your email server really works, is the Global DNS. We do not show you how to configure your DNS so email can go to your box, or email is accepted from your box, but there are good tips on the Internet on what to do.

The Mail Transport Agent - Mail Server: Postfix

The rest of this documentation is dedicated to the installation, and configuration of Postfix as a Virtual User Account Mail Server.

postfix is found in the ports collection for OpenBSD, and you can get further information from the developer site http://www.postfix.org

POP3, IMAP Server for clients - Dovecot

The installation of a POP3, IMAP server to work with this virtual user account configuration of Postfix follows the link dovecot.htm.

We purposely provide that documentation separately to ensure we can :

Authentication Database - MySQL

Although virtual user accounts can be managed and created using text/hash database files, a large install can be managed better using a database server backend because other tools can interact with the database server, be used for managing users.

This documentation will describe how to configure the MySQL database server for virtual user accounts. Our documentation for installing and testing MySQL for OpenBSD follows the link mysql.htm.

GUI Management Tool - postfixadmin

postfixadmin is a wonderful Web GUI tool for managing your postfix installation.

This sample installation should work tested without the need for postfixadmin. I would advise installing postfixadmin for a simplified mechanism for managing virtual user accounts.

Our documentation for installing and configuring postfixadmin follows the link postfixadmin.htm

Installing Postfix

The dovecot, mysql flavor of Postfix is not normally available as a pre-built package, so the best way to get it is to install the ports tree and build the package manually.

Installing the Port

To build the package from ports, we go into the postfix/stable port and make a flavor.

# cd /usr/ports/mail/postfix/stable

# make show=FLAVORS

sasl2 ldap mysql pgsql dovecot

# env FLAVOR="mysql dovecot" make package

# env FLAVOR="mysql dovecot" make install

--- postfix-2.3.2-mysql-dovecot -------------------
-> Creating /etc/mailer.conf.postfix
-> Creating Postfix spool directory and chroot area under /var/spool/postfix

    Warning: you still need to edit myorigin/mydestination/mynetworks
    parameter settings in /etc/postfix/main.cf.

    See also http://www.postfix.org/faq.html for information about
    dialup sites or about sites inside a firewalled network.

    BTW: Check your /etc/mail/aliases file and be sure to set up
    aliases that send mail for root and postmaster to a real person,
    then run /usr/local/sbin/newaliases.

+---------------
| Configuration files has been installed in /etc/postfix.
| Please update these files to meet your needs.
+---------------
+---------------
| Postfix can be set up to replace Sendmail entirely. Please read the
| documentation at file:/usr/local/share/doc/postfix/html/index.html or
| http://www.postfix.org/ carefully before you decide to do this!
|
| To replace Sendmail with postfix you have to install a new mailer.conf
| using the following command:
|
|     /usr/local/sbin/postfix-enable
|
| If you want to restore Sendmail, this is done using the following command:
|
|     /usr/local/sbin/postfix-disable

The package build gives us a number of tasks to perform before we can assume that postfix is minimally installed.

  1. Read the documentation
  2. Enable Postfix using provided script
  3. Customise Postfix for your local install
  4. Enable automatic startup on System Restart (i.e. edit startup configuration file: /etc/rc.conf.local)
  5. Complete Disabling Sendmail (i.e. edit root's crontab to disable Sendmail)
  6. Verify alias configuration

1. Read the documentation

The documentation is made available in html format so let's put it into our webspace for future reading.

# mkdir -p /var/www/htdocs/manual
# cp -R /usr/local/share/doc/postfix/html /var/www/htdocs/manual/postfix

If you've previously enabled the standard OpenBSD apache distribution then you should now be able to browse the Postfix documentation locally at http://www.example.org/manual/postfix/. If you have enabled the Apache server and have no intentions of doing so, then you can read the official documentation at http://www.postfix.org/docs.html.

2. Enable Postfix using OpenBSD provided script postfix-enable

The OpenBSD port provides a script 'postfix-enable' that will back-up the standard Sendmail installation, and install postfix.

# /usr/local/sbin/postfix-enable
old /etc/mailer.conf saved as /etc/mailer.conf.pre-postfix
postfix /etc/mailer.conf enabled

NOTE: do not forget to add Sendmail_flags="-bd" to
      /etc/rc.conf.local to startup postfix correctly.

NOTE: do not forget to add "-a /var/spool/postfix/dev/log" to
      syslogd_flags in /etc/rc.conf.local and restart syslogd.

NOTE: do not forget to remove the "Sendmail clientmqueue runner"
      from root's crontab.

3. Customise Postfix for your local install

[ref: http://www.postfix.org/INSTALL.html#mandatory ]

To customise Postfix for our local install, we need to modify Postfix's main configuration file: /etc/postfix/main.cf

File Fragment: /etc/postfix/main.cf

myhostname = myhost.example.org
mydomain = example.org
myorigin = $mydomain
alias_database = hash:/etc/postfix/aliases
smtpd_banner = $myhostname ESMTP $mail_name
parent_domain_matches_subdomains =

Notes:

You can use mynetworks to set your network. If you don't know how to make this setting, you can leave it and postfix will automatically set the known ip addresses on your servers configuration.

[ref: http://www.postfix.org/postconf.5.html#parent_domain_matches_subdomains ]

parent_domain_matches_subdomains (default: see "postconf -d" output)
What Postfix features match subdomains of "domain.tld" automatically, instead of requiring an explicit ".domain.tld" pattern. This is planned backwards compatibility: eventually, all Postfix features are expected to require explicit ".domain.tld" style patterns when you really want to match subdomains.

After making the above changes, we need to rebuild the 'hash' files with the following commands.

# /usr/local/sbin/postalias /etc/aliases

# /usr/local/sbin/newaliases

Likewise, we need to run the above commands after changes to related files (for example: /etc/aliases or /etc/postfix/aliases)

After postfix has been started, you can then use "postconf | grep mynetworks" as a basis for fine-tuning your configuration.

4. Enable automatic startup on System Restart

To enable Postfix to start with each system start, we make the following edits to the startup configuration file: /etc/rc.conf.local

You should have something like the following in your /etc/rc.conf.local

File: /etc/rc.conf.local

syslogd_flags="-a /var/spool/postfix/dev/log"
Sendmail_flags="-bd"

Explaining the "-a /var/spool/postfix/dev/log" (from the man pages)

syslogd(8)

syslogd reads and logs messages to the system console, log files, other machines and/or users as specified by its configuration file.

-a path
Specify a location where syslogd should place an additional log socket. Up to about 20 additional logging sockets can be speci-
fied. The primary use for this is to place additional log sockets in /dev/log of various chroot filespaces.

Explaining Sendmail -bd (from the man pages)

Sendmail(8)

 -bd Run as a daemon. Sendmail will fork and run in the background listening on socket 25 for incoming SMTP connections. By default, Sendmail will also listen on socket 587 for RFC 2476 message submission. This is normally run from /etc/rc.

 

5. Complete Disabling Sendmail

To complete the installation of Postfix, and disabling of Sendmail, we need to edit root's crontab and disable supplied Sendmail behaviour

To be safe, you should just comment out the relevant line, (just in case you need or want to go back to Sendmail.) We use "crontab -e" and add "#" hashes to 'comment' out the execution of the Sendmail line shown below.

# crontab -e

#minute hour mday month wday command
#
# Sendmail clientmqueue runner
#*/30 * * * * /usr/sbin/Sendmail -L sm-msp-queue -Ac -q

6. Verify alias configuration

Take a look at your /etc/postfix/aliases file which will contain some default aliases that you should manage.

alias_database = hash:/etc/postfix/aliases

For a basic, test install then there shouldn't be any real need to change this file. You should remember when you're ready for a full install then you should review this file for aliases such as root, postmaster, webmaster and ensure they are routed to the correct 'person.'

From the file: The program "newaliases" must be run after this file is updated for any changes to show through to Postfix.

 # /usr/local/sbin/newaliases

Starting Postfix

[ref: postfix(1)]

Now, we are ready to make some fundamental tests, so let's start Postfix which at this stage is a nice 5 step process.

  1. Restart syslogd (using pkill -HUP)
  2. Kill existing session of Sendmail (using pkill)
  3. Rebuild the alias file (using newaliases)
  4. Check files and configuration (using postfix check)
  5. Start postfix (using our 'new' Sendmail)
# pkill -HUP syslogd
# pkill Sendmail
# /usr/local/sbin/newaliases
# /usr/local/sbin/postfix check
# /usr/local/sbin/Sendmail -bd -q30m
postfix/postfix-script: starting the Postfix mail system

1. Restart syslogd

We are sending the SIGHUP (hangup) to syslogd, from the man page.

syslogd(8)

syslogd reads its configuration file when it starts up and whenever it receives a hangup signal.

2 Kill existing session of Sendmail

We want to force the Sendmail program to die, from the man page

pkill(1)

The pkill command searches the process table on the running system and signals all processes that match the criteria given on the command line.

The default signal TERM is sent when no other signal is specified, so we're just telling Sendmail to die.

3. Rebuild the alias file

Be careful to specify the full path with these commands. Remember that we have not deleted the old files from the original Sendmail installation, so it is very important that we use the full path of the programs /usr/local/sbin where postfix commands have been installed. If you do  not use the full path, then we do not know, but you will most likely be running the OpenBSD Sendmail installation, which is not what we want here.

/usr/local/sbin/newaliases

4. Check files and configuration

[Ref: postconf(1), postfix(1)]

Postfix comes with rudimentary testing of file (using postfix check) and configuration settings(using postconf), so its a good habit to give it a test run before doing anything else.

The first quick test can be performed using the postfix command

postfix(1)

The following commands are implemented:

check Warn about bad directory/file ownership or permissions, and create missing directories.

Essentially, just run the program and if it doesn't give you error messages, then we are one step closer with 'fewer' errors in our setup.

# postfix check

The second test can be performed using the postconf "Postfix configuration utility" , from the man pages

postconf(1)

-n Print parameter settings that are not left at their built-in default value, because they are explicitly specified in main.cf.

This essentially lets us quickly find out any blatant errors. For example, an output could look like this.

# postconf | grep ^my
mydestination = $myhostname, localhost.$mydomain, localhost
mydomain = example.org
myhostname = myhost.example.org
mynetworks = 127.0.0.0/8 public_ip/23 192.168.1.0/24 192.168.2.0/24 [::1]/128 ...IPV6_Addresses
mynetworks_style = subnet
myorigin = $mydomain

A quick perusal of the postconf output should give us an idea if we forgot or incorrectly put some information in.

Using "postconf -n" is a good way to check for typing mistakes that can lead to many lost hours due the system being misconfigured and we're still trying to solve a problem with the wrong expections because the settings we placed in the configuration have not been set because of a typing mistake.

At this point in our install, there has been no serious changes to the configuration files.

5. Start postfix

After the above testing, validation, we should be able to start postfix with the postfix command, or in our example we will use the 'new' Sendmail command.

# /usr/local/sbin/Sendmail -bd -q30m

Testing the mail server

[ref: The Network People, Inc. Mail Server Testing ]

We should now be able to test whether the server's 'face' to the world (smtp) is working.

To simplify testing, we will perform the tests on server itself. Where possible/practical, you should also run the tests from an external client to verify expected behaviour with an active firewall or other systems between your Postfix/SMTP Server and your clients.

This test procedure will only test a few basic commands, writing myself a message, my system user account is samt (and you can use any valid user account on the system)

telnet localhost smtp

Screen Session

$ telnet localhost smtp
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 myhost.example.org ESMTP Postfix
EHLO example.org
250-myhost.example.org
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
MAIL FROM: <samt@example.org>
250 2.1.0 Ok
RCPT TO: <samt@example.org>
250 2.1.5 Ok
DATA
354 Enter mail, end with "." on a line by itself
Subject: This is my subject line

I continue writing until I'm out of interesting things to say
which is not that far away

.
250 2.0.0 Ok: queued as 699ACBA2D7
QUIT
221 2.0.0 Bye
Connection closed by foreign host.

I've just used capital letters for the SMTP commands, but obviously they work fine with lowercase.

If your server is not yet online with a valid DNS record, then you can test using RCPT TO: samt@localhost.

Mail Log

The corresponding log messages will look something like the below.

Screen Session

# tail -f /var/log/maillog

starting the Postfix mail system
daemon started -- version 2.3.2, configuration /etc/postfix
connect from localhost[::1]
5E4A5BA2D4: client=localhost[::1]
5E4A5BA2D4: message-id=<20061212080251.5E4A5BA2D4@hostname.example.org>
5E4A5BA2D4: from=<samt@example.org>, size=457, nrcpt=1 (queue active)
5E4A5BA2D4: to=<samt@example.org>, relay=local, delay=77, delays=77/0.05/0/0.03, dsn=2.0.0, status=sent (delivered to mailbox)
5E4A5BA2D4: removed
disconnect from localhost[::1]

'tail' is a unix program to look at the recent additions to a file, and in our case we're looking at the log file for 'mail' related programs. Using the "-f" parameter tells 'tail' to continue looking at the recent additions to the file (such that updates to the file are displayed on the screen for us.) Use Ctrl+C (i.e. hold the Ctrl key while pressing C) to break out of the log review session shown above

mail(1)

[Ref: mail(1) ]

While we're testing with real system user accounts, we can use the unix 'mail' program to check our mail message.

Screen Session

# /usr/bin/mail -u samt

Mail version 8.1.2 01/15/2001. Type ? for help.
"/var/mail/samt": 1 message 1 new
>N 1 samt@example.org Tue Dec 12 21:03 18/605 This is my subject line

& more 1

Message 1:
From samt@example.org Tue Dec 12 21:03:54 2006
X-Original-To: samt@myhost.example.org
Delivered-To: samt@myhost.example.org
Subject: This is my subject line
From: samt@example.org
To: undisclosed-recipients:;

I continue writing until I'm out of interesting things to say
which is not that far away

& q
Saved 1 message in mbox

In the above example, we enter mail for the user samt ("-u samt") and the 'mail' client shows a list of current email for user 'samt' and then gives us the "&" ampersand prompt.

We can read the email message by typing the message number, and 'mail' supports the use of a screen 'pager' such as 'more' so that we can scroll through longer messages.

Quit. We quit out of 'mail' using the 'q' command.

The above reference to the log files and mail client is to provide you with more tools for validating your installation.

Summary

We now have a fully functional email server that can receive email messages, and store those messages for users.

Configuring a Virtual Email Service - basic test install

[ref: Postfix Virtual Domain Hosting Howto http://www.example.org/manual/postfix/VIRTUAL_README.html ]

I've always had difficulty in getting the full featured database driven virtual email working, so we will go through a slow installation process of installing the non-database driven version first to make sure all other configuration items are correct within Postfix.

  1.  Base Configuration for virtual hosting
  2. Create system user account for managing virtual mail
  3. Virtual Mail Accounts

1. Base Configuration for virtual hosting

Main Configuration

We'll put in some basic configuration information for virtual hosting into Postfix's main.cf

File Fragment: /etc/postfix/main.cf

###### Virtual Mailbox Services - Local

mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
                       mail.$mydomain, www.$mydomain, ftp.$mydomain,

virtual_mailbox_base = /var/spool/postfix/vmail
virtual_mailbox_domains = hash:/etc/postfix/virtual/mailbox/domains
virtual_mailbox_maps = hash:/etc/postfix/virtual/mailbox/alpha.example.org,
                                  hash:/etc/postfix/virtual/mailbox/beta.example.org,
                                  hash:/etc/postfix/virtual/mailbox/gamma.example.org
virtual_minimum_uid = 900
virtual_transport = virtual
virtual_uid_maps = static:901
virtual_gid_maps = static:901

Notes:

Disk Layout for Virtual Domains

We need to layout our files mentioned in the configuration file above and I have chosen the following which is hopefully scaleable if you want to use this as the basis (ignoring the simpler database solution reviewed later.)

Screen Session

# mv /etc/postfix/virtual /etc/postfix/virtual_aliases
# mkdir -p /etc/postfix/virtual/mailbox

# mkdir -p /etc/postfix/virtual/aliases

# mv /etc/postfix/virtual_aliases /etc/postfix/virtual/aliases/common
# touch /etc/postfix/virtual/mailbox/domains
# touch /etc/postfix/virtual/mailbox/alpha.example.org
# touch /etc/postfix/virtual/mailbox/beta.example.org
# touch /etc/postfix/virtual/mailbox/gamma.example.org

We move the current virtual alias file from /etc/postfix/virtual to /etc/postfix/virtual/aliases.

Virtual Domains

We specify for postfix which virtual domains we want it to receive email with the following configuration option:

File Fragment: /etc/postfix/main.cf

virtual_mailbox_domains = hash:/etc/postfix/virtual/domains

File:/etc/postfix/virtual/mailbox/domains

alpha.example.org     IGNORED_PARAMETER

beta.example.org       IGNORED_PARAMETER

gamma.example.org  IGNORED_PARAMETER

After creating or making any changes to the above domains file, recreate the hash database using postmap

# /usr/local/sbin/postmap /etc/postfix/virtual/mailbox/domains

Virtual Mailbox

For OpenBSD, the default chroot'd postfix installation stores its files in /var/spool/postfix so we'll specify the location for virtual email accounts within that structure.

File Fragment: /etc/postfix/main.cf

virtual_mailbox_base = /var/spool/postfix/vmail

When setting up virtual mailboxes (in this manner), it makes sense to structure the directories for scalability and to prevent clashing namespaces. Prior to setting up accounts we'll consider that our mailbox accounts will be structured by domain. For example:

We can now create some sample user accounts into our virtual mailbox

File Fragment: /etc/postfix/main.cf

virtual_mailbox_maps = hash:/etc/postfix/virtual/mailbox/alpha.example.org,
                                  hash:/etc/postfix/virtual/mailbox/beta.example.org,
                                  hash:/etc/postfix/virtual/mailbox/gamma.example.org

Obviously, each valid user needs a corresponding mailbox storage space. The mailbox file is specified relative to the virtual_mailbox_base shown above and since we already have our directory design structure above, we can go ahead and create some accounts.

Virtual Accounts - alpha.example.org
File: /etc/postfix/virtual/mailbox/alpha.example.org

#account                          --> Storage location

alfred@alpha.example.org        alpha.example.org/alfred/
bob@alpha.example.org           alpha.example.org/bob/
charlie@alpha.example.org       alpha.example.org/charlie/

After creating or making any changes to the above alpha.example.org file, recreate the hash database using postmap

# /usr/local/sbin/postmap /etc/postfix/virtual/mailbox/alpha.example.org

Virtual Accounts - beta.example.org

 

File: /etc/postfix/virtual/mailbox/beta.example.org

#account                          --> Storage location

auntie@beta.example.org          beta.example.org/auntie/
bill@beta.example.org               beta.example.org/bill/
chou@beta.example.org            beta.example.org/chou/

After creating or making any changes to the above beta.example.org file, recreate the hash database using postmap

# /usr/local/sbin/postmap /etc/postfix/virtual/mailbox/beta.example.org

Virtual Accounts - gamma.example.org

 

File: /etc/postfix/virtual/mailbox/gamma.example.org

#account                          --> Storage location

alistair@gamma.example.org      gamma.example.org/alistair/
ben@gamma.example.org         gamma.example.org/ben/
cinder@gamma.example.org      gamma.example.org/cinder/

After creating or making any changes to the above gamma.example.org file, recreate the hash database using postmap

# /usr/local/sbin/postmap /etc/postfix/virtual/mailbox/gamma.example.org

We must now tell postfix to re-read its configuration files, by using postfix reload.

# /usr/local/sbin/postfix reload

Mailbox files (above) can use either mbox or maildir format. To use maildir format, include a slash at the end of the filename. For a discussion of the relative differences you can follow the link to: Benchmarking mbox versus maildir, in short if your have a modern Unix OS (post 2004?) you should not have any problems using maildirs as an efficient scalable system. But read the benchmark and search the web for your own edification.

I have chosen for this example to use separate files per domain, merely for illustration of the flexibility of the system (and if you are insane enough to manage it manually you can at least let the file structure assist you in some manner.)

NOT WORKING YET.

Virtual Aliases (broken)

NOT WORKING YET.

Do not use yet.

I am not yet sure how virtual aliasing work, or its restrictions but below documents some thinking of how to install it. Unfortunately I haven't found any workable samples on the Internet and likewise tools such as webmin do not use virtual_alias_maps (as far as I can read.) While, the same below configuration (hash) works fine when used in combinaton with mysql for all other virtual_alias_*

Note: With this exercise, virtual_alias_maps works fine using mysql (and thus postfixadmin)

We can also create some aliases for virtual accounts

File Fragment: /etc/postfix/main.cf

virtual_alias_maps = hash:/etc/postfix/virtual/alias/alpha.example.org,
                              hash:/etc/postfix/virtual/alias/beta.example.org,
                              hash:/etc/postfix/virtual/alias/gamma.example.org

Virtual Alias - alpha.example.org
File: /etc/postfix/virtual/alias/alpha.example.org

 

alpha.example.org                     IGNORED_PARAMETER

postmaster@alpha.example.org    alfred@alpha.example.org
sales@alpha.example.org             alfred@alpha.example.org
marketing@alpha.example.org       bob@alpha.example.org
info@alpha.example.org               bob@alpha.example.org 
accounting@alpha.example.org     charlie@alpha.example.org 

After creating or making any changes to the above alias/alpha.example.org file, recreate the hash database using postmap

# /usr/local/sbin/postmap /etc/postfix/virtual/alias/alpha.example.org

# /usr/local/sbin/postfix reload

Virtual Alias - beta.example.org
File: /etc/postfix/virtual/alias/beta.example.org

 

beta.example.org                     IGNORED_PARAMETER

postmaster@beta.example.org    auntie@beta.example.org
sales@beta.example.org             auntie@beta.example.org, bill@beta.example.org
marketing@beta.example.org       bill@beta.example.org
info@beta.example.org               bill@beta.example.org 
accounting@beta.example.org     chou@beta.example.org 

After creating or making any changes to the above alias/beta.example.org file, recreate the hash database using postmap

# /usr/local/sbin/postmap /etc/postfix/virtual/alias/beta.example.org

# /usr/local/sbin/postfix reload

Virtual Alias - gamma.example.org
File: /etc/postfix/virtual/alias/gamma.example.org

 

gamma.example.org                     IGNORED_PARAMETER

postmaster@gamma.example.org    alistair@gamma.example.org
sales@gamma.example.org             alistair@gamma.example.org, ben@gamma.example.org
marketing@beta.example.org       ben@gamma.example.org
info@gamma.example.org               ben@gamma.example.org 
accounting@gamma.example.org     cinder@gamma.example.org 

After creating or making any changes to the above alias/gamma.example.org file, recreate the hash database using postmap

# /usr/local/sbin/postmap /etc/postfix/virtual/alias/gamma.example.org

We must now tell postfix to re-read its configuration files, by using postfix reload.

# /usr/local/sbin/postfix reload

 

2. Create the system user account for managing virtual mail

[ref: http://www.postfix.org/postconf.5.html#virtual_uid_maps, http://www.postfix.org/postconf.5.html#virtual_gid_maps]

Mail delivery happens with the recipient's UID/GID privileges specified with virtual_uid_maps and virtual_gid_maps, therefore the virtual mailbox files must be owned by a system user account and associated with a  group on your system. Fortunately Postfix is flexible to allow each mailbox to be owned by a unique system user account or by a single system user account for all domains, and even one system user account per domain. This is set by using the virtual_uid_maps and virtual_gid_maps setting.

virtual_uid_maps = static:901
virtual_gid_maps = static:901

The 'static' map type tells Postfix that you want the uid/gid to be for all accounts.

We can now create the system user account to manage virtual email mailboxes.

Screen Session

# useradd -d /var/spool/postfix/vmail -g=uid -u 901 -s /sbin/nologin -m -c "Virtual Mailbox Owner" _vmail
# chmod -R 770 /var/spool/postfix/vmail

A by-product of the user/group creation is that the "base" directory will also be created with the correct permissions.

If we wanted to use different users, groups for managing mailboxes, then we could have used a lookup file instead.

virtual_uid_maps = hash:/etc/postfix/virtual_uids
virtual_gid_maps = hash:/etc/postfix/virtual_gids

Ensure the standard (non-virtual) alias file is built by using Postfix's newaliases.

# /usr/local/sbin/newaliases

Restart Postfix

# /usr/local/sbin/postfix stop

# /usr/local/sbin/postfix start

3. Testing Configuration

postconf

Use postconf -n to compare whether what we expect in virtual_* parameter settings is what is running on the system.

Screen Session

# postconf | grep ^virtual

virtual_alias_domains = $virtual_alias_maps
virtual_alias_expansion_limit = 1000
virtual_alias_maps = $virtual_maps
virtual_alias_recursion_limit = 1000
virtual_destination_concurrency_limit = $default_destination_concurrency_limit
virtual_destination_recipient_limit = $default_destination_recipient_limit
virtual_gid_maps = static:901
virtual_mailbox_base = /var/spool/postfix/vmail
virtual_mailbox_domains = hash:/etc/postfix/virtual/domains
virtual_mailbox_limit = 51200000
virtual_mailbox_lock = fcntl
virtual_mailbox_maps = hash:/etc/postfix/virtual/mailbox/alpha.tbu.to,
                                  hash:/etc/postfix/virtual/mailbox/beta.tbu.to,
                                  hash:/etc/postfix/virtual/mailbox/gamma.tbu.to
virtual_minimum_uid = 900
virtual_transport = virtual
virtual_uid_maps = static:901

 

telnet localhost smtp

Remember to use the /var/log/maillog file to validate postfix has started without errors. You can also repeat the above 'telnet localhost smtp' to review nothing has drastically broken.

Screen Session

$ telnet localhost smtp

Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 myhost.example.org ESMTP Postfix (2.3.2)
ehlo example.org
250-myhost.example.org
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

mail from: <samt@example.org>

250 2.1.0 Ok

rcpt to: <alfred@alpha.example.org>

250 2.1.5 Ok

rcpt to: <auntie@beta.example.org>

250 2.1.5 Ok

rcpt to: <alistair@gamma.example.org>

250 2.1.5 Ok

data

354 End data with <CR><LF>.<CR><LF>

Subject: Welcome Virtual Users

Hopefully you are all virtually OK.

Welcome to email

.

250 2.0.0 Ok: queued as BA1FC5A950

quit

221 2.0.0 Bye
Connection closed by foreign host.

Mail Log

The corresponding /var/log/maillog entry should look something like the following

File: /var/log/maillog

connect from unknown[::1]
client=unknown[::1]
message-id=<20070208214647.BA1FC5A950@myhost.example.org>
from=<samt@example.org>, size=393, nrcpt=3 (queue active)
to=<alfred@alpha.example.org>, relay=virtual, delay=69, delays=67/0.05/0/1.8, dsn=2.0.0, status=sent (delivered to maildir)
to=<auntie@beta.example.org>, relay=virtual, delay=69, delays=67/0.05/0/1.9, dsn=2.0.0, status=sent (delivered to maildir)
to=<alistair@gamma.example.org>, relay=virtual, delay=69, delays=67/0.14/0/1.9, dsn=2.0.0, status=sent (delivered to maildir)
removed
disconnect from unknown[::1]

 

Mail Store

We should also be able to see evidence of the virtual account mails in the file system such as has occurred on this installation.

Screen Session

# ls -l /var/spool/postfix/vmail/alpha.example.org/alfred/new/

total 4
-rw------- 1 _vmail _vmail 481 Feb 9 10:47 1170971257.V5I5a95aM294234.myhost.example.org

cat /var/spool/postfix/vmail/alpha.example.org/alfred/new/1170971257.V5I5a95aM294234.myhost.example.org

Return-Path: <samt@example.org>
X-Original-To: alfred@alpha.example.org
Delivered-To: alfred@alpha.example.org
Received: from example.org (unknown [IPv6:::1])
by myhost.example.org (Postfix) with ESMTP id BA1FC5A950;
Fri, 9 Feb 2007 10:46:30 +1300 (TOT)
Subject: Welcome Virtual Users
Message-Id: <20070208214647.BA1FC5A950@myhost.example.org>
Date: Fri, 9 Feb 2007 10:46:30 +1300 (TOT)
From: samt@example.org
To: undisclosed-recipients:;

 

Hopefully you are all virtually OK.

 

Welcome to email

 

We can likewise confirm the same message was received for aunti@beta.example.org and alistair@gamma.example.org.

Configuring a Virtual Email Service - MySQL

Mischa Peters at high5.net has created a great tool for managing virtual user email accounts based on Postfix. We will look at installing and testing the foundation database configuration here.

To minimise tools being reviewed for debugging, we're going to attempt installing MySQL support, using the postfixadmin data tables, but without installing or using postfixadmin.

Configuring MySQL

The following notes differs from a standard postfixadmin install in how it uses usernames, largely because it simplifies things for my understanding. The whole process has helped me to better understand the interactions between these different applications, finding methods for debugging installation problems. I hope it also simplifies for our understanding.

Please refer to our MySQL notes for how to install MySQL for OpenBSD.

Following Mischa's instructions at Postfix Wiki, Virtual Users and Domains we'll take a look at:

Much of these database settings are straight out of the postfixadmin/DATABASE_MYSQL.TXT file with slight/inane modifications where it helps me find things more legible.

The key differentiators between these database instructions than the default install are as follows:

Minor quibbles but makes the install instructions slightly more legible?

Creating the database

We will first log into the mysql server with an account that has root/administrator privileges and insert (copy/paste) sql commands below.

Screen Session

$ mysql -u root -p

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 41 to server version: 5.0.24a-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

The rest of the 'greyed' instructions can be copy/pasted into your MySQL monitor above. Be sure to change your usernames and passwords as appropriate.

The first thing we want is to tell mysql that we want to modify the records relating to user accounts for the database server.

mysql client session

USE mysql;

Next, we want to create some new settings for a new user 'postfixserver' that we want to designate for use by the postfix server.

mysql client session

INSERT INTO user (Host, User, Password) VALUES ('localhost','postfixserver',password('postfixserver'));
INSERT INTO db (Host, Db, User, Select_priv) VALUES ('localhost','mail','postfixserver','Y');

Next, we want to create a new user 'postfixadmin' that we want to designate for use by the postfixadmin application.

mysql client session

INSERT INTO user (Host, User, Password) VALUES ('localhost','postfixadmin',password('postfixadmin'));
INSERT INTO db (Host, Db, User, Select_priv, Insert_priv, Update_priv, Delete_priv) VALUES ('localhost', 'mail', 'postfixadmin', 'Y', 'Y', 'Y', 'Y');

To ensure that these new user settings have been loaded into use, we flush the settings.

mysql client session

FLUSH PRIVILEGES;

Now, we want to set privileges for the database that we will be using.

mysql client session

GRANT USAGE ON mail.* TO postfixserver@localhost;
GRANT SELECT, INSERT, DELETE, UPDATE ON mail.* TO postfixserver@localhost;

GRANT USAGE ON mail.* TO postfixadmin@localhost;
GRANT SELECT, INSERT, DELETE, UPDATE ON mail.* TO postfixadmin@localhost;

Next, we create the database itself.

mysql client session

CREATE DATABASE mail;

The next stage is to create the relevant tables and some dummy/sample data.

Creating the Alias table

The alias table will store/retrieve our virtual aliases (which I have not yet

mysql client session

USE mail;

CREATE TABLE `alias` (
`address` varchar(255) NOT NULL default '',
`goto` text NOT NULL,
`domain` varchar(255) NOT NULL default '',
`created` datetime NOT NULL default '0000-00-00 00:00:00',
`modified` datetime NOT NULL default '0000-00-00 00:00:00',
`active` tinyint(1) NOT NULL default '1',
PRIMARY KEY (address),
KEY address (`address`)
) TYPE=MyISAM COMMENT='Postfix Admin - Virtual Aliases';

 

Creating the Domain table

The domain table will store/retrieve our virtual domains

mysql client session

CREATE TABLE `domain` (
`domain` varchar(255) NOT NULL default '',
`description` varchar(255) NOT NULL default '',
`aliases` int(10) NOT NULL default '0',
`mailboxes` int(10) NOT NULL default '0',
`maxquota` int(10) NOT NULL default '0',
`quota` int(10) NOT NULL default '0',
`transport` varchar(255) default NULL,
`backupmx` tinyint(1) NOT NULL default '0',
`created` datetime NOT NULL default '0000-00-00 00:00:00',
`modified` datetime NOT NULL default '0000-00-00 00:00:00',
`active` tinyint(1) NOT NULL default '1',
PRIMARY KEY (`domain`),
KEY domain (`domain`)
) TYPE=MyISAM COMMENT='Postfix Admin - Virtual Domains';

Creating the Mailbox table

The mailbox table will store/retrieve the usernames, passwords, and file directories

mysql client session

USE mail;

CREATE TABLE `mailbox` (
`username` varchar(255) NOT NULL default '',
`password` varchar(255) NOT NULL default '',
`name` varchar(255) NOT NULL default '',
`maildir` varchar(255) NOT NULL default '',
`quota` int(10) NOT NULL default '0',
`domain` varchar(255) NOT NULL default '',
`created` datetime NOT NULL default '0000-00-00 00:00:00',
`modified` datetime NOT NULL default '0000-00-00 00:00:00',
`active` tinyint(1) NOT NULL default '1',
PRIMARY KEY (`username`),
KEY username (`username`)
) TYPE=MyISAM COMMENT='Postfix Admin - Virtual Mailboxes';

 

Other Tables for postfixadmin

The database is now created, and we might as well configure the other tables used by postfixadmin

mysql client session

USE mail;

CREATE TABLE `admin` (
`username` varchar(255) NOT NULL default '',
`password` varchar(255) NOT NULL default '',
`created` datetime NOT NULL default '0000-00-00 00:00:00',
`modified` datetime NOT NULL default '0000-00-00 00:00:00',
`active` tinyint(1) NOT NULL default '1',
PRIMARY KEY (`username`),
KEY username (`username`)
) TYPE=MyISAM COMMENT='Postfix Admin - Virtual Admins';

 

mysql client session

USE mail;

CREATE TABLE `domain_admins` (
`username` varchar(255) NOT NULL default '',
`domain` varchar(255) NOT NULL default '',
`created` datetime NOT NULL default '0000-00-00 00:00:00',
`active` tinyint(1) NOT NULL default '1',
KEY username (`username`)
) TYPE=MyISAM COMMENT='Postfix Admin - Domain Admins';



USE mail;

CREATE TABLE `log` (
`timestamp` datetime NOT NULL default '0000-00-00 00:00:00',
`username` varchar(255) NOT NULL default '',
`domain` varchar(255) NOT NULL default '',
`action` varchar(255) NOT NULL default '',
`data` varchar(255) NOT NULL default '',
KEY timestamp (`timestamp`)
) TYPE=MyISAM COMMENT='Postfix Admin - Log';

 

mysql client session

USE mail;

#
# Table structure for table vacation
#
CREATE TABLE `vacation` (
`email` varchar(255) NOT NULL default '',
`subject` varchar(255) NOT NULL default '',
`body` text NOT NULL default '',
`cache` text NOT NULL default '',
`domain` varchar(255) NOT NULL default '',
`created` datetime NOT NULL default '0000-00-00 00:00:00',
`active` tinyint(1) NOT NULL default '1',
PRIMARY KEY (`email`),
KEY email (`email`)
) TYPE=MyISAM COMMENT='Postfix Admin - Virtual Vacation';

Populating the tables

We will populate our database with some test data that you can easily remove later using postfixadmin. By using our sample data below we avoid having to configure postfixadmin to have a working test server.

We will replicate our virtual user system used with the above hash files into our MySQL database.

Super Administrator Account.

This is largely relevant only if you will be installing postfixadmin, and can be skipped.

Now, here's one part where the standard documentation always got me lost. The standard instructions provides the below image which will work for logging into the system, but will cause other problems. Instead of the following instructions

# superadmin user & password (login: admin@domain.tld, password: admin)
INSERT INTO domain_admins (username, domain, active) VALUES ('admin@domain.tld','ALL','1');
INSERT INTO admin (username, password, active) VALUES ('admin@domain.tld','$1$0fec9189$bgI6ncWrldPOsXnkUBIjl1','1');

We will be using the following instructions which uses CRYPT instead of postfixadmin's md5crypt for encrypting the password to:

mysql client session

USE mail;

INSERT INTO admin (username, password, active) VALUES ('admin','6dwLx9NTxhTjU','1');

INSERT INTO domain_admins (username, domain, active) VALUES ('admin','ALL','1');

When installing postfixadmin, from the above settings, we set:

File: /var/www/htdocs/postfixadmin/config.inc.php

$CONF['encrypt'] = 'system';

 

Virtual Domains

We will be creating virtual domains for our three sample domains:

Creating our virtual domains

mysql client session

USE mail;

INSERT INTO domain (domain,description,aliases,mailboxes,maxquota,quota,transport,backupmx,active) VALUES ('alpha.example.org', 'Alpha Tester','10','10', '0','0','virtual', '0','1');

INSERT INTO domain (domain,description,aliases,mailboxes,maxquota,quota,transport,backupmx,active) VALUES ('beta.example.org', 'Beta Site','10','10', '0','0','virtual', '0','1');

INSERT INTO domain (domain,description,aliases,mailboxes,maxquota,quota,transport,backupmx,active) VALUES ('gamma.example.org', 'Gamma Born','10','10', '0','0','virtual', '0','1');

We can verify that the data has been entered correctly with the following simple test. From the command prompt, start mysql.

Screen Session

# mysql -u root -p

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 94 to server version: 5.0.24a-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use mail;

Database changed

mysql> select domain, transport from domain;

+--------------+-----------+
| domain | transport |
+--------------+-----------+
| alpha.example.org | virtual |
| beta.example.org | virtual |
| gamma.example.org | virtual |
+--------------+-----------+
3 rows in set (0.00 sec)

Virtual Mailbox

As from our example above, we will be creating virtual mailboxes (virtual user accounts) for our above three sample domains:

Virtual Users for alpha.example.org

Creating our virtual users for alpha.example.org : password is username

mysql client session

USE mail;

INSERT INTO mailbox (username,password,name,maildir,quota,domain,active) VALUES ('alfred@alpha.example.org','82fU0EHEzA6wo', 'Alfred','alpha.example.org/alfred@alpha.example.org/', '0','alpha.example.org','1');

INSERT INTO mailbox (username,password,name,maildir,quota,domain,active) VALUES ('bob@alpha.example.org','1bdyGcAE/JC0I', 'Bob','alpha.example.org/bob@alpha.example.org/', '0','alpha.example.org','1');

INSERT INTO mailbox (username,password,name,maildir,quota,domain,active) VALUES ('charlie@alpha.example.org','048qvFjqS3zBc', 'Charlie','alpha.example.org/charlie@alpha.example.org/', '0','alpha.example.org','1');

Virtual Users for beta.example.org

Creating our virtual users for beta.example.org : password is username

mysql client session

USE mail;

INSERT INTO mailbox (username,password,name,maildir,quota,domain,active) VALUES ('auntie@beta.example.org', '3336RmmvRQ0NU', 'Auntie','beta.example.org/auntie@beta.example.org/', '0','beta.example.org','1');

INSERT INTO mailbox (username,password,name,maildir,quota,domain,active) VALUES ('bill@beta.example.org', 'fbVsBHcPJVVjU', 'Bill','beta.example.org/bill@beta.example.org/', '0','beta.example.org','1');

INSERT INTO mailbox (username,password,name,maildir,quota,domain,active) VALUES ('chou@beta.example.org', '359nFQg1J.8nc', 'Chou','beta.example.org/chou@beta.example.org/', '0','beta.example.org','1');

Virtual Users for gamma.example.org

Creating our virtual users for gamma.example.org : password is username

mysql client session

USE mail;

INSERT INTO mailbox (username,password,name,maildir,quota,domain,active) VALUES ('alistair@gamma.example.org', '12XeQqcTNk3YU', 'Alistair','gamma.example.org/alistair@gamma.example.org/', '0','gamma.example.org','1');

INSERT INTO mailbox (username,password,name,maildir,quota,domain,active) VALUES ('ben@gamma.example.org', '1bKpSqtESjdck', 'Ben','gamma.example.org/ben@gamma.example.org/', '0','gamma.example.org','1');

INSERT INTO mailbox (username,password,name,maildir,quota,domain,active) VALUES ('cinder@gamma.example.org', '19rP1zls.evZQ', 'Cinder','gamma.example.org/cinder@gamma.example.org/', '0','gamma.example.org','1');

Verify our settings

Remember to change the user domains in the above to your specific virtual domain(s). You can use an sql query such as the below to help verify that you are not using the *.example. domains from this document.

mysql client session

use mail;

 select username from mailbox;

 select domain from domain;

 

Create the system user account for managing virtual mail

[ref: http://www.postfix.org/postconf.5.html#virtual_uid_maps, http://www.postfix.org/postconf.5.html#virtual_gid_maps]

If you've skipped the hash virtual user instructions, then you will need to create the System user Account for Postfix to use for delivering 'virtual' mail.

Mail delivery happens with the recipient's UID/GID privileges specified with virtual_uid_maps and virtual_gid_maps, therefore the virtual mailbox files must be owned by a system user account and associated with a  group on your system. Fortunately Postfix is flexible to allow each mailbox to be owned by a unique system user account or by a single system user account for all domains, and even one system user account per domain. This is set by using the virtual_uid_maps and virtual_gid_maps setting.

virtual_uid_maps = static:901
virtual_gid_maps = static:901

The 'static' map type tells Postfix that you want the uid/gid to be for all accounts.

We can now create the system user account to manage virtual email mailboxes.

# useradd -d /var/spool/postfix/vmail -g=uid -u 901 -s /sbin/nologin -m -c "Virtual Mailbox Owner" _vmail
# chmod -R 770 /var/spool/postfix/vmail

A by-product of the user/group creation is that the "base" directory will also be created with the correct permissions.

If we wanted to use different users, groups for managing mailboxes, then we could have used a lookup file instead.

virtual_uid_maps = hash:/etc/postfix/virtual_uids
virtual_gid_maps = hash:/etc/postfix/virtual_gids

Ensure the standard (non-virtual) alias file is built by using Postfix's newaliases.

# /usr/local/sbin/newaliases

Restart Postfix

# /usr/local/sbin/postfix stop

# /usr/local/sbin/postfix start

 

Configuring Postfix

Postfix can read it's configuration data from hash files, text files, and from databases. To tell Postfix that data will be obtained from a MySQL database, we use the "mysql:" prefix to a text file that contains the relevant information for postfix to extract that data.

For our example, modify the above /etc/postfix/main.cf. We can work by just removing the additions made above and replacing them with the following.

File Fragment: /etc/postfix/main.cf

virtual_alias_maps = mysql:/etc/postfix/mysql/alias_maps.cf
virtual_gid_maps = static:901
virtual_mailbox_base = /var/spool/postfix/vmail
virtual_mailbox_domains = mysql:/etc/postfix/mysql/domains_maps.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql/mailbox_maps.cf
virtual_minimum_uid = 900
virtual_transport = virtual
virtual_uid_maps = static:901
parent_domain_matches_subdomains =

The /etc/postfix/mysql/*.cf files contain the login information for postfix to access and retrieve the MySQL database/table.

Verify that what we've set above is actually what postfix will recognise.

Screen Session

/usr/local/sbin/postfix reload

/usr/local/sbin/postconf -n | grep ^virtual

virtual_alias_maps = mysql:/etc/postfix/mysql/alias_maps.cf
virtual_gid_maps = static:901
virtual_mailbox_base = /var/spool/postfix/vmail
virtual_mailbox_domains = mysql:/etc/postfix/mysql/domains_maps.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql/mailbox_maps.cf
virtual_minimum_uid = 900
virtual_transport = virtual
virtual_uid_maps = static:901

Key things to watch out for is that we are using the file type: "mysql" and that the file locations specified above will be correct to what we are creating below.

Creating the postfix to mysql settings file

Before we create our text *.cf files, we'll need to make the directory.

# mkdir -p /etc/postfix/mysql

Create the current mysql instruction/configuration files for postfix.

Virtual Domains

virtual_mailbox_domains = mysql:/etc/postfix/mysql/domains_maps.cf

domains_maps.cf will be used for virtual_mailbox_domains

File:/etc/postfix/mysql/domains_maps.cf

user = postfixserver
password = postfixserver
hosts = 127.0.0.1
dbname = mail
table = domain
select_field = domain
where_field = domain
additional_conditions = and backupmx = '0' and active = '1'

Virtual Mailbox

virtual_mailbox_maps = mysql:/etc/postfix/mysql/mailbox_maps.cf

mailbox_maps.cf will be used for virtual_mailbox_maps

File:/etc/postfix/mysql/mailbox_maps.cf

user = postfixserver
password = postfixserver
hosts = 127.0.0.1
dbname = mail
table = mailbox
select_field = maildir
where_field = username
additional_conditions = and active = '1'

Virtual Aliases

virtual_alias_maps = mysql:/etc/postfix/mysql/alias_maps.cf

alias_maps.cf will be used for virtual_alias_maps

File: /etc/postfix/mysql/alias_maps.cf

user = postfixserver
password = postfixserver
hosts = 127.0.0.1
dbname = mail
table = alias
select_field = goto
where_field = address

Restart Postfix

Once you've created all these mysql files, we can stop and restart postfix and should be working together with postfixadmin for managing virtual user accounts.

# /usr/local/sbin/postfix stop

# /usr/local/sbin/postfix start

Testing

We can now provide some sample testing of mail routing through to our virtual accounts, using MySQL as the database.

telnet localhost smtp

Screen Session

$ telnet localhost smtp

Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 myhost.example.org ESMTP Postfix (2.3.2)

ehlo example.org

250-myhost.example.org
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

mail from: <samt@example.org>

250 2.1.0 Ok

rcpt to: <charlie@alpha.example.org>

250 2.1.5 Ok

rcpt to: <chou@beta.example.org>

250 2.1.5 Ok

rcpt to: <cinder@gamma.example.org>

250 2.1.5 Ok

data

354 End data with <CR><LF>.<CR><LF>

Subject: Welcome MySQL based virtual users

Hopefully you've received this email message without fault ?


.
250 2.0.0 Ok: queued as 357E65A950
quit
221 2.0.0 Bye
Connection closed by foreign host.

Mail Log

With the following results showing in our log file: /var/log/maillog

File: /var/log/maillog

connect from unknown[::1]
: client=unknown[::1]
: message-id=<20070209010215.45BCC5A956@myhost.example.org>
: from=<samt@example.org>, size=402, nrcpt=3 (queue active)
: disconnect from unknown[::1]
: to=<charlie@alpha.example.org>, relay=virtual, delay=54, delays=52/0.01/0/1.6, dsn=2.0.0, status=sent (delivered to maildir)
: to=<chou@beta.example.org>, relay=virtual, delay=54, delays=52/0.03/0/1.7, dsn=2.0.0, status=sent (delivered to maildir)
: to=<cinder@gamma.example.org>, relay=virtual, delay=54, delays=52/0.04/0/1.8, dsn=2.0.0, status=sent (delivered to maildir)
: removed

Mail Store

We should now have email in the user directories ./alpha.example.org/charlie@alpha.example.org/new as well as ./beta.example.org/chou@beta.example.org/new and ./gamma.example.org/cinder@gamma.example.org/new with the same content as below:

File: /var/spool/postfix/vmail/alpha.example.org/charlie@alpha.example.org

59984.myhost.example.org <
Return-Path: <samt@example.org>
X-Original-To: charlie@alpha.example.org
Delivered-To: charlie@alpha.example.org
Received: from example.org (unknown [IPv6:::1])
by myhost.example.org (Postfix) with ESMTP id 45BCC5A956;
Fri, 9 Feb 2007 14:02:08 +1300 (TOT)
Subject: Welcome MySQL based virtual users
Message-Id: <20070209010215.45BCC5A956@myhost.example.org>
Date: Fri, 9 Feb 2007 14:02:08 +1300 (TOT)
From: samt@example.org
To: undisclosed-recipients:;

 

Hopefully you've received this email message without fault ?

MySQL Log file

If you're having problems then you can also take a look at the sql log files at /var/mysql/myhost.log

File: /var/mysql/myhost.log

20 Connect postfixserver@localhost on mail
20 Query SELECT goto FROM alias WHERE address='example.org'
21 Connect postfixserver@localhost on mail
21 Query SELECT domain FROM domain WHERE domain='example.org'
070209 13:50:37 20 Query SELECT goto FROM alias WHERE address='alpha.example.org'
21 Query SELECT domain FROM domain WHERE domain='alpha.example.org'
22 Connect postfixserver@localhost on mail
22 Query SELECT goto FROM alias WHERE address='charlie@alpha.example.org'
22 Query SELECT goto FROM alias WHERE address='@alpha.example.org'
23 Connect postfixserver@localhost on mail
23 Query SELECT maildir FROM mailbox WHERE username='charlie@alpha.example.org' and active = '1'
24 Connect postfixserver@localhost on mail
24 Query SELECT goto FROM alias WHERE address='charlie@alpha.example.org'
24 Query SELECT goto FROM alias WHERE address='@alpha.example.org'
070209 13:50:44 20 Query SELECT goto FROM alias WHERE address='beta.example.org'
21 Query SELECT domain FROM domain WHERE domain='beta.example.org'
22 Query SELECT goto FROM alias WHERE address='chou@beta.example.org'
22 Query SELECT goto FROM alias WHERE address='@beta.example.org'
23 Query SELECT maildir FROM mailbox WHERE username='chou@beta.example.org' and active = '1'
24 Query SELECT goto FROM alias WHERE address='chou@beta.example.org'
24 Query SELECT goto FROM alias WHERE address='@beta.example.org'
070209 13:50:55 20 Query SELECT goto FROM alias WHERE address='gamma.example.org'
21 Query SELECT domain FROM domain WHERE domain='gamma.example.org'
22 Query SELECT goto FROM alias WHERE address='cinder@gamma.example.org'
22 Query SELECT goto FROM alias WHERE address='@gamma.example.org'
23 Query SELECT maildir FROM mailbox WHERE username='cinder@gamma.example.org' and active = '1'
24 Query SELECT goto FROM alias WHERE address='cinder@gamma.example.org'
24 Query SELECT goto FROM alias WHERE address='@gamma.example.org'
070209 13:51:24 20 Query SELECT goto FROM alias WHERE address='alpha.example.org'
21 Query SELECT domain FROM domain WHERE domain='alpha.example.org'
20 Query SELECT goto FROM alias WHERE address='beta.example.org'
21 Query SELECT domain FROM domain WHERE domain='beta.example.org'
20 Query SELECT goto FROM alias WHERE address='gamma.example.org'
21 Query SELECT domain FROM domain WHERE domain='gamma.example.org'
25 Connect postfixserver@localhost on mail
25 Query SELECT maildir FROM mailbox WHERE username='charlie@alpha.example.org' and active = '1'
26 Connect postfixserver@localhost on mail
26 Query SELECT maildir FROM mailbox WHERE username='chou@beta.example.org' and active = '1'
27 Connect postfixserver@localhost on mail
27 Query SELECT maildir FROM mailbox WHERE username='cinder@gamma.example.org' and active = '1'

So, if some of the above are not working properly then you at least can get some clues from the above two log files of where you can begin debugging your installation.

Remember that postconf returns what Postfix actually understands from your changes to the ./postfix/main.cf file so it is always a good point to start here to ensure that what you thought you typed in, is actually what postfix is reading.

 

The next logical step in configuring your email server with Postfix, is to set up an imap/pop3 server. For this exercise, we've reviewed instructions to use dovecot.

 

Configuring a Virtual Email Service - MySQL high load server

Below are just collections from other people's notes, since I haven't got a 'high load' server for testing as yet (otherwise known as machines and ram are dealing well currently.)

You can improve performance in high load environments by sharing database/mysql connections among Postfix smtpd connections.

virtual_alias_maps = proxy:mysql:/etc/postfix/mysql/alias_maps.cf
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql/domains_maps.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql/mailbox_maps.cf

 

 

Reference Resources

There is a plethora of documentation out there using Postfix with Virtual Accounts, likewise there is also quite a few with OpenBSD as the server operating system.

Postfix Documentation http://www.postfix.org/documentation.html, http://www.postfix.org/docs.html
Postfix - Hosting Multiple Domains with Virtual Accounts - http://www.akadia.com/services/postfix_separate_mailboxes.html
OpenBSD Postfix Admin Guide - http://postfix.wiki.xs4all.nl/index.php?title=OpenBSD_PostfixAdmin_Guide
Virtual Users and Domains with Courier IMAP and MySQL - http://postfix.wiki.xs4all.nl/index.php?title=Virtual_Users_and_Domains_with_Courier-IMAP_and_MySQL
SASL README http://www.postfix.org/SASL_README.html
Server with virtual multi-domain support How-To setup a server to use Apache2, Postfix, Pure-FTPd, Dovecot, Roundcube and all of them controlled easy over web by CCC http://postfix.pentachron.net/ 
The Book of Postfix - Chapter 25 Troubleshooting Tips. http://www.postfix-book.com/debugging.html
ISP-style Email Service with Debian-Sarge and Postfix 2.1 http://workaround.org/articles/ispmail-sarge/ 
Virtual Users and Domains with Postfix, Courier and MySQL (+SMTP_AUTH, Quota, SpamAssassin, ClamAV) http://www.howtoforge.com/virtual_postfix_mysql_quota_courier

 

Author and Copyright

Copyright (c) 2006 Samiuela LV Taufa. All Rights Reserved.

I reserve the right to be totally incorrect even at the best advice of betters. In other words, I'm probably wrong in enough places for you to call me an idiot, but don't 'cause you'll hurt my sensibilities, just tell me where I went wrong and I'll try again.

You are permitted and encouraged to use this guide for fun or for profit as you see fit. If you republish this work in what-ever form, it would be nice (though not enforceable) to be credited.

 Serving Up Virtual User Account Mail - OpenBSD

Copyright  © 2000/1/2 NoMoa.COM All rights reserved.