Add email recipient in header in Postfix

I have an email virtual alias in Postfix that forwards the email to another address: [email protected] -> [email protected]. The domain2.com is a catch-all that parses the email and sends it to a script. In the script I can access the email headers, where I see the header To: <[email protected]>. I would also like to know what is the end recipient, in this example [email protected]. Do you know any way to add it as a custom header? Thanks

Update: I'm using Centos 7 with Virtualmin. Here is my postconf -n

alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
allow_percent_hack = no
authorized_submit_users = root
broken_sasl_auth_clients = yes
command_directory = /usr/sbin
config_directory = /etc/postfix
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
debug_peer_level = 2
debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id & sleep 5
html_directory = no
inet_interfaces = all
inet_protocols = ipv4
mail_owner = postfix
mailbox_size_limit = 0
mailq_path = /usr/bin/mailq.postfix
manpage_directory = /usr/share/man
mydestination = $myhostname, localhost.$mydomain, localhost, domain1
myhostname = mail.domain1.com
newaliases_path = /usr/bin/newaliases.postfix
queue_directory = /var/spool/postfix
readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES
recipient_canonical_classes = envelope_recipient
recipient_canonical_maps = tcp:127.0.0.1:10002
sample_directory = /usr/share/doc/postfix-2.10.1/samples
sender_bcc_maps = hash:/etc/postfix/bcc
sender_canonical_classes = envelope_sender
sender_canonical_maps = tcp:127.0.0.1:10001
sendmail_path = /usr/sbin/sendmail.postfix
setgid_group = postdrop
smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
transport_maps = hash:/etc/postfix/transport
unknown_local_recipient_reject_code = 550
virtual_alias_maps = hash:/etc/postfix/virtual

The headers that I receive currently in [email protected] are:

X-Envelope-To: <[email protected]>
Received: from mail-qk0-f170.google.com (mail-qk0-f170.google.com [209.85.220.170]) by mail.domain1.com (Postfix) with ESMTP id DD6A419EAE  for <[email protected]>; Mon, 14 Mar 2016 14:26:50 +0000 (UTC)
Received: by mail-qk0-f170.google.com with SMTP id s68so75293658qkh.3        for <[email protected]>; Mon, 14 Mar 2016 07:26:50 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;        d=sender1-com.20150623.gappssmtp.com; s=20150623;        h=date:subject:from:message-id:mime-version:to;        bh=hqH2fYTPM8TH6JmNeCLDMgOPLarFPGZIH9iNM2C8si8=;        b=QLx2ne7ecBDzFf7KxvphGmJ3j2ofGv2hPsnDQc/2WGaIsXkrNUactEHg06dHvUx7Fr         A6sfsY3ldQeFYctYkaFkHmIIahOA5wU+//vQt63eezyT7DwPPgdV3M325hrRIl/yXtzx         /a9i6o9HodPKLO9mzw3O/BFrkhX2/yns8+L4m6flxh7pO6XR/L+7CA52q6tikj3LA0vY         I6EVUjALjGESkVvfLQCqz0xqihrydx5ZjHQs8/kejPyHNKjuG1mhT71Aw8fOpm5cr/U4         oFtg8D8mk04KSNCJpPfj4r8q6PWkk2lWmrMZBqVb1lgsc++Ms94OnTUOP66ahkF4FSHe         hk0Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;        d=1e100.net; s=20130820;        h=x-gm-message-state:date:subject:from:message-id:mime-version:to;        bh=hqH2fYTPM8TH6JmNeCLDMgOPLarFPGZIH9iNM2C8si8=;        b=C+MBNHvzJgDJytvU0O77IMci0KDVbBAoE7vjh1l7ywehwO8rbts8g1Aw1rLKqVRC1j         7AuSSNl4Aw24EHrKJ/d0FeeZj7wYttzieAGcoVHO/aU8nU1VB4z1551/JNLdERT12JsJ         Q9vi8wSdn/YyywVcQrYAkj2AbJJuVDxAWwnh+DFKMtfSqhSjLouXIgHW3Em/n6HKk/Fo         gQ3YX3VcbVhh/Wb1bnhYKa9p+dHycxR/6FqH6iUPfC9FOoyQgyT5Dy22uf14qc+4BfxL         aI7zkWZSnrTGvAo0qZYv83Ju9y/OqnIzu+8rtcvvFTMBlfbSRZk7EPHoK6epOvk2gGCr         YdUg==
X-Gm-Message-State: AD7BkJJN9qbPo6gcrUH1cqYbdbf1ONXUw66KBuM1smFgz/9/kg7LqgsyGfGxkI5riSw59A==
X-Received: by 10.55.20.224 with SMTP id 93mr31121731qku.109.1457965610167;        Mon, 14 Mar 2016 07:26:50 -0700 (PDT)
Received: from localhost (ec2-52-90-4-238.compute-1.amazonaws.com. [52.90.4.238])        by smtp.gmail.com with ESMTPSA id x64sm9199694qhx.47.2016.03.14.07.26.49        for <[email protected]>        (version=TLSv1/SSLv3 cipher=OTHER);        Mon, 14 Mar 2016 07:26:49 -0700 (PDT)
Date: Mon, 14 Mar 2016 07:26:40 -0700
X-Mailer: Polymail
Subject: test
From: Jesús Carrera <[email protected]>
X-Polymail-Id: 56e6c9f42bcdb00a00000023
Message-ID: <[email protected]>
Mime-Version: 1.0
To: <[email protected]>

