Back

Setting Up Postfix to use Gmail

2021-04-04

Table of Contents

    A quick list of commands to use gmail as a relay for postfix.

    First step is to install postfix and mailx.

    bash
    # yum install postfix mailx cyrus-sasl cyrus-sasl-plain

    Next create /etc/postfix/sasl_passwd and add the following:

    [smtp.gmail.com]:587    username@gmail.com:password

    Now edit /etc/postfix/main.cf and add the following:

    relayhost = [smtp.gmail.com]:587
    smtp_use_tls = yes
    smtp_sasl_auth_enable = yes
    smtp_sasl_security_options =
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt

    Use postmap to hash the password so that postfix can use it.

    bash
    # postmap /etc/postfix/sasl_passwd

    Now enable postfix to start on boot and restart it.

    bash
    # systemctl enable postfix
    # systemctl restart postfix

    We should now be able to send mail through gmail and we can test this through the following command.

    bash
    # echo "Hello, World!" | mail -s "My First E-mail" recipient@domain.com

    Voila! Now postfix is using gmail to send mail.