Postfix send through SMTP relay

    • Postfix send through SMTP relay

      This allows Postfix to send emails through a third party SMTP relay like Gmail. I know on Debian based system you need libsasl2-modules installed.

      First create a SASL credential file, /etc/postfix/sasl_passwd:

      Source Code

      1. [smtp.gmail.com]:587 [email protected]:$PASS


      Generate a map file:
      postmap /etc/postfix/sasl_passwd
      It'll generate a /etc/postfix/sasl_passwd.db file.


      Let's secure those two files:
      chmod 0600 /etc/postfix/sasl_passwd*


      Add following lines to your /etc/postfix/main.cf:

      Source Code: /etc/postfix/main.cf

      1. relayhost = [smtp.gmail.com]:587
      2. smtp_sasl_auth_enable = yes
      3. smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
      4. smtp_sasl_security_options = noanonymous
      5. #smtp_use_tls = yes
      6. smtp_tls_security_level = may
      7. header_size_limit = 4096000


      Restart Postfix.
      I like cars and taking pictures! 8)
    • Recently I setup a home lab and have a centralized Postfix server running. Since my ISP blocked port 25 and Postfix SMTPs doesn't support SSL auth I had to setup stunnel TLS proxy between the two.

      My home lab setup and e-mail flow: [Multiple computers/servers] => [local centralized Postifx server] => [Private Postfix server on Internet] => [Internet]

      Install stunnel on local Postfix server:
      apt-get install stunnel4


      Enable stunnel by modifying /etc/default/stunnel.

      Source Code

      1. ENABLED=1


      Also enable on startup.
      systemctl enable stunnel4


      Create a custom stunnel configuration file. Filename like /etc/stunnel/SMTPs.conf would do with following content. $EXT_POSTFIX will be FQDN or IP of your external Postfix server.

      Source Code

      1. pid = /var/run/stunnel-SMTPs.pid
      2. [smtp-tls-wrapper]
      3. accept = 22225
      4. client = yes
      5. connect = $EXT_POSTFIX:465


      Modify your /etc/postfix/main.cf file and change relayhost line to point to localhost port 22225.

      Source Code

      1. relayhost = [localhost]:22225


      Restart both Postfix and stunnel.
      systemctl restart postfix.service
      systemctl restart stunnel4.service
      I like cars and taking pictures! 8)