Postfix – How to Relay Mail to Different Port on Localhost

postfix

Currently I have one Postfix server listening on port 2525 for outgoing mail and another one for incoming mail on port 25 running behind an SSH tunnel on the same machine. I'd like all mail going into the server at 2525 (mainly "mail undeliverable" messages) to be relayed to port 25, because only the server at 25 has all the mailboxes. I tried setting local_transport to smtp:127.0.0.1:25 or doing the equivalent thing in transport_maps as others on the Internet have suggested, but all I get is (mail for 127.0.0.1 loops back to myself) in the logs of the server at 2525.

I tried searching all over the Internet for an answer but I couldn't find any. How can I get rid of this error message?

Best Answer

So I actually downloaded the source code of the latest release of Postfix 3.7.x and went looking through it for my answer. Quickly I found out that for the "mail for X loops back to myself" error message to appear, at least one of the following two conditions must be met:

  1. destination port is 25 and destination address is in inet_interfaces or proxy_interfaces
  2. destination port is 25 and destination hostname sent in EHLO response is Postfix's own hostname and destination supports ESMTP

I'm too lazy to patch the source code myself, so the solution I chose was to add the following lines in the config of the server at 2525:

mydestination = mydomain.com, localhost
local_transport = relay:127.0.0.1:25
inet_interfaces =

and in the server at 25:

myhostname = mail.mydomain.com
mydestination = mydomain.com, localhost

And it works like a charm now. I also added an MX record for mail.mydomain.com pointing to mydomain.com just in case.

Edit (2022-03-27): So my setup broke after two weeks and Postfix started giving me the following error: fatal: /etc/postfix/master.cf: line X: no valid IP address found: 2525. I applied the following fix to the 2525 server's config and it works again.

local_transport = relay:mydomain.com:25
inet_interfaces = 127.0.0.1

Edit (2022-04-02): It broke again and I decided to just build Postfix from source and patch what I needed. I followed the guide at https://wiki.debian.org/BuildingTutorial (you will have to follow the appropriate tutorial for your own distribution if you're not using Debian) and changed the value of the preprocessor macro SMTP_MISC_FLAG_LOOP_DETECT in src/smtp/smtp.h to 0. I also changed all my Postfix servers' configs back to their original, sane values.

Related Topic