Skip to content

Commit 1b2600c

Browse files
feat: add message token to return path for bounce tracking
Adds the message token to the return path (MAIL FROM) to enable faster bounce message matching. When use_message_tags_for_bounces is enabled, bounces can be matched by extracting the token from the return path instead of scanning the entire message body. Format: server_token+message_token@return_path_domain Cherry-picked from postalserver/postal PR postalserver#3036 Co-authored-by: schueffi <schueffi@users.noreply.github.com> Co-authored-by: openhands <openhands@all-hands.dev>
1 parent 3cedb20 commit 1b2600c

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

app/senders/smtp_sender.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ def determine_mail_from_for_message(message)
142142
# If the domain has a valid custom return path configured, return
143143
# that.
144144
if message.domain.return_path_status == "OK"
145-
return "#{message.server.token}@#{message.domain.return_path_domain}"
145+
return "#{message.server.token}+#{message.token}@#{message.domain.return_path_domain}"
146146
end
147147

148-
"#{message.server.token}@#{Postal::Config.dns.return_path_domain}"
148+
"#{message.server.token}+#{message.token}@#{Postal::Config.dns.return_path_domain}"
149149
end
150150

151151
# Return the RCPT TO to use for the given message in this sending session

doc/config/yaml.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ postal:
2323
use_local_ns_for_domain_verification: false
2424
# Append a Resend-Sender header to all outgoing e-mails
2525
use_resent_sender_header: true
26+
# Use message tags when assigning bounces to their corresponding messages
27+
use_message_tags_for_bounces: false
2628
# The default size for new DKIM keys
2729
default_dkim_key_size: 1024
2830
# Path to the private key used for signing

lib/postal/config_schema.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ module Postal
6666
default true
6767
end
6868

69+
boolean :use_message_tags_for_bounces do
70+
description "Use message tags when assigning bounces to their corresponding messages"
71+
default false
72+
end
73+
6974
integer :default_dkim_key_size do
7075
description "The default size for new DKIM keys"
7176
default 1024

lib/postal/message_db/message.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,17 @@ def create_link(url)
511511
def original_messages
512512
return nil unless bounce
513513

514-
other_message_ids = raw_message.scan(/\X-Postal-MsgID:\s*([a-z0-9]+)/i).flatten
514+
# if this is enabled, we search first for the message tag as this is faster
515+
# than to scan the full message (which is probably many mbytes in size)
516+
if Postal::Config.postal.use_message_tags_for_bounces?
517+
uname, domain = rcpt_to.split("@", 2)
518+
uname, other_message_ids = uname.split("+", 2)
519+
end
520+
# if we did not found any messages, or this was not enabled, then fall back to
521+
# the old header based search
522+
if other_message_ids.nil? || other_message_ids.empty?
523+
other_message_ids = raw_message.scan(/\X-Postal-MsgID:\s*([a-z0-9]+)/i).flatten
524+
end
515525
if other_message_ids.empty?
516526
[]
517527
else

0 commit comments

Comments
 (0)