Relay Mail From Localhost Using Postfix

Purpose

On a system on which we do not wish to have local mail, we need to relay locally-generated (system) mail to another host. This article discusses how to set that up.

Preliminaries

  • We assume the System MTA is Postfix.

Procedure

  1. Configure /etc/aliases

    1. Add a line with root: youruser@example.com (with the desired destination mail of course) to /etc/aliases
    2. Execute newaliases
  2. Configure /etc/postfix/canonical

    1. Create /etc/postfix/canonical with contents such as the following, where host.example.com is the host’s hostname, example.com is your mail domain, and other.example.com is the domain of the recipient (which may be your domain: example.com in this example):
    @host.example.com   youruser@other.example.com
    @example.com        youruser@other.example.com
    @host.localdomain   youruser@other.example.com
    @localhost          youruser@other.example.com
    @localhost.localdomain    youruser@other.example.com
    *                   youruser@other.example.com
    
    1. Execute postmap hash:/etc/postfix/canonical
  3. Configure /etc/postfix/main.cf

    1. Set myorigin = $mydomain
    2. Set mydestination = (this tells the system we don’t accept any mail as local).
    3. Set recipient_canonical_maps = hash:/etc/postfix/canonical
    4. Set inet_interfaces = localhost
    5. Set mynetworks_style = host
    6. Set relay_domains = example.com (obviously example.com should be yourdomain.tld)
  4. Restart postfix

  5. Test sending mail from your localhost to various addresses (especially ones related to your domain and your localhost) and make sure the results are what you expect.

End Note

If you need to authenticate to your mail relay to send mail (e.g. if you are not only sending mail to yourself), then you will need to configure SASL as usual. Otherwise, it really is that easy!.