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:
- destination port is 25 and destination address is in inet_interfaces or proxy_interfaces
- 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.