• Recent blogs

    How to send mail through Gmail On CentOS 7

    How to send mail through Gmail On CentOS 7


    Postfix is a flexible mail server that is available on most Linux distribution. Though a full feature mail server, Postfix can also be used as a simple relay host to another mail server, or smart host. This tutorial will describe How to send mail through Gmail On CentOS 7 with Postfix as a relay.


    CentOS 7 or Red Hat Enterprise Linux 7
    Valid Gmail or Google App credentials

    Install Postfix

    Install Postfix, the SASL authentication framework, and mailx are all installed.

    yum -y install postfix cyrus-sasl-plain mailx

    [root@ip-k8smaster ~]# yum -y install postfix cyrus-sasl-plain mailx
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    epel/x86_64/metalink                                                                                         | 9.1 kB  00:00:00
     * base: d36uatko69830t.cloudfront.net
     * epel: mirrors.aliyun.com
     * extras: d36uatko69830t.cloudfront.net
     * updates: d36uatko69830t.cloudfront.net
    base                                                                                                         | 3.6 kB  00:00:00

    Restart Postfix to detect  SASL framework .

    systemctl restart postfix

    Start Postfix  on boot.

    systemctl enable postfix

    [root@ip-k8smaster ~]# systemctl restart postfix
    [root@ip-k8smaster ~]# systemctl enable postfix

    Configure Postfix

    Open the /etc/postfix/main.cf and add the following lines to the file.

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

    Save the main.cf file and close the editor.

    Configure Postfix SASL Credentials

    Add the Gmail credentials for authentication. Create a "/etc/postfix/sasl_passwd" file and add following line:

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

    Replace valid Gmail credentials with  username and password values. The sasl_passwd file can now be saved and closed.

    Create a Postfix lookup table from the sasl_passwd text file by running the following command.

    postmap /etc/postfix/sasl_passwd

    Now Restrict access to the sasl_passwd files .

    chown root:postfix /etc/postfix/sasl_passwd*
    chmod 640 /etc/postfix/sasl_passwd*

    Reload the Postfix configuration.

    systemctl reload postfix



    Use the mail command to test the relay.

    echo "This is Manas." | mail -s "message" xxxxx@yyy.com

    The destination address should now receive the test message.

    If you have issues like " Application-specific password required" , then you need to create app password and provide that in sasl_passwd file.

    The logs can be viewed at "/var/log/maillog". The debug level can be increased by adding the following lines to the /etc/postfix/main.cf and restart postfix.


    Check Postfix Config:

    [root@ip-k8smaster ~]# postconf -n

    No comments