Posted on Leave a comment

Configure Postfix to Send Mail Using an External SMTP Server

Sometimes due to network configuration you cannot use your SMTP server and you want to send emails through a proxy/relay. Below you can find how to configure a linux machine so that to act as an SMTP relay and forward emails to one SMTP server.

First if not installed you should install postfix package for your linux server depending on the distribution. For example on a RedHat server you should do:

sudo yum install postfix

Then you should edit the below postfix configuration lines. Postfix configuration is handled by /etc/postfix/main.cf

  • Change inet_interfaces from localhsot to all
inet_interfaces = all

Configure the source from which the emails will be sent. For example you can use a specific host or a subnet of hosts. In my case I chose a subnet so the second line should be uncommented.

#mynetworks_style = class
mynetworks_style = subnet
#mynetworks_style = host
  • Configure your subnet source Address and mask.
You can also specify the absolute pathname of a pattern file instead
of listing the patterns here. Specify type:table for table-based lookups
(the value on the table right-hand side is not used).

mynetworks = 192.168.0.0/16, 127.0.0.0/8
#mynetworks = $config_directory/mynetworks
#mynetworks = hash:/etc/postfix/network_table
  • Add your SMTP server address in the latest line
#relayhost = $mydomain
#relayhost = [gateway.my.domain]
#relayhost = [mailserver.isp.tld]
#relayhost = uucphost
relayhost = [IP_Address]

Finally test your configuration by a powershell command to verify the functionality.

Send-MailMessage -From "sender@mail.com" -To "recipient@mail.com" -Subject "test email" -Body "test subject" -SMTPServer IP_Address