How to use gmail as relay host for postfix in Ubuntu 12.04

Reference: https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/

first of all, please note that when the recipient receives your email, the sender will be changed to the gmail account you are going to use

Add the following code in /etc/postfix/main.cf

# Define relay host to gmail
relayhost = smtp.gmail.com:587

# Gmail requires tls connections
smtp_use_tls=yes
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt #Your trusted CA list

# Gmail requires authentication
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd

# define authentication mechanism
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_sasl_tls_security_options = noanonymous

Then, edit your authentication file /etc/postfix/sasl_passwd as the following format:

smtp.gmail.com    USERNAME:PASSWORD

please note that username should be your full mail address, include “@gmail.com” or your own domain name.
then update the file to postfix format for using:

#postmap /etc/postfix/sasl_passwd

remember restart postfix at the last

#service postfix restart