3

This question has a related answer here: How can I forward mail while saving a copy using postfix and mysql? But there are two problems with it:

  1. the OP's setup uses MySQL and all answers are geared towards that, and
  2. the answer that seems to be the one I want, the one that uses main.cf and hash tables, is from 2009 (and got no attention then). So I'd like to ask this one again, for a config file setup instead of a MySQL setup.

I'm forwarding mail to my gmail address successfully with these lines in /etc/postfix/main.cf:

virtual_alias_domains = example.com, example.net
virtual_alias_maps = hash:/etc/postfix/virtual

and in /etc/postfix/virtual:

@example.com myusername
@example.net myusername
myusername me@gmail.com

This works great and forwards all mail to me@gmail.com, but I'd like to keep a copy on the server. Ideally they'd go to myusername's inbox. What's the proper way to do this?


My /var/log/mail.log log after sending a test email:

Oct  8 09:16:26 myservername postfix/qmgr[1494]: 89736105BA7: from=<sending_address@gmail.com>, size=1676, nrcpt=2 (queue active)
Oct  8 09:16:26 myservername postfix/trivial-rewrite[7338]: warning: do not list domain mydomain.com in BOTH mydestination and virtual_alias_domains
Oct  8 09:16:26 myservername postfix/cleanup[7339]: B7888105BB0: message-id=<CAGS0grDYkvWj_wxpGKW1h7XJZ5YEky3DwPBnB4XdvE3TU-W7jg@mail.gmail.com>
Oct  8 09:16:26 myservername postfix/local[7340]: 89736105BA7: to=<myusername@mydomain.com>, relay=local, delay=0.21, delays=0.19/0.01/0/0.01, dsn=2.0.0, status=sent (forwarded as B7888105BB0)
Oct  8 09:16:26 myservername postfix/qmgr[1494]: B7888105BB0: from=<sending_address@gmail.com>, size=1802, nrcpt=1 (queue active)
Oct  8 09:16:26 myservername postfix/smtpd[7335]: disconnect from mail-io0-f171.google.com[209.85.223.171]
Oct  8 09:16:27 myservername postfix/smtp[7341]: connect to gmail-smtp-in.l.google.com[2607:f8b0:400e:c04::1a]:25: Network is unreachable
Oct  8 09:16:27 myservername postfix/smtp[7342]: connect to gmail-smtp-in.l.google.com[2607:f8b0:400e:c04::1a]:25: Network is unreachable
Oct  8 09:16:27 myservername postfix/smtp[7342]: B7888105BB0: to=<mygmailaddr@gmail.com>, orig_to=<myusername@mydomain.com>, relay=gmail-smtp-in.l.google.com[74.125.28.26]:25, delay=0.89, delays=0/0.01/0.6/0.27, dsn=2.0.0, status=sent (250 2.0.0 OK 1444310187 n40si31732683ioi.161 - gsmtp)
Oct  8 09:16:27 myservername postfix/qmgr[1494]: B7888105BB0: removed
2

Just modify the last line

myusername me@gmail.com

so it becomes

myusername myusername me@gmail.com

Explanation

When postfix discover that an address was aliased to himself, then mail will be delivered to corresponding user instead

3
  • That doesn't seem to have worked, although it didn't break anything. I did do postmap /etc/postfix/virtual and postfix reload after, and when that didn't work, restarted postfix.
    – felwithe
    Sep 9 '15 at 0:32
  • Can you share full mail.log entry when you send email? Please post it by editing your question.
    – masegaloeh
    Sep 9 '15 at 0:34
  • updated with log
    – felwithe
    Oct 8 '15 at 13:24
0

You can also set up a file .forward in the user's home directory with content:

\myusername, me@gmail.com

More details can be found here: https://www.ccsf.edu/Pub/UNIXhelp/mail/file_forward.html and http://www.postfix.org/local.8.html

The system administrator can set up one or more system-wide send- mail-style alias databases. Users can have sendmail-style ~/.forward files. Mail for name is delivered to the alias name, to destinations in ~name/.forward, to the mailbox owned by the user name, or it is sent back as undeliverable.

   The system administrator can specify a comma/space  separated  list  of
   ~/.forward like files through the forward_path configuration parameter.
   Upon delivery, the local delivery agent tries each pathname in the list
   until a file is found.

   Delivery via ~/.forward files is done with the privileges of the recip-
   ient.  Thus, ~/.forward like files must be readable by  the  recipient,
   and  their  parent directory needs to have "execute" permission for the
   recipient.

   The forward_path parameter is subject to interpolation of $user (recip-
   ient  username),  $home  (recipient  home directory), $shell (recipient
   shell), $recipient (complete recipient address), $extension  (recipient
   address  extension), $domain (recipient domain), $local (entire recipi-
   ent   address   localpart)   and   $recipient_delimiter.   The    forms
   ${name?value}  and  ${name:value}  expand  conditionally  to value when
   $name is (is not) defined.  Characters that may have special meaning to
   the  shell  or  file  system  are replaced by underscores.  The list of
   acceptable characters is specified  with  the  forward_expansion_filter
   configuration parameter.

   An  alias  or ~/.forward file may list any combination of external com-
   mands, destination file names, :include: directives, or mail addresses.
   See  aliases(5)  for a precise description. Each line in a user's .for-
   ward file has the same syntax as the right-hand part of an alias.

   When an address is found in its own alias expansion, delivery  is  made
   to the user instead. When a user is listed in the user's own ~/.forward
   file, delivery is made to the user's mailbox instead.  An empty ~/.for-
   ward file means do not forward mail.

   In  order to prevent the mail system from using up unreasonable amounts
   of memory, input records read from :include: or from  ~/.forward  files
   are broken up into chunks of length line_length_limit.

   While  expanding  aliases,  ~/.forward  files,  and  so on, the program
   attempts to avoid duplicate deliveries. The duplicate_filter_limit con-
   figuration parameter limits the number of remembered recipients.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.