0

I have an email virtual alias in Postfix that forwards the email to another address: email1@domain1.com -> email2@domain2.com. 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: <email1@domain1.com>. I would also like to know what is the end recipient, in this example email2@domain2.com. 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 email2@domain2.com are:

X-Envelope-To: <email1@domain1.com>
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 <email1@domain1.com>; Mon, 14 Mar 2016 14:26:50 +0000 (UTC)
Received: by mail-qk0-f170.google.com with SMTP id s68so75293658qkh.3        for <email1@domain1.com>; 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 <email1@domain1.com>        (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 <me@sender1.com>
X-Polymail-Id: 56e6c9f42bcdb00a00000023
Message-ID: <56e6c9f42bcdb00a00000023@polymail.io>
Mime-Version: 1.0
To: <email1@domain1.com>
Jesús Carrera
  • 143
  • 2
  • 7
  • You can use "X-Original-To" header – ALex_hha Mar 14 '16 at 16:43
  • 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? – Jesús Carrera Mar 15 '16 at 08:07
  • Have I correctly understood, that postfix forward email for domain2.com to some gmail account? – ALex_hha Mar 16 '16 at 11:41
  • No, the gmail account is the one I'm sending from (me@sender1.com) – Jesús Carrera Mar 17 '16 at 07:25
  • email1@domain1.com from the headers is original recipient or aliased one? Does postfix on centos 7 is a final destination for domains domain1.com and domain2.com? – ALex_hha Mar 17 '16 at 12:22
  • email1@domain1.com is the aliased to email2@domain2.com. I don't know the system behind domain2.com since it's not on my server. – Jesús Carrera Mar 18 '16 at 12:34
  • If you don't have access to the relay (smtp server for domain1.com) - there is no other way to get final recipient. It seems such headers are cleaned on the relay. because by default postfix should add them – ALex_hha Mar 18 '16 at 22:47
  • I have access to domain1.com, the one doing the alias, and that's where I want to add the header with the forwarded email (email2@domain2.com), but I don't know how to do it – Jesús Carrera Mar 18 '16 at 23:26
  • Related: [How I can parse email to get original recipient of an email?](http://serverfault.com/q/715182/58408) – user Apr 28 '16 at 09:47

1 Answers1

1

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 alex.hha@gmail.com --to alex@example.net --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:<alex.hha@gmail.com>
<-  250 2.1.0 Ok
 -> RCPT TO:<alex@example.net>
<-  250 2.1.5 Ok
 -> DATA
<-  354 End data with <CR><LF>.<CR><LF>
 -> Date: Tue, 15 Mar 2016 09:34:18 +0000
 -> To: alex@example.net
 -> From: alex.hha@gmail.com
 -> 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=<20160315093418.8F6CE1C0B33@smtp.example.net>
Mar 15 09:34:18 ci postfix/qmgr[24553]: 8F6CE1C0B33: from=<alex.hha@gmail.com>, size=428, nrcpt=1 (queue active)
Mar 15 09:34:18 ci postfix/smtp[24560]: 8F6CE1C0B33: to=<alex@example.com>, orig_to=<alex@example.net>, 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 alex@example.net

orig_to=<alex@example.net>

but was delivered to alex@example.com via relay smtp.example.com[192.168.88.230]:25

to=<alex@example.com>

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=<20160315093810.E71EA1C0B33@smtp.example.net>
Mar 15 06:38:07 relay postfix/qmgr[19244]: 292366065D: from=<alex.hha@gmail.com>, size=622, nrcpt=1 (queue active)
Mar 15 06:38:07 relay postfix/virtual[19249]: 292366065D: to=<alex@example.com>, 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: <alex.hha@gmail.com>
X-Original-To: alex@example.com
Delivered-To: alex@example.com
Received: from smtp.example.net (smtp.example.net [192.168.88.87])
        by smtp.example.com (Postfix) with ESMTP id 292366065D
        for <alex@example.com>; 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 <alex@example.net>; Tue, 15 Mar 2016 09:38:10 +0000 (UTC)
Date: Tue, 15 Mar 2016 09:38:10 +0000
To: alex@example.net
From: alex.hha@gmail.com
Subject: Test message
X-Mailer: swaks v20130209.0 jetmore.org/john/code/swaks/
Message-Id: <20160315093810.E71EA1C0B33@smtp.example.net>

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
ALex_hha
  • 7,193
  • 1
  • 25
  • 40
  • My Postfix configuration is not adding the *X-Original-To*, nor the *Delivered-To* headers. Can you let me know what configuration makes that happen in Postfix? – Jesús Carrera Mar 15 '16 at 14:41
  • I'm using default one - CentOS 6, postfix 2.6.6. No customization at all, just out from the box. It would be great if you add all headers from any email to the question. BTW domain1.com and domain2.com are handled on the same server? – ALex_hha Mar 15 '16 at 14:50
  • I'm using Centos 7 with Virtualmin. domain1.com is at my server, domain2.com is the one doing the script parsing and I have no access to. I'll add my `postconf -n` to the question so you can see if I have something different than you – Jesús Carrera Mar 15 '16 at 18:32
  • Add headers from any email on domain domain2.com to the question – ALex_hha Mar 15 '16 at 19:23
  • ok, headers added – Jesús Carrera Mar 16 '16 at 08:04
  • My postfix gives a correct "X-Original-To" value, but for kerio connect I need "X-Envelope-To". worked, but useless: /^To:(\s)?(.*)$/ PREPEND X-Envelope-To: $2 not working: /^X-Original-To:(\s)?(.*)$/ PREPEND X-Envelope-To: $2 Have anyone an idea? – Fusca Software Oct 07 '19 at 16:16