redirect mail traffic through iptables

I’m not a big fan of this method, I prefer to run postfix to process, then forward the mail.


echo "1" > /proc/sys/net/ipv4/ip_forward
# or sysctl net.ipv4.ip_forward=1
iptables -t nat -A OUTPUT -p tcp --dport 25 -j DNAT --to-destination [ip of relay]:[port]

for https here is an example

sudo sysctl net.ipv4.ip_forward=1
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination [destination_ip]:443
sudo iptables -t nat -A POSTROUTING -j MASQUERADE