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.

 

 


Posted by shufla (83.30.xx.xx) on Wed 14 Sep 2005 at 23:09
Hello,

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 ]

Posted by Anonymous (82.119.xx.xx) on Wed 14 Sep 2005 at 23:52
It seems quite good, but for example with this setup, you can't have forward-only email addresses (because of that default email2email).
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 ]

Posted by simonw (84.45.xx.xx) on Thu 15 Sep 2005 at 17:43
[ View Weblogs ]
And for those of us who prefer postgres, and are forever mixing up postgres and postfix because the names are too similar....

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 ]

Posted by Anonymous (213.164.xx.xx) on Fri 16 Sep 2005 at 09:40
> Postfix can get pretty messy when you deliver to virtual users

What do you mean? It works fine for me.

[ Parent | Reply to this comment ]

Posted by Anonymous (84.194.xx.xx) on Thu 15 Sep 2005 at 08:17
I realy prefer using a MySQL solution with a webbased front-end. All the e-mail boxes are stored in a MySQL database.

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 ]

Posted by peterhoeg (193.163.xx.xx) on Thu 15 Sep 2005 at 09:21

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 ]

Posted by Steve (82.41.xx.xx) on Thu 15 Sep 2005 at 09:25
[ 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 ]

Posted by peterhoeg (193.163.xx.xx) on Thu 15 Sep 2005 at 09:47
No, because virtual/domains does not have anything to do with that.

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 ]

Posted by peterhoeg (193.163.xx.xx) on Thu 15 Sep 2005 at 10:01
Here we go:
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  peter
No more errors and it works!

[ Parent | Reply to this comment ]

Posted by Anonymous (80.218.xx.xx) on Thu 13 Oct 2005 at 18:57
Hi, isn't this setup a Problem?
************************************************
/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!
  1. So why do you list /etc/postfix/virtual/domains in mydestination?
  2. What does the line "example.com DOMAIN" mean? isn't THIS for relay virtualdomains not mydestination?
    Is "DOMAIN" just a comment?
  3. 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 ]

Posted by Anonymous (212.113.xx.xx) on Tue 27 Dec 2005 at 16:31
is possible to create an email like sales@ and have multiple recepients for that email?

[ Parent | Reply to this comment ]

Posted by Anonymous (128.131.xx.xx) on Wed 18 Jan 2006 at 14:29
i'd say that a trouble ticket system or (if that is too much) a simple mailing list should do that...

[ Parent | Reply to this comment ]

Posted by clsgis (66.159.xx.xx) on Fri 6 Oct 2006 at 18:38

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 ]

Posted by Anonymous (202.162.xx.xx) on Sat 19 May 2007 at 05:58
By Aris Nurbawani :

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 ]

Posted by Anonymous (202.162.xx.xx) on Sat 27 Oct 2007 at 05:00
Aris Nurbawani :
emm ... corection :

virtual_alias_domains = yes

[ Parent | Reply to this comment ]

Posted by Ivica (91.150.xx.xx) on Thu 13 Dec 2007 at 12:33
I want to configure postfix to receive mails for 20+ users from my ISP's public mail server and to deliver it to my local domain(company.local). Example:
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 ]

Posted by Anonymous (62.195.xx.xx) on Thu 3 Feb 2011 at 09:39
You could comment-out (by prepending '#') the lines in '/etc/postfix/virtual/domains' containing 'DOMAIN'.

Then repeat 'sudo postmap...' statement and restart postfix (or reload its configuration)

That should do.

[ Parent | Reply to this comment ]

Posted by Anonymous (62.45.xx.xx) on Sat 15 Dec 2007 at 22:02
Hi everyone, I still have a question, Let me explain my situation first, I've already got a working mailserver, (very simple, no virtual users etc. just 1 domain with 5 local users) Now I want to add a domain as explained above The way it's now: the MX-10 record points to mail.example.com And myhostname = mail.example.com And so to to add the second domain I added: mydestination = $myhostname, /etc/postfix/virtual/domains and virtual_maps = hash:/etc/postfix/virtual/addresses But then the second domain, (let's say example2.com) where should this mx-10 record point to and how do I configure myhostname for example2.com Any help much appreciated!

[ Parent | Reply to this comment ]

Posted by Anonymous (208.65.xx.xx) on Wed 9 Jan 2008 at 00:19

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.

  1. Add the following to /etc/postfix/main.cf:

    virtual_alias_maps = hash:/etc/postfix/virtual
  2. Create /etc/postfix/virtual and 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.com
  3. Do the database file dance with: postmap /etc/postfix/virtual

  4. invoke-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 ]

Posted by Anonymous (80.180.xx.xx) on Fri 16 Apr 2010 at 08:22
That last comment is not "wrong"; it just refers to Postfix 2, while the article is about Postfix 1.

[ Parent | Reply to this comment ]

Posted by Anonymous (193.219.xx.xx) on Tue 10 Apr 2012 at 14:59
And how to do the same but with LDAP, not files? plz:) with AD or 389DS? how look tree?

After: postfix>?>dovecot>

where will be email storage between postfix and dovecot(no sql db)? and after them?

[ Parent | Reply to this comment ]

Posted by Anonymous (204.8.xx.xx) on Tue 6 Dec 2011 at 05:54
Excellent!, Thank-you.

[ Parent | Reply to this comment ]

Posted by Anonymous (173.188.xx.xx) on Fri 15 Jun 2012 at 17:19
Hi,
you are saying that...
(Note that mail sent to other addresses @example.com will be bounced with "no such user".)
but I am getting all users emails, NO BOUNCE... even if I do not have have that user mapped or listed there???

[ Parent | Reply to this comment ]

Posted by Anonymous (173.188.xx.xx) on Fri 15 Jun 2012 at 18:23
My main.cf file is like this....

queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
mail_owner = postfix
myhostname = mail.abcd.com
mydomain = abcd.com
myorigin = $myhostname
inet_interfaces = all
mydestination = $myhostname, /etc/postfix/virtuals/domains
virtual_maps = hash:/etc/postfix/virtuals/addresses
unknown_local_recipient_reject_code = 550
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
recipient_delimiter = +
home_mailbox = Maildir/
debug_peer_level = 2
debugger_command =
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
xxgdb $daemon_directory/$process_name $process_id & sleep 5

newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.3.3/samples
readme_directory = /usr/share/doc/postfix-2.3.3/README_FILES

[ Parent | Reply to this comment ]

Sign In

Username:

Password:

[Register|Advanced]

 

Flattr

 

Current Poll

What do you use for configuration management?








( 456 votes ~ 5 comments )

 

 

Related Links