I have 3 interfaces on my main server, first is used for the local network, second and third are internet connections (each has its own ip routing table):
eth0 192.168.0.1
eth1 9.9.9.9
eth2 7.7.7.7
There is mail server on host 192.168.0.2 of local net. Iptables translates packets to 25 port of hosts 9.9.9.9 and 7.7.7.7 to 192.168.0.2:25 via DNAT rules.
Also there is specific routing table T1 in my main server, which is used by mail server via
ip rule add from 192.168.0.2 lookup T1
But table T1 uses only one default route:
default via 7.7.7.6 dev eth2 src 7.7.7.7
If packet has came from my first provider to interface 9.9.9.9, when 192.168.0.2 sends request back to sender, it uses default gateway of second provider and sender can't establish connection.
What can i do? My eth1 and eth2 are mx records on DNS and they must both be working.
That's kind of the expected behavior.
SMTP and routing paths are two different layers on the 7 layer networking cake. The closest you can come to managing what routes your mail takes is to build a secondary default route for specific networks you always want to go through a particular NIC for just that particular port.
For example, you can build an iptables rule that only affects port 25 traffic; if the destination address is something, route it to default gateway 2. The rest (0.0.0.0) will always default to the primary default gateway.