Some times it's required that the mailserver relays all outgoing email through another server. If authentication is not required, you only need to add a relayhost with the following command for the outgoing email relay to work:
postconf -e relayhost=smtp-relay.example.com
But sometimes it's more complicated and the email relay server requires authentication. In the following example, Gmail is used as an example of an outgoing mailserver to relay messages. Gmail requires authentication.
First, in /etc/postfix/main.cf, add the following
relayhost = smtp.gmail.com:587 smtp_use_tls = yes smtp_sasl_auth_enable = yes smtp_sasl_security_options = smtp_sasl_password_maps = hash:/etc/postfix/relay_passwd
Create the relay_passwd file using the following commands:
echo "smtp.gmail.com username@gmail.com:password" > /etc/postfix/relay_passwd postmap /etc/postfix/relay_passwd chmod 600 /etc/postfix/relay_passwd*
and finally
postfix reload
for the changes to take effect.
If things don't work as expected. Try sending an email to yourself from the command line:
echo test | mail -s testsubject me@example.com
Running mailq will display clues as to what has gone wrong.
Next step is monitoring /var/log/maillog for clues as to what's not working. It will display authentication errors among other things.
Run the following on the command line:
ls -l /etc/postfix/relay_passwd*
to make sure that relay_passwd and relay_passwd.db has the same timestamp and rerun the postmap command above if they don't match.