Easiest way to relay mail through GMail using Postfix and Debian

There are times when you need need to send E-Mail from your Linux server.  The reason that I had to set up E-Mail was because I had a php script that E-Mailed me once some one filled out their information on the website.  The easiest way to get your server sending mail is using GMail to send the mail.  This tutorial will describe how to set it up on Ubuntu / Debian.

sudo aptitude install postfix libsasl2 ca-certificate libsasl2-modules

You want to add the fowling lines to your /etc/postfix/main.cf

relayhost = [smtp.gmail.com]:587<br /> smtp_sasl_auth_enable = yes<br /> smtp_sasl_password_maps = hash:/etc/postfix/gmail_passwd
smtp_sasl_security_options = noanonymous<br /> smtp_tls_CAfile = /etc/postfix/cacert.pem<br /> smtp_use_tls = yes

You need to edit /etc/postfix/gmail_passwd and add the fowling information below, edit it to fit your needs.

[smtp.gmail.com]:587    UserMame@gmail.com:PassWord

Next you need to turn the gmai_passwd file into something that postfix can understand, you can use the command below to accomplish that.

sudo chmod 400 /etc/postfix/sasl_passwd<br /> sudo postmap /etc/postfix/sasl_passwd

Now you can restart postfix but it will complain about not being able to authenticate the certificate.  In order to fix the problem we will use the ca-certificate package we installed earlier to tell it where it can validate the certificate.

cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

Now restart postfix for the final time.