Skip to content

Instantly share code, notes, and snippets.

@farhadhf
Last active March 15, 2025 16:15
Show Gist options
  • Select an option

  • Save farhadhf/ddfb3cfaa022503eed3db9ed0b157429 to your computer and use it in GitHub Desktop.

Select an option

Save farhadhf/ddfb3cfaa022503eed3db9ed0b157429 to your computer and use it in GitHub Desktop.
kumo.on('http_message_generated', function(msg)
local failed = msg:check_fix_conformance(
-- check for and reject messages with these issues:
'NON_CANONICAL_LINE_ENDINGS',
-- fix messages with these issues:
'NEEDS_TRANSFER_ENCODING|MISSING_DATE_HEADER|MISSING_MESSAGE_ID_HEADER|LINE_TOO_LONG'
)
if failed then
kumo.reject(552, string.format('5.6.0 %s', failed))
end
local original_sender_domain = msg:sender().domain
local tenant = aha.cached_tenant_id(original_sender_domain)
local domain = aha.cached_domain_id(original_sender_domain)
if tenant and domain then
local rejection_settings = aha.cached_tenant_rejection_settings(tenant)
if rejection_settings.bad then
aha.reject_bad_recipients(msg:recipient().domain)
end
if rejection_settings.mistyped then
aha.reject_mistyped_domain(msg:recipient().domain)
end
msg:append_header('X-CUSTOMER-ID', tenant)
msg:set_meta('source', 'http')
msg:set_meta('tenant', tenant)
msg:set_meta('customer_id', tenant)
msg:set_meta('domain_id', domain)
msg:set_meta('direction', 'outbound')
if aha.cached_domain_has_rp(domain) then
msg:set_sender(string.format('%s@psrp.%s', msg:id(), original_sender_domain))
else
msg:set_sender(string.format('%s@%s', msg:id(), original_sender_domain))
end
local egress_pool = aha.cached_domain_egress_pool(domain)
if egress_pool ~= nil and egress_pool ~= "" then
msg:set_meta("campaign", string.format("egress_%s", egress_pool))
end
else
kumo.reject(
556,
string.format("from domain '%s' is not allowed.", original_sender_domain)
)
end
local can_send = aha.cached_tenant_can_send(tenant)
if can_send == "inv" then
kumo.reject(
556,
"Invalid sender"
)
end
if can_send == "no" then
kumo.reject(
521,
"Account cannot send. Payment required."
)
end
local metadata_retention = tonumber(msg:get_first_named_header_value('ahasend-metadata-retention'))
local data_retention = tonumber(msg:get_first_named_header_value('ahasend-data-retention'))
if metadata_retention ~= nil and math.floor(metadata_retention) == metadata_retention and metadata_retention > 0 then
msg:set_meta('metadata_retention', metadata_retention)
end
if data_retention ~= nil and math.floor(data_retention) == data_retention and data_retention >= 0 then
msg:set_meta('data_retention', data_retention)
end
local tags = msg:get_first_named_header_value('ahasend-tags')
msg:set_meta("tags", tags)
if data_retention == nil or data_retention > 0 then
-- calls msg:get_data(), no change is made to the message itself.
aha.save_message(msg)
end
if aha.cached_is_suppressed(msg:recipient().email, original_sender_domain) then
msg:set_meta("campaign", "suppressed")
else
-- this is the only place where we could make changes to the message.
-- but tracking is disabled in all my tests and aha.add_tracking() returns nil.
local response = aha.add_tracking(msg)
if response ~= nil then
print('making changes to the message')
if response:status_is_success() then
local txt = response:text()
msg:set_data(txt)
end
else
-- this print statement gets called.
print('not making changes to the message, tracking is disabled.')
end
end
msg:remove_all_named_headers('ahasend-track-opens')
msg:remove_all_named_headers('ahasend-track-clicks')
msg:remove_all_named_headers('ahasend-message-retention')
msg:remove_all_named_headers('ahasend-message-data-retention')
msg:remove_all_named_headers('ahasend-tags')
queue_helper:apply(msg)
dkim_signer(msg)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment