Configure postfix to relay to gmail

Nov 4, 2024 | 1 min read

install postfix and other packages.

apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

configure postfix

tee /etc/postfix/main.cf
nano /etc/postfix/main.cf
  • in the main.cfg put the following
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = 
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_use_tls = yes

Edit or Create authentication credentials

  • nano /etc/postfix/sasl_passwd

change file permission and generate hash

chmod 600 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd
systemctl restart postfix.service

try sending mail


echo "This is the body of an encrypted email" | mail -s "This is the subject line" test@test.com

change the email of From

  • add the following line end of the /etc/postfix/main.cf
smtp_generic_maps = hash:/etc/postfix/generic
  • create the list to be re-written in /etc/postfix/generic
root@localdomain myaccount@myoffice356domain.com
@localdomain myaccount@myoffice356domain.com
root@myserver  myaccount@myoffice356domain.com
userA@myserver myaccount@myoffice356domain.com

Generate hash and restart postfix

postmap /etc/postfix/generic
systemctl restart postfix