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.
- 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?
- Once the install is finished, sudo nano /etc/postfix/main.cf scroll to the end and set the following entries:
- 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
- Add mydomain = the domain part of the host e.g. example.co.uk
- remove the entries from mydestination, otherwise you might get addresses rejected.
- 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 - You now need to create the sasl_passwd file required to authenticate with gmail.
- 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 - Note the colon between username and password. These need to be valid login credentials for gmail (obviously?).
- Run sudo chown root:root sasl_passwd and sudo chmod go-rw sasl_passwd
- 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.
- Ensure that both the sasl_passwd and sasl_passwd.db files are owned by root:root and are only read/writable by root.
- sudo service postfix restart
- To test it, create a file (called test.email for example) in your home directory and type sendmail -v user@domainname < test.mail