Handling mail for multiple virtual domains with postfix
Posted by Steve on Wed 14 Sep 2005 at 22:31
Handling email for multiple virtual domains is a common server task. Here we'll show how easy that is to manage using the postfix mailserver using just a couple of small configuration changes.
Note: The Debian mail server packages all conflict with each other meaning that it is impossible to have more than one installed at the same time. If you are already using postfix this won't be a problem.
However if you are running a different mailserver, such as exim, be warned that installing postfix will remove it. (Your configuration files should be kept safely in place, but taking a backup in advance is worth doing anyway).
To install postfix run, as root:
apt-get install postfix
Once installed you'll be presented with a simple debconf setup wizard. Proceed by answering the questions as follows:
- General Type of Configuration?
- Internet Site
- Where should mail for root go?
- [Default]
- Mail name?
- Your mailserver's primary hostname. (Fully qualified)
- Other destinations to accept mail for?
- localhost
Now we can move on to the real configuration. What we need to do is two things:
- Tell postfix which domains we will accept mail for.
- (i.e. All the virtual domains we wish to handle).
- Setup the mapping between email addresses and local accounts.
(It is not required that users have local accounts, Postfix offers several alternative virtual hosting approaches - but this is what I use. It is also equivalent to the previous guide on virtual mail handling with exim4)
To tell postfix which domains we will use we will create a text file to list them in, in the directory /etc/postfix/virtual.
(Create that directory by running "mkdir /etc/postfix/virtual").
List all the domains you wish to handle mail for, in the file /etc/postfix/virtual/domains. For example:
example.com example.net example.org
Then to setup the mappings create the file /etc/postfix/virtual/addresses:
example.com DOMAIN bob@example.com bob steve@example.com steve example.net DOMAIN @example.net steve example.org DOMAIN @example.org chris
This file contains the mappings of email addresses to local accounts. Here we've setup four mappings:
- Mail sent to bob@example.com goes to the local user bob.
- Mail sent to steve@example.com goes to the local user steve.
- Mail sent to anything@example.net goes to the local user steve
- Mail sent to anything@example.org goes to the local user chris
(Note that mail sent to other addresses @example.com will be bounced with "no such user".)
To make these settings live we need to do two things:
- Update postfix's main.cf to read these settings.
- Create a hash of the /etc/postfix/virtual/addresses file.
In the file /etc/postfix/main.cf add:
mydestination = $myhostname, /etc/postfix/virtual/domains virtual_maps = hash:/etc/postfix/virtual/addresses
Then run:
postmap /etc/postfix/virtual/addresses
That should be all you need to do. Reload postfix by running:
/etc/init.d/postfix reload
You can test the setup by attempting to send mail to the server, and watching /var/log/mail.info to see what postfix does with the mail.
Very good (and deeper HOWTO) about postfix, virtual domains and even more is here: http://workaround.org/articles/ispmail-sarge/.
Best Regards,
Luke
[ Parent | Reply to this comment ]
We run similar setup (but with slightly more complicated mysql tables) for few years now without any problems. It's set up using this HOWTO - http://www.marlow.dk/site.php/tech/postfix
We have added spam&virus scanning using excellent tool sagator - http://www.salstar.sk/sagator/ and autoreply using yaa - http://frost.ath.cx/software/yaa/ .
Users can add email accounts and for each manage aliases and forwarding (+catch-all)...
[ Parent | Reply to this comment ]
http://wiki.dovecot.org/moin.cgi/DovecotPostgresql
Postfix can get pretty messy when you deliver to virtual users, rather than delivering email to virtual domains to real users.
I'm hoping the new dovecot mail delivery agent will save me specifying the same settings to two different programs. Then all Postfix will need is a list of valid domains, and a list of valid email accounts at that domain, and Postfix will become "merely" the SMTP transport mechanism.
Oh always test your MTA (especially Postfix and Qmail) for rejecting email to unknown recipients at a virtual domain, rather than sending DSN's for those accounts, very easy to mess up on that one and become a source of backscatter.
[ Parent | Reply to this comment ]
What do you mean? It works fine for me.
[ Parent | Reply to this comment ]
The implementation of the Postfix/Courier-imap/Courier-pop3/MySQL solution can be found at
Virtual Users and Domains with Courier-IMAP and MySQL. The Web based front-end can be found at Postfix Admin.
For antispam and antivirus I'm using MailScanner with spamassassin and clamav.
Fred from Linox.BE
[ Parent | Reply to this comment ]
Steve, there seems to be a minor problem with your set up.
warning: do not list domain example.com in BOTH mydestination and virtual_alias_domains
Any ideas?
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
Strange.
The warning there is the same thing you see in the documentation, but in the example (and my live setup) I have:
myhostname = mail.foo.com mydestination = $myhostname, /etc/postfix/virtual/domains
The only way I can see this being a problem is if you list foo.com inside the virtual/domains file - if you do that remove "$myhostname" from the mydestination setting.
Failing that feel free to post your main.cf file to see if there's anything different you've got there.
Steve
--
[ Parent | Reply to this comment ]
I did list it in virtual/addresses on top of the virtual/domains because I have some virtual users in there (normally listed in /etc/aliases), but it seems to be choking on that...
I'll continue the investigations and get back to you...
[ Parent | Reply to this comment ]
postconf -n alias_database = hash:/etc/aliases alias_maps = hash:/etc/aliases append_dot_mydomain = no biff = no config_directory = /etc/postfix inet_interfaces = all mailbox_command = /usr/bin/procmail mailbox_size_limit = 0 mydestination = $myhostname, smtp.$mydomain, mail.$mydomain, /etc/postfix/virtual/domains, localhost, localhost.localdomain mydomain = hoeg.com myhostname = remote.$mydomain mynetworks = 127.0.0.0/8 myorigin = /etc/mailname recipient_delimiter = + relayhost = smtpd_banner = $myhostname NO UCE ESMTP smtpd_delay_reject = no smtpd_helo_required = yes smtpd_recipient_restrictions = permit_mynetworks reject_unauth_destination check_policy_service inet:127.0.0.1:60000 strict_rfc821_envelopes = yes
/etc/postfix/virtual/domains hoeg.com hoeg.org example.com
/etc/postfix/virtual/addresses peter@hoeg.com peter hoeg.org DOMAIN peter@hoeg.org peter example.com DOMAIN abc@example.com peterNo more errors and it works!
[ Parent | Reply to this comment ]
************************************************ /etc/postfix/main.cf mydestination = $myhostname, /etc/postfix/virtual/domains /etc/postfix/virtual/domains example.com example.net example.org /etc/postfix/virtual/addresses: example.com DOMAIN bob@example.com bob steve@example.com steve example.net DOMAIN @example.net steve example.org DOMAIN @example.org chris ************************************************Quote from http://www.postfix.org/VIRTUAL_README.html:
NEVER list a virtual alias domain name as a mydestination domain! NEVER list a virtual MAILBOX domain name as a mydestination domain! NEVER list a virtual MAILBOX domain name as a virtual ALIAS domain!
- So why do you list /etc/postfix/virtual/domains in mydestination?
- What does the line "example.com DOMAIN" mean? isn't THIS for relay virtualdomains not mydestination?
Is "DOMAIN" just a comment? - What is the difference of these two systems?
a) virtual_maps (virtual_alias_maps, virtual_alias_domains)
b) virtual_mailbox_maps,virtual_mailbox_domains
Somewhat confusing!
(http://www.postfix.org/ADDRESS_CLASS_README.html)
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
It's a feature. Older Postfix versions tended to match subdomains in places like main.cf. New versions will require fully qualified domain names.
/etc/postfix/main.cf:mydomains = example.net
virtual_alias_domains = obscure.example.net
To suppress the warning, define the variable
/etc/postfix/main.cf:parent_domain_matches_subdomains =
He's warning you that you might be depending on the old behavior, and you ought to revisit your main.cf and map files.
If the definition of parent_domain_matches_subdomains in postconf(5) does not make sense to you, try changing the first word from "What" to "List of".
- cls
[ Parent | Reply to this comment ]
Default the postfix configuration for items :
virtual_alias_maps = $virtual_maps
virtual_alias_domains = $virtual_alias_maps
whereas we have set :
virtual_maps = hash:/etc/postfix/virtual/addresses
mydestination = $myhostname, /etc/postfix/virtual/domains
So, it's redundant configuration. To handle it, I try to add with this configuration :
virtual_alias_domains = no
Alhamdulillah, Postfix work without warning.
[ Parent | Reply to this comment ]
emm ... corection :
virtual_alias_domains = yes
[ Parent | Reply to this comment ]
I have user John with mail john.smith@company.com. I want to configure MSOutlook on John's workstation to receive mails from local mail server(mailserver.company.local). How to do this using postfix?
[ Parent | Reply to this comment ]
Then repeat 'sudo postmap...' statement and restart postfix (or reload its configuration)
That should do.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
I found the following to be easier than the method suggested here, and since I found this resource a great starting point, I'd like to mention an easier way here.
Add the following to
/etc/postfix/main.cf:virtual_alias_maps = hash:/etc/postfix/virtualCreate
/etc/postfix/virtualand add something like:first.example.com anything
postmaster@first.example.com admin@example.com
webmaster@first.example.com webmaster@www.example.com
second.example.com anything
postmaster@second.example.com admin@somewhereelse.example.com
blast@second.example.com one@example.com, two@example.com, three@example.comDo the database file dance with:
postmap /etc/postfix/virtualinvoke-rc.d postfix reload
Step one is different because it uses virtual_alias_domains instead of the virtual_maps suggested here. It makes the most sense for my application and was learned from "Mail forwarding domains" in the Postfix Virtual Domain Hosting Howto.
Step two can be tricky because the domain.example.com anything lines take the place the place of /etc/postfix/virtual/domain suggested here--letting postfix know what domains destinations it will accept. See the Virtual Alias Domains section of the virtual manpage for a more thorough explanation.
Beauty here is all that is needed is in one file.
Of course, please correct me if I'm wrong! And thanks for this site, and go Debian!
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
After: postfix>?>dovecot>
where will be email storage between postfix and dovecot(no sql db)? and after them?
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]