I don't care about the name of the header, just want to know how to add the last recipient to the headers. How to do that?

As I said before - you can use X-Original-To/To headers.

Basic test

# echo "Hello world" | swaks -4 --server localhost:25 --from [email protected] --to [email protected] --h-Subject "Test message" --body -
=== Trying localhost:25...
=== Connected to localhost.
<-  220 smtp.example.net ESMTP Postfix
 -> EHLO jenkins.example.net
<-  250-smtp.example.net
<-  250-PIPELINING
<-  250-SIZE 10240000
<-  250-VRFY
<-  250-ETRN
<-  250-ENHANCEDSTATUSCODES
<-  250-8BITMIME
<-  250 DSN
 -> MAIL FROM:<[email protected]>
<-  250 2.1.0 Ok
 -> RCPT TO:<[email protected]>
<-  250 2.1.5 Ok
 -> DATA
<-  354 End data with <CR><LF>.<CR><LF>
 -> Date: Tue, 15 Mar 2016 09:34:18 +0000
 -> To: [email protected]
 -> From: [email protected]
 -> Subject: Test message
 -> X-Mailer: swaks v20130209.0 jetmore.org/john/code/swaks/
 ->
 -> Hello world
 ->
 ->
 -> .
<-  250 2.0.0 Ok: queued as 8F6CE1C0B33
 -> QUIT
<-  221 2.0.0 Bye
=== Connection closed with remote host.

Check maillog

# grep 8F6CE1C0B33 /var/log/maillog
Mar 15 09:34:18 ci postfix/smtpd[24556]: 8F6CE1C0B33: client=localhost[127.0.0.1]
Mar 15 09:34:18 ci postfix/cleanup[24559]: 8F6CE1C0B33: message-id=<[email protected]>
Mar 15 09:34:18 ci postfix/qmgr[24553]: 8F6CE1C0B33: from=<[email protected]>, size=428, nrcpt=1 (queue active)
Mar 15 09:34:18 ci postfix/smtp[24560]: 8F6CE1C0B33: to=<[email protected]>, orig_to=<[email protected]>, relay=smtp.example.com[192.168.88.230]:25, delay=0.06, delays=0.02/0.01/0.01/0.03, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as BFFDA6065D)
Mar 15 09:34:18 ci postfix/qmgr[24553]: 8F6CE1C0B33: removed

As you can see originally the email was sent to [email protected]

orig_to=<[email protected]>

but was delivered to [email protected] via relay smtp.example.com[192.168.88.230]:25

to=<[email protected]>

Maillog from the relay

# grep 292366065D /var/log/maillog
Mar 15 06:38:07 relay postfix/smtpd[19245]: 292366065D: client=smtp.example.net[192.168.88.87]
Mar 15 06:38:07 relay postfix/cleanup[19248]: 292366065D: message-id=<[email protected]>
Mar 15 06:38:07 relay postfix/qmgr[19244]: 292366065D: from=<[email protected]>, size=622, nrcpt=1 (queue active)
Mar 15 06:38:07 relay postfix/virtual[19249]: 292366065D: to=<[email protected]>, relay=virtual, delay=0.03, delays=0.02/0.01/0/0, dsn=2.0.0, status=sent (delivered to maildir)
Mar 15 06:38:07 relay postfix/qmgr[19244]: 292366065D: removed

The email itself

# cat /var/mail/virtual/example.com/alex
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Received: from smtp.example.net (smtp.example.net [192.168.88.87])
        by smtp.example.com (Postfix) with ESMTP id 292366065D
        for <[email protected]>; Tue, 15 Mar 2016 06:38:07 -0400 (EDT)
Received: from jenkins.example.net (localhost [127.0.0.1])
        by smtp.example.net (Postfix) with ESMTP id E71EA1C0B33
        for <[email protected]>; Tue, 15 Mar 2016 09:38:10 +0000 (UTC)
Date: Tue, 15 Mar 2016 09:38:10 +0000
To: [email protected]
From: [email protected]
Subject: Test message
X-Mailer: swaks v20130209.0 jetmore.org/john/code/swaks/
Message-Id: <[email protected]>

Hello world

As you can see

  • [X-Original-To]: will contain final recipient
  • [To]: will contain original recipient

but I don't know how to do it

The last thing you can try - is to add header via header_checks option. You should add the following option on the "relay" server

# main.cf
header_checks = pcre:/etc/postfix/add_original_rcpt.pcre

# cat add_original_rcpt.pcre
/^To:(\s)?(.*)$/ PREPEND X-Original-To: $2