Wednesday, 5 June 2013

Setting up linux to relay mail via gmail

We use gmail for business and have gmail accounts etc. We also run servers that need to send email out so we do it via gmail to ensure we have credibility and don't end up in SPAM filters etc. I have recently installed wordpress and that uses phpmail to send emails but which relies on an underlying mail delivery system to send it on. Below is how you get it to work with gmail.

  1. sudo apt-get install postfix and choose "satellite system" as the system to configure. Set the relay host to smtp.gmail.com. You can optionally add port 587 to the end but I think it automatically does that anyway?
  2. Once the install is finished, sudo nano /etc/postfix/main.cf scroll to the end and set the following entries: 
  3. Set myhostname = whatever you want to appear as the mail host in the email trail, this will also affect how delivery addresses are resolved if, for instance, you mail an address without an @ sign. A standard would be servername.domain.name
  4. Add mydomain = the domain part of the host e.g. example.co.uk
  5. remove the entries from mydestination, otherwise you might get addresses rejected.
  6. Add the following new entries in the same block. Items in bold are my choice of names:
    smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
    smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
    smtp_sasl_auth_enable = yes
    smtp_use_tls = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_sasl_security_options = noanonymous
    smtp_sasl_tls_security_options = noanonymous
  7. You now need to create the sasl_passwd file required to authenticate with gmail.
  8. Run sudo nano sasl_passwd in the postfix directory and add a single line equal to the following, items in bold need setting to relevant values:
    smtp.gmail.com something@example.com:password
  9. Note the colon between username and password. These need to be valid login credentials for gmail (obviously?).
  10. Run sudo chown root:root sasl_passwd and  sudo chmod go-rw sasl_passwd
  11. Run the command sudo postmap saslpasswd and if you get an error, you will need to install it (and run it again). It is called sasl2-bin package on ubuntu.
  12. Ensure that both the sasl_passwd and sasl_passwd.db files are owned by root:root and are only read/writable by root.
  13. sudo service postfix restart
  14. To test it, create a file (called test.email for example) in your home directory and type sendmail -v user@domainname < test.mail
Post a Comment