Saturday, March 9, 2013

Using postfix to use gmail as smtp relay

Install the required packages

sudo aptitude install postfix libsasl2 ca-certificates libsasl2-modules


relayhost = [smtp.gmail.com]:587 # The google smtp server and the port

smtp_sasl_auth_enable = yes # Enable Authentication,

smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd # Where is the authentication information                  located

smtp_sasl_security_options = noanonymous # According to http://www.postfix.org/SASL_README.html

smtp_tls_CAfile = /etc/postfix/cacert.pem # The certificate file
smtp_use_tls = yes # Use tls


Then create the authentication file at /etc/postfix/sasl_passwd with the following content.

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

 Then,

 sudo chmod 400 /etc/postfix/sasl_passwd # The right permission for the file

 sudo postmap /etc/postfix/sasl_passwd # let postfix know about the file

cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem # Installing the certificate created earlier.

 This should get you going. reload postfix configuration.


 If you get an error like postfix reload failed, make sure no sendmail( sendmail will be uninstalled while installing postfix but older processes won't die and block port 25 which is your local smtp port) or any other running processes which is blocking port 25, kill them and then reload. Should just work, now any mail client like mail() for php or pony for ruby on rails should be able to send email using the gmail address specified in the /etc/postfix/sasl_passwd #resources

Links to follow

  1. https://help.ubuntu.com/community/PostfixBasicSetupHowto
  2. http://www.postfix.org/ 
  3. https://help.ubuntu.com/community/Postfix