1

Topic: Local email delivery for internal domain + relay for external domains

==== REQUIRED BASIC INFO OF YOUR IREDMAIL SERVER ====
- iRedMail version (check /etc/iredmail-release): 1.4.0 MARIADB edition
- Deployed with iRedMail Easy or the downloadable installer? Download
- Linux/BSD distribution name and version:  Debian 10 i386 buster
- Store mail accounts in which backend (LDAP/MySQL/PGSQL):  MySQL
- Web server (Apache or Nginx): Nginx
- Manage mail accounts with iRedAdmin-Pro?
- [IMPORTANT] Related original log or error message is required if you're experiencing an issue.
====

My goal: configure postfix to sent local domain emails locally and external emails with replaced "From" address through external relay. Local delivery should be performed without changing "From" address.

My current config files
/etc/hosts:

192.168.110.11 mail.mydomain.by mail
127.0.0.1       localhost
127.0.1.1       mail.mydomain.by       mail

/etc/hostname:

mail.mydomain.by

etc/postfix/main.cf  postconf -n:

alias_database = hash:/etc/postfix/aliases
alias_maps = hash:/etc/postfix/aliases
allow_min_user = no
allow_percent_hack = no
biff = no
body_checks = pcre:/etc/postfix/body_checks.pcre
bounce_template_file = /etc/postfix/bounce.cf
command_directory = /usr/sbin
compatibility_level = 2
daemon_directory = /usr/lib/postfix/sbin
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
disable_vrfy_command = yes
dovecot_destination_recipient_limit = 1
enable_long_queue_ids = yes
enable_original_recipient = no
header_checks = pcre:/etc/postfix/header_checks
inet_interfaces = all
inet_protocols = all
lmtp_tls_mandatory_protocols = !SSLv2 !SSLv3
lmtp_tls_protocols = !SSLv2 !SSLv3
mail_owner = postfix
mailq_path = /usr/bin/mailq
maximal_queue_lifetime = 2d
message_size_limit = 15728640
mlmmj_destination_recipient_limit = 1

mydestination = localhost, localhost.localdomain
mydomain = mydomain.by
myhostname = mail.mydomain.by
mynetworks = 127.0.0.1 [::1], 192.168.110.0/24, 192.168.103.0/24
myorigin = mail.mydomain.by

newaliases_path = /usr/bin/newaliases
postscreen_access_list = permit_mynetworks cidr:/etc/postfix/postscreen_access.cidr
postscreen_blacklist_action = drop
postscreen_dnsbl_action = drop
postscreen_dnsbl_reply_map = texthash:/etc/postfix/postscreen_dnsbl_reply
postscreen_dnsbl_sites = zen.spamhaus.org=127.0.0.[2..11]*3 b.barracudacentral.org=127.0.0.2*2
postscreen_dnsbl_threshold = 2
postscreen_dnsbl_whitelist_threshold = -2
postscreen_greet_action = drop
proxy_read_maps = $canonical_maps $lmtp_generic_maps $local_recipient_maps $mydestination $mynetworks $recipient_bcc_maps $recipient_canonical_maps $relay_domains $relay_recipient_maps $relocated_maps $sender_bcc_maps $sender_canonical_maps $smtp_generic_maps $smtpd_sender_login_maps $transport_maps $virtual_alias_domains $virtual_alias_maps $virtual_mailbox_domains $virtual_mailbox_maps $smtpd_sender_restrictions $sender_dependent_relayhost_maps
queue_directory = /var/spool/postfix
recipient_bcc_maps = proxy:mysql:/etc/postfix/mysql/recipient_bcc_maps_user.cf proxy:mysql:/etc/postfix/mysql/recipient_bcc_maps_domain.cf
recipient_delimiter = +

relay_domains = $mydestination proxy:mysql:/etc/postfix/mysql/relay_domains.cf
relayhost = [192.168.0.5]:25

sender_bcc_maps = proxy:mysql:/etc/postfix/mysql/sender_bcc_maps_user.cf proxy:mysql:/etc/postfix/mysql/sender_bcc_maps_domain.cf
sender_dependent_relayhost_maps = proxy:mysql:/etc/postfix/mysql/sender_dependent_relayhost_maps.cf
sendmail_path = /usr/sbin/sendmail
setgid_group = postdrop
show_user_unknown_table_name = no
smtp-amavis_destination_recipient_limit = 1
smtp_sasl_auth_enable = yes
smtp_sasl_mechanism_filter = login

smtp_sasl_password_maps = hash:/etc/postfix/sasl_password

smtp_sasl_security_options = noanonymous
smtp_sender_dependent_authentication = yes
smtp_tls_CAfile = $smtpd_tls_CAfile
smtp_tls_CApath = /etc/ssl/certs
smtp_tls_loglevel = 1
smtp_tls_mandatory_protocols = !SSLv2 !SSLv3
smtp_tls_note_starttls_offer = yes
smtp_tls_protocols = !SSLv2 !SSLv3
smtp_tls_security_level = may
smtp_use_tls = yes
smtpd_command_filter = pcre:/etc/postfix/command_filter.pcre
smtpd_data_restrictions = reject_unauth_pipelining
smtpd_end_of_data_restrictions = check_policy_service inet:127.0.0.1:7777
smtpd_helo_required = yes
smtpd_helo_restrictions = permit_mynetworks permit_sasl_authenticated check_helo_access pcre:/etc/postfix/helo_access.pcre reject_non_fqdn_helo_hostname reject_unknown_helo_hostname
smtpd_recipient_restrictions = reject_non_fqdn_recipient reject_unlisted_recipient check_policy_service inet:127.0.0.1:7777 permit_mynetworks permit_sasl_authenticated reject_unauth_destination check_policy_service inet:127.0.0.1:12340
smtpd_reject_unlisted_recipient = yes
smtpd_reject_unlisted_sender = yes
smtpd_sasl_path = private/dovecot-auth
smtpd_sasl_type = dovecot
smtpd_sender_login_maps = proxy:mysql:/etc/postfix/mysql/sender_login_maps.cf
smtpd_sender_restrictions = reject_non_fqdn_sender reject_unlisted_sender permit_mynetworks permit_sasl_authenticated check_sender_access pcre:/etc/postfix/sender_access.pcre reject_unknown_sender_domain
smtpd_tls_CAfile = /etc/ssl/certs/iRedMail.crt
smtpd_tls_CApath = /etc/ssl/certs
smtpd_tls_cert_file = /etc/ssl/certs/iRedMail.crt
smtpd_tls_dh1024_param_file = /etc/ssl/dh2048_param.pem
smtpd_tls_dh512_param_file = /etc/ssl/dh512_param.pem
smtpd_tls_exclude_ciphers = aNULL, eNULL, EXPORT, DES, RC4, MD5, PSK, aECDH, EDH-DSS-DES-CBC3-SHA, EDH-RSA-DES-CDC3-SHA, KRB5-DE5, CBC3-SHA
smtpd_tls_key_file = /etc/ssl/private/iRedMail.key
smtpd_tls_loglevel = 1
smtpd_tls_mandatory_protocols = !SSLv2 !SSLv3
smtpd_tls_protocols = !SSLv2 !SSLv3
smtpd_tls_security_level = may
swap_bangpath = no
tls_random_source = dev:/dev/urandom

transport_maps = hash:/etc/postfix/transport proxy:mysql:/etc/postfix/mysql/transport_maps_user.cf proxy:mysql:/etc/postfix/mysql/transport_maps_maillist.cf proxy:mysql:/etc/postfix/mysql/transport_maps_domain.cf

unknown_local_recipient_reject_code = 550
virtual_alias_domains =
virtual_alias_maps = proxy:mysql:/etc/postfix/mysql/virtual_alias_maps.cf proxy:mysql:/etc/postfix/mysql/domain_alias_maps.cf proxy:mysql:/etc/postfix/mysql/catchall_maps.cf proxy:mysql:/etc/postfix/mysql/domain_alias_catchall_maps.cf

virtual_gid_maps = static:2000
virtual_mailbox_base = /var/vmail
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql/virtual_mailbox_domains.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql/virtual_mailbox_maps.cf
virtual_minimum_uid = 2000
virtual_transport = dovecot
virtual_uid_maps = static:2000
#sender_canonical_maps = hash:/etc/postfix/sender_canonical_maps
#smtp_generic_maps = hash:/etc/postfix/generic

etc/postfix/master.cf:

smtp      inet  n       -       y       -       1       postscreen
smtpd     pass  -       -       y       -       -       smtpd
dnsblog   unix  -       -       y       -       0       dnsblog
tlsproxy  unix  -       -       y       -       0       tlsproxy
pickup     unix  n       -       n       60      1       pickup
cleanup    unix  n       -       n       -       0       cleanup
qmgr       unix  n       -       n       300     1       qmgr
tlsmgr     unix  -       -       n       1000?   1       tlsmgr
rewrite    unix  -       -       n       -       -       trivial-rewrite
bounce     unix  -       -       n       -       0       bounce
defer      unix  -       -       n       -       0       bounce
trace      unix  -       -       n       -       0       bounce
verify     unix  -       -       n       -       1       verify
flush      unix  n       -       n       1000?   0       flush
proxymap   unix  -       -       n       -       -       proxymap
proxywrite unix  -       -       n       -       1       proxymap
smtp       unix  -       -       n       -       -       smtp
relay      unix  -       -       n       -       -       smtp
    -o syslog_name=postfix/$service_name
showq      unix  n       -       n       -       -       showq
error      unix  -       -       n       -       -       error
retry      unix  -       -       n       -       -       error
discard    unix  -       -       n       -       -       discard

local      unix  -       n       n       -       -       local
    -o receive_override_options=no_address_mappings

virtual    unix  -       n       n       -       -       virtual
lmtp       unix  -       -       n       -       -       lmtp
anvil      unix  -       -       n       -       1       anvil
scache     unix  -       -       n       -       1       scache
postlog    unix-dgram n  -       n       -       1       postlogd
maildrop   unix  -       n       n       -       -       pipe flags=DRhu
    user=vmail argv=/usr/bin/maildrop -d ${recipient}
uucp       unix  -       n       n       -       -       pipe flags=Fqhu
    user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
ifmail     unix  -       n       n       -       -       pipe flags=F user=ftn
    argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
bsmtp      unix  -       n       n       -       -       pipe flags=Fq.
    user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient
scalemail-backend unix - n       n       -       2       pipe flags=R
    user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop}
    ${user} ${extension}
mailman    unix  -       n       n       -       -       pipe flags=FR
    user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py ${nexthop}
    ${user}
submission inet n       -       n       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o content_filter=smtp-amavis:[127.0.0.1]:10026

dovecot unix    -       n       n       -       -      pipe
    flags=DRh user=vmail:vmail argv=/usr/lib/dovecot/deliver -f ${sender} -d ${user}@${domain} -m ${extension}
    -o receive_override_options=no_address_mappings

mlmmj   unix  -       n       n       -       -       pipe
    flags=ORhu user=mlmmj:mlmmj argv=/usr/bin/mlmmj-amime-receive -L /var/vmail/mlmmj/${nexthop}
smtp-amavis unix -  -   n   -   1  smtp
    -o syslog_name=postfix/amavis
    -o smtp_data_done_timeout=1200
    -o smtp_send_xforward_command=yes
    -o disable_dns_lookups=yes
    -o max_use=20
127.0.0.1:10025 inet n  -   n   -   -  smtpd
    -o syslog_name=postfix/10025
    -o content_filter=
    -o mynetworks_style=host
    -o mynetworks=127.0.0.0/8
    -o local_recipient_maps=
    -o relay_recipient_maps=
    -o strict_rfc821_envelopes=yes
    -o smtp_tls_security_level=none
    -o smtpd_tls_security_level=none
    -o smtpd_restriction_classes=
    -o smtpd_delay_reject=no
    -o smtpd_client_restrictions=permit_mynetworks,reject
    -o smtpd_helo_restrictions=
    -o smtpd_sender_restrictions=
    -o smtpd_recipient_restrictions=permit_mynetworks,reject
    -o smtpd_end_of_data_restrictions=
    -o smtpd_error_sleep_time=0
    -o smtpd_soft_error_limit=1001
    -o smtpd_hard_error_limit=1000
    -o smtpd_client_connection_count_limit=0
    -o smtpd_client_connection_rate_limit=0
    -o receive_override_options=no_header_body_checks,no_unknown_recipient_checks,no_address_mappings
127.0.0.1:10028 inet n  -   n   -   -  smtpd
    -o syslog_name=postfix/10028
    -o content_filter=
    -o mynetworks_style=host
    -o mynetworks=127.0.0.0/8
    -o local_recipient_maps=
    -o relay_recipient_maps=
    -o strict_rfc821_envelopes=yes
    -o smtp_tls_security_level=none
    -o smtpd_tls_security_level=none
    -o smtpd_restriction_classes=
    -o smtpd_delay_reject=no
    -o smtpd_client_restrictions=permit_mynetworks,reject
    -o smtpd_helo_restrictions=
    -o smtpd_sender_restrictions=
    -o smtpd_recipient_restrictions=permit_mynetworks,reject
    -o smtpd_end_of_data_restrictions=
    -o smtpd_error_sleep_time=0
    -o smtpd_soft_error_limit=1001
    -o smtpd_hard_error_limit=1000
    -o smtpd_client_connection_count_limit=0
    -o smtpd_client_connection_rate_limit=0
    -o receive_override_options=no_header_body_checks,no_unknown_recipient_checks
465     inet  n       -       n       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o content_filter=smtp-amavis:[127.0.0.1]:10026

/etc/postfix/sasl_password:

[192.168.0.5]:25 user:password

/etc/postfix/transport:

mydomain.by dovecot:
* relay:[192.168.0.5]:25

mail.log after restart:

Jul 16 15:07:35 mail postfix/postfix-script[364]: stopping the Postfix mail system
Jul 16 15:07:35 mail postfix/master[308]: terminating on signal 15
Jul 16 15:07:44 mail postfix/postfix-script[1202]: warning: symlink leaves directory: /etc/postfix/./makedefs.out
Jul 16 15:07:45 mail postfix/postfix-script[1401]: starting the Postfix mail system
Jul 16 15:07:45 mail postfix/master[1403]: daemon started -- version 3.4.14, configuration /etc/postfix
Jul 16 15:07:46 mail postfix/qmgr[1405]: warning: bounce_queue_lifetime is larger than maximal_queue_lifetime - adjusting bounce_queue_l$

With this setup everything works fine, local delivery from mail1@mydomail.by to mail2@mydomail.by performed correctly. Email delivery to external mailboxes is not performed because the external smtp server doesn't accept an unknown recipient, it's ok.

One setting that needs to be configured is rewriting the sender's address.

If i send an email from mail1@mydomail.by to mail2@mydomail.by using Generic mapping.
main.cf:

smtp_generic_maps = hash:/etc/postfix/generic

/etc/postfix/generic:

@mydomain.by externaladdress@externaldomain

The email is delivered to externaladdress@externaldomain with FROM field "externaladdress@externaldomain". The sender address was rewritten (it should not be) and the email was delivered throug a relay to external account, not locally and not to mail2@mydomail.by.

mail.log:

Jul 16 15:09:38 mail postfix/submission/smtpd[2215]: connect from localhost[127.0.0.1]
Jul 16 15:09:39 mail postfix/submission/smtpd[2215]: Anonymous TLS connection established from localhost[127.0.0.1]: TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Jul 16 15:09:39 mail postfix/submission/smtpd[2215]: 4GR95v1y1Zz5xxn: client=localhost[127.0.0.1], sasl_method=LOGIN, sasl_username=mail1@mydomain.by
Jul 16 15:09:39 mail postfix/cleanup[2227]: 4GR95v1y1Zz5xxn: message-id=<0a5cba941f22cf3f4e09f49ae9bc6af6@mydomain.by>
Jul 16 15:09:39 mail postfix/qmgr[1405]: 4GR95v1y1Zz5xxn: from=<mail1@mydomain.by>, size=440, nrcpt=1 (queue active)
Jul 16 15:09:39 mail roundcube: <n4cao0m3> User mail1@mydomain.by [192.168.110.2]; Message <0a5cba941f22cf3f4e09f49ae9bc6af6@mydomain.by> for mail2@mydomain.by; 250: 2.0.0 Ok: queued as 4GR95v1y1Zz5xxn
Jul 16 15:09:39 mail postfix/submission/smtpd[2215]: disconnect from localhost[127.0.0.1] ehlo=2 starttls=1 auth=1 mail=1 rcpt=1 data=1 quit=1 commands=8
Jul 16 15:09:39 mail postfix/10025/smtpd[2231]: connect from localhost[127.0.0.1]
Jul 16 15:09:40 mail postfix/10025/smtpd[2231]: 4GR95w0C3qz5xyN: client=localhost[127.0.0.1]
Jul 16 15:09:40 mail postfix/cleanup[2227]: 4GR95w0C3qz5xyN: message-id=<0a5cba941f22cf3f4e09f49ae9bc6af6@mydomain.by>
Jul 16 15:09:40 mail postfix/qmgr[1405]: 4GR95w0C3qz5xyN: from=<externaladdress@externaldomain>, size=1627, nrcpt=1 (queue active)
Jul 16 15:09:40 mail postfix/10025/smtpd[2231]: disconnect from localhost[127.0.0.1] ehlo=1 mail=1 rcpt=1 data=1 quit=1 commands=5
Jul 16 15:09:40 mail amavis[7571]: (07571-10) Passed CLEAN {RelayedInternal}, ORIGINATING/MYNETS LOCAL [127.0.0.1]:51376 ESMTP/ESMTP <externaladdress@externaldomain> -> <externaladdress@externaldomain>, (), Queue-ID: 4GR95v1y1Zz5xxn, Message-ID: <0a5cba941f22cf3f4e09f49ae9bc6af6@mydomain.by>, mail_id: sIf953oKUTLR, b: 1B2M2Y8As, Hits: -, size: 436, queued_as: 4GR95w0C3qz5xyN, Subject: "check 11", From: <externaladdress@externaldomain>, User-Agent: Roundcube_Webmail, helo=localhost, dkim_new=dkim:mydomain.by, 637 ms
Jul 16 15:09:40 mail postfix/amavis/smtp[2228]: 4GR95v1y1Zz5xxn: to=<mail2@mydomain.by>, relay=127.0.0.1[127.0.0.1]:10026, delay=1.1, delays=0.23/0.18/0.02/0.68, dsn=2.0.0, status=sent (250 2.0.0 from MTA(smtp:[127.0.0.1]:10025): 250 2.0.0 Ok: queued as 4GR95w0C3qz5xyN)
Jul 16 15:09:40 mail postfix/qmgr[1405]: 4GR95v1y1Zz5xxn: removed
Jul 16 15:09:49 mail postfix/relay/smtp[2239]: 4GR95w0C3qz5xyN: to=<externaladdress@externaldomain>, relay=192.168.0.5[192.168.0.5]:25, delay=9.8, delays=0.08/0.17/9.4/0.09, dsn=2.0.0, status=sent (250 Ok, message saved <Message-ID: 0a5cba941f22cf3f4e09f49ae9bc6af6@mydomain.by>)
Jul 16 15:09:49 mail postfix/qmgr[1405]: 4GR95w0C3qz5xyN: removed

Emails to external accounts are delivered correctly (mail1@mydomain.by -> **********@gmail.com).
mail.log:

Jul 16 15:38:05 mail postfix/submission/smtpd[12588]: connect from localhost[127.0.0.1]
Jul 16 15:38:05 mail postfix/submission/smtpd[12588]: Anonymous TLS connection established from localhost[127.0.0.1]: TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)
Jul 16 15:38:06 mail postfix/submission/smtpd[12588]: 4GR9kk1h0Sz5xxn: client=localhost[127.0.0.1], sasl_method=LOGIN, sasl_username=mail1@mydomain.by
Jul 16 15:38:06 mail postfix/cleanup[12596]: 4GR9kk1h0Sz5xxn: message-id=<d62b9014c766d2ee867c48d828d4523b@mydomain.by>
Jul 16 15:38:06 mail postfix/qmgr[1405]: 4GR9kk1h0Sz5xxn: from=<mail1@mydomain.by>, size=440, nrcpt=1 (queue active)
Jul 16 15:38:06 mail roundcube: <n4cao0m3> User mail1@mydomain.by [192.168.110.2]; Message <d62b9014c766d2ee867c48d828d4523b@mydomain.by> for **********@gmail.com; 250: 2.0.0 Ok: queued as 4GR9kk1h0Sz5xxn
Jul 16 15:38:06 mail postfix/submission/smtpd[12588]: disconnect from localhost[127.0.0.1] ehlo=2 starttls=1 auth=1 mail=1 rcpt=1 data=1 quit=1 commands=8
Jul 16 15:38:06 mail postfix/10025/smtpd[12610]: connect from localhost[127.0.0.1]
Jul 16 15:38:07 mail postfix/10025/smtpd[12610]: 4GR9kk74BXz5yLx: client=localhost[127.0.0.1]
Jul 16 15:38:07 mail postfix/cleanup[12596]: 4GR9kk74BXz5yLx: message-id=<d62b9014c766d2ee867c48d828d4523b@mydomain.by>
Jul 16 15:38:07 mail postfix/qmgr[1405]: 4GR9kk74BXz5yLx: from=<externaladdress@externaldomainy>, size=1636, nrcpt=1 (queue active)
Jul 16 15:38:07 mail postfix/10025/smtpd[12610]: disconnect from localhost[127.0.0.1] ehlo=1 mail=1 rcpt=1 data=1 quit=1 commands=5
Jul 16 15:38:07 mail amavis[7571]: (07571-11) Passed CLEAN {RelayedInternal}, ORIGINATING/MYNETS LOCAL [127.0.0.1]:51690 ESMTP/ESMTP <externaladdress@externaldomain> -> <**********@gmail.com>, (), Queue-ID: 4GR9kk1h0Sz5xxn, Message-ID: <d62b9014c766d2ee867c48d828d4523b@mydomain.by>, mail_id: D9rZKJ4z24Ha, b: 1B2M2Y8As, Hits: -, size: 439, queued_as: 4GR9kk74BXz5yLx, Subject: "check 16", From: <externaladdress@externaldomain>, User-Agent: Roundcube_Webmail, helo=localhost, dkim_new=dkim:mydomain.by, 606 ms
Jul 16 15:38:07 mail postfix/amavis/smtp[12601]: 4GR9kk1h0Sz5xxn: to=<**********@gmail.com>, relay=127.0.0.1[127.0.0.1]:10026, delay=1.1, delays=0.27/0.19/0.02/0.65, dsn=2.0.0, status=sent (250 2.0.0 from MTA(smtp:[127.0.0.1]:10025): 250 2.0.0 Ok: queued as 4GR9kk74BXz5yLx)
Jul 16 15:38:07 mail postfix/qmgr[1405]: 4GR9kk1h0Sz5xxn: removed
Jul 16 15:38:07 mail postfix/relay/smtp[12611]: 4GR9kk74BXz5yLx: to=<**********@gmail.com>, relay=192.168.0.5[192.168.0.5]:25, delay=0.77, delays=0.07/0.17/0.38/0.15, dsn=2.0.0, status=sent (250 Ok, message saved <Message-ID: d62b9014c766d2ee867c48d828d4523b@mydomain.by>)
Jul 16 15:38:07 mail postfix/qmgr[1405]: 4GR9kk74BXz5yLx: removed

I tried another way of rewriting - canonical.

main.cf:

#smtp_generic_maps = hash:/etc/postfix/generic
sender_canonical_classes = envelope_sender, header_sender
sender_canonical_maps = hash:/etc/postfix/sender_canonical_maps
smtp_header_checks = hash:/etc/postfix/header_check

/etc/postfix/sender_canonical_maps:

@mydomain.by externaladdress@externaldomain

/etc/postfix/header_check:

/From:.*/ REPLACE From: externaladdress@externaldomain

local delivery mail1@mydomain.by -> mail2@mydomain.by is fine
mail.log:

Jul 16 16:17:22 mail postfix/cleanup[27830]: 4GRBc25xp1z5xyP: message-id=<f08e9d02ec9cd1c8372d939c602431d5@mydomail.by>
Jul 16 16:17:22 mail postfix/qmgr[27372]: 4GRBc25xp1z5xyP: from=<mail1@mydomail.by>, size=440, nrcpt=1 (queue active)
Jul 16 16:17:22 mail roundcube: <n4cao0m3> User mail1@mydomail.by [192.168.110.2]; Message <f08e9d02ec9cd1c8372d939c602431d5@mogilevti$
Jul 16 16:17:22 mail postfix/submission/smtpd[27827]: disconnect from localhost[127.0.0.1] ehlo=2 starttls=1 auth=1 mail=1 rcpt=1 data=1$
Jul 16 16:17:23 mail postfix/10025/smtpd[27842]: connect from localhost[127.0.0.1]
Jul 16 16:17:23 mail postfix/10025/smtpd[27842]: 4GRBc34cFGz5xyS: client=localhost[127.0.0.1]
Jul 16 16:17:23 mail postfix/cleanup[27830]: 4GRBc34cFGz5xyS: message-id=<f08e9d02ec9cd1c8372d939c602431d5@mydomail.by>
Jul 16 16:17:23 mail postfix/qmgr[27372]: 4GRBc34cFGz5xyS: from=<mail1@mydomail.by>, size=1637, nrcpt=1 (queue active)
Jul 16 16:17:23 mail postfix/10025/smtpd[27842]: disconnect from localhost[127.0.0.1] ehlo=1 mail=1 rcpt=1 data=1 quit=1 commands=5
Jul 16 16:17:23 mail amavis[7571]: (07571-12) Passed CLEAN {RelayedInternal}, ORIGINATING/MYNETS LOCAL [127.0.0.1]:52158 ESMTP/ESMTP <ba$
Jul 16 16:17:23 mail postfix/amavis/smtp[27833]: 4GRBc25xp1z5xyP: to=<mail2@mydomail.by>, relay=127.0.0.1[127.0.0.1]:10026, delay=1.$
Jul 16 16:17:23 mail postfix/qmgr[27372]: 4GRBc25xp1z5xyP: removed
Jul 16 16:17:24 mail postfix/pipe[27843]: 4GRBc34cFGz5xyS: to=<mail2@mydomail.by>, relay=dovecot, delay=0.48, delays=0.05/0.09/0/0.3$
Jul 16 16:17:24 mail postfix/qmgr[27372]: 4GRBc34cFGz5xyS: removed

External delivery - error "Recipient unknown":
mail.log:

Jul 16 16:22:41 mail postfix/cleanup[29793]: 4GRBk90f7lz5xxl: message-id=<a5b0d38f58b559eecf96becb97bf574a@mydomain.by>
Jul 16 16:22:41 mail postfix/qmgr[27372]: 4GRBk90f7lz5xxl: from=<mail1@mydomain.by>, size=440, nrcpt=1 (queue active)
Jul 16 16:22:41 mail roundcube: <62bhmjvg> User mail1@mydomain.by [192.168.110.2]; Message <a5b0d38f58b559eecf96becb97bf574a@mydomain.by> for externaladdress@externaldomain; 250: 2.0.0 Ok: queued as 4GRBk90f7lz5xxl
Jul 16 16:22:41 mail postfix/submission/smtpd[29790]: disconnect from localhost[127.0.0.1] ehlo=2 starttls=1 auth=1 mail=1 rcpt=1 data=1 quit=1 commands=8
Jul 16 16:22:41 mail postfix/10025/smtpd[29805]: connect from localhost[127.0.0.1]
Jul 16 16:22:41 mail postfix/10025/smtpd[29805]: 4GRBk95xyBz5xyT: client=localhost[127.0.0.1]
Jul 16 16:22:41 mail postfix/cleanup[29793]: 4GRBk95xyBz5xyT: message-id=<a5b0d38f58b559eecf96becb97bf574a@mydomain.by>
Jul 16 16:22:41 mail postfix/qmgr[27372]: 4GRBk95xyBz5xyT: from=<mail1@mydomain.by>, size=1637, nrcpt=1 (queue active)
Jul 16 16:22:41 mail postfix/10025/smtpd[29805]: disconnect from localhost[127.0.0.1] ehlo=1 mail=1 rcpt=1 data=1 quit=1 commands=5
Jul 16 16:22:42 mail amavis[7571]: (07571-13) Passed CLEAN {RelayedInternal}, ORIGINATING/MYNETS LOCAL [127.0.0.1]:52344 ESMTP/ESMTP <mail1@mydomain.by> -> <externaladdress@externaldomain>, (), Queue-ID: 4GRBk90f7lz5xxl, Message-ID: <a5b0d38f58b559eecf96becb97bf574a@mydomain.by>, mail_id: 1__Ks2zEfsPh, b: 1B2M2Y8As, Hits: -, size: 440, queued_as: 4GRBk95xyBz5xyT, Subject: "check 17", From: <mail1@mydomain.by>, User-Agent: Roundcube_Webmail, helo=localhost, dkim_new=dkim:mydomain.by, 619 ms
Jul 16 16:22:42 mail postfix/amavis/smtp[29801]: 4GRBk90f7lz5xxl: to=<externaladdress@externaldomain>, relay=127.0.0.1[127.0.0.1]:10026, delay=1.4, delays=0.27/0.24/0.02/0.87, dsn=2.0.0, status=sent (250 2.0.0 from MTA(smtp:[127.0.0.1]:10025): 250 2.0.0 Ok: queued as 4GRBk95xyBz5xyT)
Jul 16 16:22:42 mail postfix/qmgr[27372]: 4GRBk90f7lz5xxl: removed
Jul 16 16:22:51 mail postfix/relay/smtp[29806]: 4GRBk95xyBz5xyT: to=<externaladdress@externaldomain>, relay=192.168.0.5[192.168.0.5]:25, delay=9.6, delays=0.13/0.18/9.1/0.18, dsn=5.0.0, status=bounced (host 192.168.0.5[192.168.0.5] said: 550 <externaladdress@externaldomain>, Recipient unknown (in reply to RCPT TO command))
Jul 16 16:22:51 mail postfix/cleanup[29793]: 4GRBkM4jNSz5xyY: message-id=<4GRBkM4jNSz5xyY@mail.mydomain.by>
Jul 16 16:22:51 mail postfix/qmgr[27372]: 4GRBkM4jNSz5xyY: from=<>, size=3896, nrcpt=1 (queue active)
Jul 16 16:22:51 mail postfix/bounce[29877]: 4GRBk95xyBz5xyT: sender non-delivery notification: 4GRBkM4jNSz5xyY
Jul 16 16:22:51 mail postfix/qmgr[27372]: 4GRBk95xyBz5xyT: removed
Jul 16 16:22:52 mail postfix/pipe[29878]: 4GRBkM4jNSz5xyY: to=<mail1@mydomain.by>, relay=dovecot, delay=0.42, delays=0.04/0.06/0/0.32, dsn=2.0.0, status=sent (delivered via dovecot service)
Jul 16 16:22:52 mail postfix/qmgr[27372]: 4GRBkM4jNSz5xyY: removed

----

Spider Email Archiver: On-Premises, lightweight email archiving software developed by iRedMail team. Supports Amazon S3 compatible storage and custom branding.