When building notification systems that generate dynamic email addresses (like notification-message-988742@mysite.com
), replies sent to these addresses need proper handling. Since these addresses don't have individual mailboxes, we need to redirect them to a central address (notification@mysite.com
).
Here are three effective methods to handle wildcard email redirection in Postfix:
1. Using Virtual Aliases
Edit /etc/postfix/virtual
:
# Wildcard pattern matching @notification-message-.*@mysite.com notification@mysite.com
Then run:
postmap /etc/postfix/virtual postfix reload
2. Regex Table Mapping
Create /etc/postfix/regexp
:
/^notification-message-[0-9]+@mysite\.com$/ notification@mysite.com
Add to main.cf
:
virtual_alias_maps = regexp:/etc/postfix/regexp
3. Using Transport Maps
For more complex routing, create /etc/postfix/transport
:
notification-message-*@mysite.com local:notification
Then:
postmap /etc/postfix/transport postfix reload
Verify your setup with:
postmap -q "notification-message-123@mysite.com" regexp:/etc/postfix/regexp
You should see the output:
notification@mysite.com
For a complete solution, consider these additional configurations:
# In main.cf bounce_notice_recipient = notification@mysite.com notify_classes = bounce, delay, policy, protocol, resource, software
When dealing with high volumes:
- Use hash maps instead of regex for better performance
- Consider implementing a milter for complex filtering
- Monitor your mail queue regularly
Here's a full configuration example:
# /etc/postfix/main.cf additions virtual_alias_domains = mysite.com virtual_alias_maps = hash:/etc/postfix/virtual, regexp:/etc/postfix/regexp
With /etc/postfix/virtual
containing:
@mysite.com @notification-message notification@mysite.com notification
When building notification systems that allow users to reply to messages, we often generate dynamic Reply-To
addresses containing unique IDs (like notification-message-988742@mysite.com
). These emails don't have individual mailboxes, but need to be routed to a central address (notification@mysite.com
) for processing.
Here are two effective approaches to handle wildcard email redirection in Postfix:
Method 1: Using Virtual Aliases
Edit your /etc/postfix/virtual
file:
# Wildcard pattern matching @notification-message-.*@mysite.com notification@mysite.com
Then update the virtual alias database:
sudo postmap /etc/postfix/virtual sudo systemctl reload postfix
Method 2: Using Recipient Canonical Maps
Create /etc/postfix/recipient_canonical
:
# Map all matching addresses to notification@ notification-message-.+@mysite.com notification@mysite.com
Add to main.cf
:
recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical
Reload Postfix:
sudo postmap /etc/postfix/recipient_canonical sudo systemctl reload postfix
Verify your setup works by sending a test email:
echo "Test" | mail -s "Wildcard Test" notification-message-12345@mysite.com
Then check if it arrives in the notification@mysite.com
mailbox.
If emails aren't being redirected properly:
- Check Postfix logs:
tail -f /var/log/mail.log
- Verify your regex patterns match the email format
- Ensure you've reloaded Postfix after configuration changes
For a complete solution, you'll need to parse the original message ID from the recipient address. Here's a basic Python example:
import re from email.utils import parseaddr def extract_message_id(email): header = parseaddr(email)[1] match = re.search(r'notification-message-(\d+)@', header) return match.group(1) if match else None