Skip to main content

Postfix (SMTP Relay)

Setting up Postfix as a SMTP relay

[!NOTE] This setup is based on tranquil IT's tutorial.

  1. Install and enable postfix and it's dependancies :

    apt install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
    systemctl enable postfix
    
  2. Edit the nano /etc/postfix/main.cf file and add/change the following lines :

    relayhost = [smtp.gmail.com]:587
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_sasl_security_options = noanonymous
    smtp_tls_CAfile = /etc/postfix/cacert.pem
    smtp_use_tls = yes
    
  3. Create an application password from your gmail account (2FA needs to be enabled).

  4. Create the /etc/postfix/sasl_passwd file and add your authentication information :

    [smtp.gmail.com]:587 USERNAME@gmail.com:MDP_Application
    
  5. Fix your file permissions and map the file for postfix :

    chmod 400 /etc/postfix/sasl_passwd
    postmap /etc/postfix/sasl_passwd
    
  6. Create a certificate for postfix

    cd /etc/ssl/certs
    openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key-for-smtp-gmail.pem -out cert-for-smtp-gmail.pem
    cat /etc/ssl/certs/cert-for-smtp-gmail.pem | sudo tee -a /etc/postfix/cacert.pem
    
  7. Restart the postfix service

    /etc/init.d/postfix reload
    
  8. Test and you're done :

    echo "Test mail from postfix" | mail -s "Test Postfix" adresse@mail.com
    

[!IMPORTANT] To make the SMTP server available to your homelab services, you will need to make sure that the /etc/postfix/main.cf file contains the following entries :

mynetworks = 127.0.0.0/8,<your services subnet>
inet_interfaces = all