Postfix - Virtual Aliasing (Email Redirect)

About

The main applications of virtual aliasing are:

  • To redirect mail for one address to one or more addresses.
  • To implement virtual alias domains where all addresses are aliased to addresses in other domains.

With virtual alias domains, each hosted address is aliased to:

Virtual alias domains are not to be confused with the virtual mailbox domains. With virtual mailbox domains, each recipient address can have its own mailbox. ie If you want to store the email locally without creating a linux user each time (ie not redirect them), you need to implements virtual MAILBOX

This process is implemented with the postfix virtual domain mail delivery agent.

The aliases table provides also a system-wide mechanism to redirect mail for local recipients. You can redirect to a command. where the E-mail message is passed on standard input. See postconf.5.html and postconf.5.html

Configuration

Definition of the domains

With the postconf.5.html configuration, all recipient addresses are aliased to addresses in other domains.

A virtual alias domain can not be defined:

This feature is for Postfix > 2.0 (before the domain was in the virtual_alias_table)

virtual_alias_domains = example.com ...other hosted domains...
# reload postfix /etc/postfix/main.cf
# optional only if you want to see the change immediately
postfix reload

Postfix is final destination for the specified list of virtual alias domains, that is, domains for which all addresses are aliased to addresses in other local or remote domains.

Virtual alias table

The virtual alias table is specified in the text file /etc/postfix/virtual that serves as input to the postmap command in order to output an indexed file (in dbm or db format) to speed up lookup.

virtual_alias_maps configuration

Just for info because this configuration is the default and should then not be changed

  • The virtual_alias_maps file defined the location of the alias text file hash is the database type (some systems use dbm). See the output from “postconf -m” for available database types.
virtual_alias_maps = hash:/etc/postfix/virtual
# reload postfix /etc/postfix/main.cf
postfix reload 

Alias definition (/etc/postfix/virtual)

Basically the format of this file is:

pattern address, address, 
 address 
# comment

where:

  • pattern: A line has a pattern that defines the address that should be used if it matches an email. Each pattern is a regular expression that is applied to the entire address being looked up.
  • An address is a local address or to a remote address
  • multi-line text: A line that start with a space continue the previous line
  • Empty lines and whitespace-only lines are ignored,

You can see the whole file format definition at Postfix virtual alias table format

Alias definition Pattern

user@domain address
  • Redirect mail for user@domain to address. This form has the highest precedence.
user@domain address, address, ...
addresses without @domain
user address, address, ...

When append_at_myorigin=yes, append “@$myorigin” to addresses without “@domain”

@domain address
  • Redirect mail for other users in domain to address. This form has the lowest precedence. @domain is a wild-card and catch all addresses even if they does not exist.
@domain address, address, ...

The result becomes the same user in otherdomain. This works only for the first address in a multi-address lookup result.

addresses without “.domain”

When append_dot_mydomain=yes, append .$mydomain

Example

In this example, Mail for all other addresses in example.com is rejected with the error message “User unknown”

[email protected] postmaster
[email protected]       joe
[email protected]      jane
[email protected]   [email protected]
# Uncomment entry below to implement a catch-all address
# @example.com         jim
...virtual aliases for more domains...

Rebuild the index

  • To rebuild the indexed file
# after changing the virtual file
postmap /etc/postfix/virtual

Reject code

unknown_virtual_alias_reject_code default to (550) is the Postfix SMTP server reply code when a recipient address matches $virtual_alias_domains, and $virtual_alias_maps specify a list of lookup tables that does not match the recipient address.

Test

Locally

echo -e "Subject: Virtual Aliasing with Postfix made easy  \n\n This is a redirected email \n" | sendmail aliasedEmail@virtualDomain
  • Check the log
tail -f /var/log/maillog
Jun 15 13:14:50 vps748761 postfix/pickup[25222]: 00F401FBE8: uid=1002 from=<thenico>
Jun 15 13:14:50 vps748761 postfix/cleanup[25330]: 00F401FBE8: message-id=<[email protected]>
Jun 15 13:14:50 vps748761 opendkim[24477]: 00F401FBE8: DKIM-Signature field added (s=vps748761.ovh.net, d=bytle.net)
Jun 15 13:14:50 vps748761 postfix/qmgr[25223]: 00F401FBE8: from=<[email protected]>, size=388, nrcpt=1 (queue active)
Jun 15 13:14:50 vps748761 postfix/smtp[25333]: connect to gmail-smtp-in.l.google.com[2a00:1450:400c:c00::1a]:25: Network is unreachable
Jun 15 13:14:50 vps748761 postfix/smtp[25333]: Untrusted TLS connection established to gmail-smtp-in.l.google.com[173.194.76.26]:25: TLSv1.2 with cipher ECDHE-ECDSA-AES128-GCM-SHA256 (128/128 bits)
Jun 15 13:14:51 vps748761 postfix/smtp[25333]: 00F401FBE8: to=<[email protected]>, orig_to=<[email protected]>, relay=gmail-smtp-in.l.google.com[173.194.76.26]:25, delay=1.5, delays=0.08/0.02/0.57/0.81, dsn=2.0.0, status=sent (250 2.0.0 OK  1592219691 r14si13363181wrm.293 - gsmtp)
Jun 15 13:14:51 vps748761 postfix/qmgr[25223]: 00F401FBE8: removed

  • check your mail

Postfix Redirect Email Example

Support

User unknown in virtual alias table

tail -f /var/log/maillog
Jun 15 12:51:28 vps748761 postfix/error[23201]: 506BB1FD27: to=<[email protected]>, relay=none, delay=0.07, delays=0.04/0.01/0/0.02, dsn=5.1.1, status=bounced (User unknown in virtual alias table)

where:

Documentation / Reference





Discover More
Email - Postfix

Postfix is a SMTP server, supporting LDAP, SMTP AUTH (SASL), TLS The postfix installation installs also an alias to the sendmail command line. Doc - the SMTP...
Postsrsd Spf Pass
How to install the Sender Rewriting Scheme (SRS) on PostFix?

This page is the installation of postsrsd which implements Sender Rewriting Scheme (SRS) for Postfix. It's mandatory if you forward emails via the alias functionality....
Spf Dns Zone
What is the Sender Policy Framework (SPF) in Email?

The Sender Policy Framework (SPF) is a framework that determines if the sender of a email transaction is valid. It's part of the email authentication framework with DKIM where the goal is to: prevent...



Share this page:
Follow us:
Task Runner