Struggle with qeueu.toml and setting a tenant based connected port

Use Case:
A client wants me to forward certain mail domains via an authenticated SMTP to get improved delivery of the mails via certain whitelisted IPs.
So I thought I create a separate port on the kumomta server and then use the received_via meta info to set the tenant. So this client is only using that tenant, which could mean I can configure alternate routing_domains for this tenant only. But the routing_domain config is never set using the queue.toml. I know you guys want full configs, btu I will try to debug this a bit more. But I also want to check if I am doing something conceptually wrong.

And to be honest. Using lua only in the init.lua I did make it work based on the received_via meta, but I figured that the helper setup should be able to do the same and it failed.
init.lua section for queue helper apply:

kumo.on('smtp_server_message_received', function(msg)

local my_received_via = msg:get_meta('received_via')
  if my_received_via == "10.30.30.213:2634" then
     msg:set_meta('tenant', 'trnsp')
  end
  queue_helper:apply(msg)

If I then try to set the routing_domain in the queue.toml it doesn’t work as the queue.lua shows me while debugging with print statements that the message has the default-tenant and not the trnsp tenant.
In the queue.toml I configured this. The trnsp tenant does have a working source pool using haproxy.

[tenant.'trnsp']
egress_pool = 'pool-trnsp'

[queue.'domain.com'.'trnsp']
routing_domain = 'smtp-relay.domain.com'
max_age=500

In the shaping.toml I added the smtp auth on smtp-relay.domain.com.

Weirdly enough if I set the tenant using the same lua code AFTER the
queue_helper:apply(msg) and NOT before, then the message actually seems to have the tenant setting when sending the mail.
The resolv_config print info:

resolve_config#011domain.com#011trnsp#011nil

But still in the queue.lua code I don’t see the ‘trnsp’ tenant at all if I use

msg:get_meta 'tenant'

I am trying to debug and see what is happening here. But I am fairly confused. My guess is for that since the meta data on tenant is maybe cached and still default-tenant it will not use the setting from the queue.toml correctly.

Am I missing something conceptually? I mean my setup works if I use the following code in init.lua and not only set the tenant meta but also the routing_domain.

  if my_received_via == "10.30.30.213:2634" then
     msg:set_meta('tenant', 'trnsp')
     if mydomain == 'domain.com' then
       msg:set_meta('routing_domain', 'smtp-relay.domain.com')
     end

And mail is then send for domain.com via the smtp-relay.domain.com via the haproxy IP. So any suggestions for better debugging are welcome

Oh and I forgot to mention:
Debian 12 vanilla build kumomta baased on the git repo (no mods at all)
ii kumomta 2025.05.06.055544.b29689af amd64

Maybe use append_header instead of msg:set_meta

msg:append_header('X-Tenant','trnsp') 
msg:set_meta('routing_domain', 'prod-cn-smtp.domain.com') 

Welll actually only setting the X-Tenenat header is enough… then the queue.lua code will pick the right tenant.

I guess that solves my problem, but I am not sure if this is intended behaviour concening setting the tenant using set_meta

There are users who combine the helper with a manual set_meta where they have a special use case not covered by the helper. As you discovered, you need to do your set_meta after the helper call or else the helper will overwrite your set_meta.

Hmm yeah, but then the routing_domain setting is not applied… that is the problem tbh. Adding the header does fix the issue.. but it doesn’t make sense that the set_meta tenant is not honored properly

I tried both ways… and the setting it after does work better, but it still does not make the mail to be send over the smtp auth where adding the header does. Doesn’t make sense to me at all. but I have a solution for the quick implementation, but I would rather manage this kind of stuff using the helper.

Oh. That is because when you set meta manually you’re bypassing the helper, which is where you have that information configured, but the helper can

can’t be set based on IP.

oh hmm that sounds a bit weird to me… I also could define in the init.lua that if I send with mydomain.com that it should be sending with the ‘mydomain’ tenant and then it doesn’t work with a set_meta… but it does work with add_header. Either way the latter is a working solution, but not how I expected it :slightly_smiling_face:

So I guess case closed kinda…

I think in this document : Configuring Queue Management - KumoMTA Docs
If add some instructions to it, let the user who reads the document notice this.

I did re-read this document and I guess it might be good to state that when using the helper tenants and campaigns must be set using headers if you want to make direct use in the queue.toml files.
I was under the impression that if you set the tenant using set_meta that it would honor that in the queue_helper:apply(msg).
So what works is:

local my_received_via = msg:get_meta('received_via')
  if my_received_via == "10.30.30.213:2634" then
     msg:append_header('X-Tenant','trnsp') 
  end
  queue_helper:apply(msg)

With that configured stuff for the tenant are executed as they should.