# Two mail servers

**URL:** <https://discourse.haproxy.org/t/two-mail-servers/5003>\
**Category:** Help!\
**Created:** [March 26, 2020, 9:27am UTC](https://discourse.haproxy.org/t/two-mail-servers/5003 "2020-03-26T09:27:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![unixm](https://avatars.discourse-cdn.com/v4/letter/u/ecccb3/32.png) [@unixm](https://discourse.haproxy.org/u/unixm)\
**Post date:** [March 26, 2020, 9:27am UTC](https://discourse.haproxy.org/t/two-mail-servers/5003/1 "2020-03-26T09:27:39Z")

</div>

I did not find how you can proxy mail to two different mail servers. Is it possible to do this?  
Example: [mail1@example.com](mailto:mail1@example.com)  
belongs to server1  
and [mail2@examle.com](mailto:mail2@examle.com) belongs to server2  
server1 192.168.0.10  
server2 192.168.10.20  
server1 postfix  
server2 exchange

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [March 26, 2020, 11:00am UTC](https://discourse.haproxy.org/t/two-mail-servers/5003/2 "2020-03-26T11:00:16Z")

</div>

I just responded in the other thread, no, it’s not possibile with haproxy:

> [@IMAP + SMTP proxy for 2 Mail Domains](https://discourse.haproxy.org/t/imap-smtp-proxy-for-2-mail-domains/4521/3):
>
> You can’t. Haproxy does not know about SMTP or IMAP, and therefor cannot make load-balancing decision based on that. Also, the destination email address is unknown when the connection establishes first (and when the first packet is send) - which is when a load-balancing decision has to take place (unless the proxy itself is a full-blown SMTP/IMAP speaker). So this really requires a software that is actually able to dialog in SMTP/IMAP with the client, which haproxy isn’t.

---

<div class="post-metadata">

**Author:** ![nlindq](https://avatars.discourse-cdn.com/v4/letter/n/2acd7d/32.png) [@nlindq](https://discourse.haproxy.org/u/nlindq)\
**Post date:** [March 31, 2020, 9:50pm UTC](https://discourse.haproxy.org/t/two-mail-servers/5003/3 "2020-03-31T21:50:13Z")

</div>

Haproxy is not the right tool for this job.

You need an SMTP relay (smarthost) capable of determining which host a destination mailbox resides on. If you had two such relays, you could use haproxy to load balance them, I suppose.

---

<div class="post-metadata">

**Author:** ![unixm](https://avatars.discourse-cdn.com/v4/letter/u/ecccb3/32.png) [@unixm](https://discourse.haproxy.org/u/unixm)\
**Post date:** [April 2, 2020, 8:22am UTC](https://discourse.haproxy.org/t/two-mail-servers/5003/4 "2020-04-02T08:22:38Z")

</div>

I tried a lot of options, unfortunately none of them works.

---

<div class="post-metadata">

**Author:** ![nlindq](https://avatars.discourse-cdn.com/v4/letter/n/2acd7d/32.png) [@nlindq](https://discourse.haproxy.org/u/nlindq)\
**Post date:** [April 3, 2020, 2:42pm UTC](https://discourse.haproxy.org/t/two-mail-servers/5003/5 "2020-04-03T14:42:50Z")

</div>

Unless you mean you’ve tried a lot of haproxy options–in which case, it can’t work–you need to be looking at changing the configuration on each of your existing mail servers so each is aware of which mailboxes reside on the other server. Look into Exchange “shared domain” documentation, for example.  
You’ll need similar functionality in Postfix. A shared source of this information (such as A/D or LDAP) will lighten the administrative burden considerably, but would require significant complexity to set up.  
Either way, this is completely out-of-scope for HAProxy and you should be looking for help for your MTAs in the usual places, or engaging a consultant with expertise in hybrid e-mail environments.

---

<div class="post-metadata">

**Author:** ![unixm](https://avatars.discourse-cdn.com/v4/letter/u/ecccb3/32.png) [@unixm](https://discourse.haproxy.org/u/unixm)\
**Post date:** [April 3, 2020, 3:52pm UTC](https://discourse.haproxy.org/t/two-mail-servers/5003/6 "2020-04-03T15:52:57Z")

</div>

Большое спасибо, именно о диспетчере каталогов я и подумал.

Попробую организовать центральное управление пользователями на  
основе OpenLDAP.

Еще раз большое спасибо.

Thank you very much.

---

<div class="post-metadata">

**Author:** ![HaHaHa](https://avatars.discourse-cdn.com/v4/letter/h/87869e/32.png) [@HaHaHa](https://discourse.haproxy.org/u/HaHaHa)\
**Post date:** [December 12, 2022, 1:22pm UTC](https://discourse.haproxy.org/t/two-mail-servers/5003/7 "2022-12-12T13:22:14Z")

</div>

I am in the process of moving and unfortunately the ISP that services the new location will only provide/rent 1 STATIC IP address.

Currently I have a block of 5 IP address are utilized for pet projects.

My pfSense box is connected to a semi-managed smart switch and to that is connected my VM Host with multiple NIC cards installed.

The VM Host is running several VMs each doing specific tasks. VM #1 Web, VM #2 Mail, VM #3 NAS, etc…

Would HAProxy work for the following scenario.

```auto
www.somedomainname1.com <---> pfSense/HA Proxy <---> 192.168.10.2 / NIC 1 / VM 1

www.somedomainname2.com <---> pfSense/HA Proxy <---> 192.168.20.2 / NIC 2 / VM 2

imap(smtp).somedomainname1.com <---> pfSense/HA Proxy <---> 192.168.30.2 / NIC 3 / VM 3
imap(smtp).somedomainname2.com <---> pfSense/HA Proxy <---> 192.168.30.2 / NIC 3 / VM 3

imap(smtp).somedomainname3.com <---> pfSense/HA Proxy <---> 192.168.40.2 / NIC 4 / VM 4

```

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [December 12, 2022, 7:06pm UTC](https://discourse.haproxy.org/t/two-mail-servers/5003/8 "2022-12-12T19:06:44Z")

</div>

No, this is not possible, for the reasons mentioned here:

> [@IMAP + SMTP proxy for 2 Mail Domains](https://discourse.haproxy.org/t/imap-smtp-proxy-for-2-mail-domains/4521/3):
>
> You can’t. Haproxy does not know about SMTP or IMAP, and therefor cannot make load-balancing decision based on that. Also, the destination email address is unknown when the connection establishes first (and when the first packet is send) - which is when a load-balancing decision has to take place (unless the proxy itself is a full-blown SMTP/IMAP speaker). So this really requires a software that is actually able to dialog in SMTP/IMAP with the client, which haproxy isn’t.

---

<div class="post-metadata">

**Author:** ![HaHaHa](https://avatars.discourse-cdn.com/v4/letter/h/87869e/32.png) [@HaHaHa](https://discourse.haproxy.org/u/HaHaHa)\
**Post date:** [December 12, 2022, 7:24pm UTC](https://discourse.haproxy.org/t/two-mail-servers/5003/9 "2022-12-12T19:24:01Z")

</div>

Thanks for the reply.

I want to be clear I am not looking for the level of granularity the OP was looking for of routing based on a users email address but rather the email domain.

For example:

```auto
imap(smtp).somedomainname1.com <---> pfSense/HA Proxy <---> 192.168.30.2 / NIC 3 / VM 3
imap(smtp).somedomainname2.com <---> pfSense/HA Proxy <---> 192.168.30.2 / NIC 3 / VM 3

imap(smtp).somedomainname3.com <---> pfSense/HA Proxy <---> 192.168.40.2 / NIC 4 / VM 4

```

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [December 12, 2022, 8:25pm UTC](https://discourse.haproxy.org/t/two-mail-servers/5003/10 "2022-12-12T20:25:57Z")

</div>

I understand, but neither SMTP nor POP3 nor IMAP indicate the domain name in the first packet, which would be required to content switch it to different backend servers.

Only in the case of implicit SSL can we actually use the hostname from the SSL client\_hello SNI field for load-balancing decisions, which I very much doubt will address all your use-cases.
