How to configure Postfix to use Gmail as mail relay in RHEL7

Let’s create the VM first:

[root@rhel7 ~]# virt-install \
> --hvm \
> --name mx1 \
> --ram 1024 \
> --disk path=/kvm/mx1.img,size=50 \
> --vcpus 1 \
> --os-type linux \
> --os-variant rhel7 \
> --network bridge=virbr0 \
> --graphics none \
> --location 'http://192.168.122.3/rhel7' \
> --extra-args "ks=http://192.168.122.3/ksfiles/rhel7-minimal-ks.cfg \
> console=tty0 console=ttyS0,115200 SERVERNAME=mx1.jefrey.io IPADDR=192.168.122.7"

Starting install...
...
...
[root@mx1 ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.122.7  netmask 255.255.255.0  broadcast 192.168.122.255
        inet6 fe80::5054:ff:feaf:920a  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:af:92:0a  txqueuelen 1000  (Ethernet)
        RX packets 1174  bytes 83014 (81.0 KiB)
        RX errors 0  dropped 32  overruns 0  frame 0
        TX packets 101  bytes 11919 (11.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@mx1 ~]#
[root@mx1 ~]# host mx1
mx1.jefrey.io has address 192.168.122.7
[root@mx1 ~]# 
[root@mx1 ~]# ping -c2 www.google.com
PING www.google.com (74.125.68.147) 56(84) bytes of data.
64 bytes from sc-in-f147.1e100.net (74.125.68.147): icmp_seq=1 ttl=43 time=10.4 ms
64 bytes from sc-in-f147.1e100.net (74.125.68.147): icmp_seq=2 ttl=43 time=13.4 ms

--- www.google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2505ms
rtt min/avg/max/mdev = 10.402/11.922/13.442/1.520 ms
[root@mx1 ~]#
[root@mx1 ~]# vi /etc/sysconfig/network
[root@mx1 ~]# cat !$
cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=mx1.jefrey.io
GATEWAY=192.168.122.1
GATEWAYDEV=eth0
[root@mx1 ~]#

The gmail account that’ll be use has less secure access to application turned on, refer to https://www.google.com/settings/security/lesssecureapps

Let’s install the necessary packages and make start on boot (although this maybe already in place):

[root@mx1 ~]# yum -y install postfix cyrus-sasl-plain mailx
[root@mx1 ~]# systemctl enable postfix
[root@mx1 ~]# systemctl restart postfix
[root@mx1 ~]# systemctl status postfix

Edit the configuration file; I added the last 8 lines:

[root@mx1 ~]# vi /etc/postfix/main.cf
[root@mx1 ~]# 
[root@mx1 ~]# grep -v '#' /etc/postfix/main.cf | sed '/^$/d'
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
inet_interfaces = localhost
inet_protocols = all
mydestination = $myhostname, localhost.$mydomain, localhost
unknown_local_recipient_reject_code = 550
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
 
  
debug_peer_level = 2
debugger_command =
     PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
     ddd $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.10.1/samples
readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES
myhostname = mx1.jefrey.io
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
[root@mx1 ~]#

Edit /etc/postfix/sasl_passwd. Format of the content is:

[smtp.gmail.com]:587 gmail-id@gmail.com:password

[root@mx1 ~]# vi /etc/postfix/sasl_passwd
[root@mx1 ~]# 
[root@mx1 ~]# cat /etc/postfix/sasl_passwd 
[smtp.gmail.com]:587 jcxxxb@gmail.com:XXXXXXXX
[root@mx1 ~]#

Set necessary permissions:

[root@mx1 ~]# postmap /etc/postfix/sasl_passwd
[root@mx1 ~]# chown root:postfix /etc/postfix/sasl_passwd*
[root@mx1 ~]# chmod 640 /etc/postfix/sasl_passwd*
[root@mx1 ~]#

Test:

[root@mx1 ~]# echo "This is a test." | mail -s "test message" jcxxxb@gmail.com
[root@mx1 ~]# 
[root@mx1 ~]# tail -5 /var/log/maillog 
Jun  3 23:17:13 mx1 postfix/pickup[1247]: 525305B4E: uid=0 from=<root>
Jun  3 23:17:13 mx1 postfix/cleanup[1320]: 525305B4E: message-id=<20150603151713.525305B4E@mx1.jefrey.io>
Jun  3 23:17:13 mx1 postfix/qmgr[1248]: 525305B4E: from=<root@mx1.jefrey.io>, size=433, nrcpt=1 (queue active)
Jun  3 23:17:16 mx1 postfix/smtp[1261]: 525305B4E: to=<jcxxxb@gmail.com>, relay=smtp.gmail.com[74.125.200.108]:587, delay=3.2, delays=0.06/0/2.1/1.1, dsn=2.0.0, status=sent (250 2.0.0 OK 1433344637 d12sm1114425pbu.14 - gsmtp)
Jun  3 23:17:16 mx1 postfix/qmgr[1248]: 525305B4E: removed
[root@mx1 ~]#

Done!