Getting incoming mail to work when port forwarding from a router

I’ve moved a working test environment from DigitalOcean to a home Linux box (Ubuntu) but I am struggling to get incoming mail to work. I am port forwarding port 25 on my router and I can see mail arriving by looking at the mail logs (I can see the To: and the From: in the logs). The failure seems to be when the mail receiver tries to POST the e-mail to discourse and I get a time out. I suspect this is because my ISP doesn’t allow loopback through the router, so if the public domain name of the discourse instance is used in the POST command it’ll hang as there is simply no response as the router drops the packets.

This has also meant that I couldn’t view the discourse website on the machine on which it is running… until I realised I could put a line in the /etc/hosts file that pointed the public domain name to the internal network IP address and since then I can view the discourse instance in Firefox.

I presume there might be some magic I can do so the mail-receiver container knows to do something similar. From my reading around it doesn’t use the /etc/hosts file and that might be the source of the problem. I’ve tried adding lines to the mail-receiver.yml file to allow for extra_hosts: but I can’t seem to get it to register. If I enter the mail-receiver app and try to add another line to the /etc/hosts (with cat >>) although I see the line appear the same problem is there.

This is the line from the logs where it shows the POST failure (note: domain is made up for this example):

<23>Feb 23 01:00:31 receive-mail[133]: Recipient:<19>Feb 23 01:01:31 receive-mail[133]: Failed to POST the e-mail to execution expired (Net::OpenTimeout)<19>Feb 23 01:01:31 receive-mail[133]: /usr/lib/ruby/2.7.0/net/http.rb:960:in `initialize’

Any thoughts?

Having had another go and realising I’d put the IP address/hostname the wrong way around in the /etc/hosts file I think I’ve got it working. However, if the mail-receiver container is restarted it loses the extra line I appended to the container’s /etc/hosts file. Is there a way to get the mail-receiver.yml file to add the extra line automatically?

You can look at some other templates that modify files. The web templates do that, for example.