Friday, 2 October 2009

Forwarding root & user email

Cron-jobs I run send email to the root user and my user (justin), and I'd like these forwarded to an external email account (like gmail). I tried hard to acheive this with sSMTP and nullmailer but just couldn't get either to work...
The solution was to install postfix (the default MTA in Ubuntu), e.g. sudo apt-get install postfix. The installer gives you various options: choose satellite installation and then give your ISP's smtp server as the relayhost.
If, like me, your chosen smtp server requires authentication (i.e. you have to log in) then there are a few additional steps:
  1. Add the following lines to /etc/postifix/main.cf:
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl/smtp-passwd
    # The following line should be blank after "=" to allow plaintext passwords
    smtp_sasl_security_options =
  2. Check, in the same file, that the relayhost line is now pointing to your isp mail server:
    relayhost = myserver.isp.com
  3. Create a file (e.g. /etc/postfix/sasl/smtp-passwd) containing the user and password like the following:
    myserver.isp.com username:passwd
  4. Turn this text file into a binary db file with:
    sudo postmap hash:/etc/postfix/sasl/smtp-passwd
Finally to forward your root and personal emails to an external email address add .forward files to /home/justin (in my case) and /root/. The root .forward should contain a non-root user name (i.e. justin in my case), and my .forward file should contain the external (gmail) address. Apparently there can be issues fowarding root email to non-local addresses; this way root gets fowarded locally which is then forwarded externally.
Then restart postfix with:/etc/init.d/postfix restart
See this page by David Grant for another view on similar set-up, and reasons why he doesn't like sSMTP.

No comments: