Postfix does not natively modify the Return-Path header field within the email body; this header is strictly determined by the receiving Mail Transfer Agent based on the SMTP envelope sender. However, you can ensure consistency between your visible return address and the envelope sender by configuring smtp_generic_maps to rewrite the MAIL FROM address before transmission.
The Distinction Between Envelope Sender and Return-Path Header
It is critical to understand that the SMTP protocol operates with two distinct addresses: the envelope sender and the header sender. The envelope sender is used during theMAIL FROM command for delivery routing and bounce handling. The receiving server then inserts this address into the Return-Path header of the delivered message. Postfix cannot alter the Return-Path text inside the MIME headers because that field is injected by the destination server, not the source. Therefore, the goal is to make the envelope sender match the desired return address so that the receiving MTA writes the correct value.
For multi-domain setups, this often requires dynamic rewriting based on the originating domain or user account. If you are looking to replace a generic sender address with a specific value across your infrastructure, reviewing our guide on Using Postfix: How to Replace Sender Address with a Specific Value provides additional context on static mapping strategies.
Configuring smtp_generic_maps for Dynamic Rewriting
The most robust method for handling multiple domains is usingsmtp_generic_maps with regular expressions. This allows you to map different local parts or domains to a single canonical return address, or map each domain to its own specific return path. You must create a lookup table that Postfix can query during the SMTP transaction.
First, generate the mapping file. For example, create a file named /etc/postfix/generic_sender. This file should contain mappings in the format original_address rewritten_address. If you want all emails from @domain1.com and @domain2.com to appear as coming from [email protected], your file might look like this:
@domain1.com [email protected]
@domain2.com [email protected]
[email protected] [email protected]
After creating the file, you must compile it into a hash database that Postfix can read efficiently. Run the following command in your terminal:
postmap /etc/postfix/generic_sender
This generates /etc/postfix/generic_sender.db. Ensure that the permissions on the original file allow Postfix to read it, typically owned by root but readable by the postfix user.
Updating Main Configuration
Next, you need to instruct Postfix to use this mapping file for outgoing mail. Open your main configuration file, usually located at/etc/postfix/main.cf. Add or modify the smtp_generic_maps parameter to point to your new database.
smtp_generic_maps = hash:/etc/postfix/generic_sender
If you previously had other maps configured, ensure they are listed here. For instance, if you also use sender_canonical_maps, note that smtp_generic_maps specifically targets the envelope sender (MAIL FROM), whereas sender_canonical_maps affects the From: header in the message body. Using both can lead to inconsistencies if not managed carefully.
Once you have saved the changes to main.cf, reload the Postfix service to apply the new settings without dropping active connections:
systemctl reload postfix
Verifying the Configuration
To verify that the envelope sender is being rewritten correctly, you can use thesendmail command-line utility or a test script to send an email to an external address. Check the headers of the received email. The Return-Path: header should now reflect the address defined in your generic_sender map.
You can also debug the rewriting process directly using Postfix's built-in tools. Use the postmap -q command to query your map file manually:
postmap -q "[email protected]" hash:/etc/postfix/generic_sender
This command should output the rewritten address. If it returns nothing or an error, the map file is not formatted correctly or the database is out of sync. Ensure there are no trailing spaces or invalid characters in your mapping file.
For users experiencing issues where mail appears to be delivered locally despite external agent configurations, checking your transport maps alongside generic maps is essential. Refer to Troubleshooting Postfix: Local Delivery despite External Agent Configuration if you suspect routing conflicts.
Handling Multiple Domains with Regular Expressions
For environments with many domains, maintaining a flat list ingeneric_sender becomes unmanageable. You can use regular expression maps instead. Create a file named /etc/postfix/generic_sender_regexp with regex patterns:
/@.*\.example\.com$/ [email protected]
/^user@sub\./ [email protected]
Then, configure main.cf to use the regexp type:
smtp_generic_maps = regexp:/etc/postfix/generic_sender_regexp
This approach allows for complex matching logic, such as excluding certain subdomains or applying rules based on the local part of the address. Remember to run postmap on regexp files as well, although some versions of Postfix handle them dynamically depending on configuration.
Always test thoroughly after changing these settings. Incorrect rewriting can cause authentication failures or spam filtering issues if the envelope sender does not align with your SPF records. Ensure that [email protected] is authorized to send mail on behalf of your sending IP addresses.
Frequently Asked Questions
Does smtp_generic_maps change the From: header?
No,smtp_generic_maps only modifies the SMTP envelope sender (MAIL FROM). It does not alter the From: header visible in the email client. To change the visible sender name or address, you must use header_checks or sender_canonical_maps.
Why is my Return-Path still showing the old address?
TheReturn-Path is set by the receiving server based on the envelope sender. If it is incorrect, your smtp_generic_maps may not be applied, or the receiving server may be stripping or modifying it due to security policies. Verify the envelope sender using telnet or swaks before it leaves your server.
Can I use different return paths for different users?
Yes, you can map individual user addresses to different return paths in yourgeneric_sender file. Ensure that more specific entries (individual users) are placed before general domain entries to prevent incorrect overrides.
What happens if I don't update postmap?
Postfix will fail to load the new mappings, and emails will continue to use the default envelope sender. Always runpostmap after editing any hash-based lookup tables and reload the Postfix service to apply changes.