Add http API call routing to Policy Chapter.

This commit is contained in:
Mike Hillyer
2023-05-08 18:23:15 -04:00
parent 9bc92b9a73
commit 217ca5be4e
3 changed files with 57 additions and 1 deletions
+1
View File
@@ -208,6 +208,7 @@ TOC = [
"Advanced Tenant to Pool Mapping",
"userguide/policy/tenantpool.md",
),
Page("Routing Messages via HTTP Request", "userguide/policy/http.md"),
],
),
],
+55
View File
@@ -0,0 +1,55 @@
# Routing Messages Via HTTP Request
Some sending environments use a mixture of different services to send messages, and while it's possible to relay messages through many services using SMTP, some services are only/better served via an HTTP API.
The following example shows how to send a queued message via custom lua, in this case assembling and API call and sending it to a third-part SMTP API relay provider.
```lua
kumo.on('make.mailgun', function(domain, tenant, campaign)
local client = kumo.http.build_client {}
local sender = { }
function sender:send(message)
local request = client:post 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages.mime'
request:basic_auth('api', 'YOUR_API_KEY')
request:form_multipart_data {
to = message:recipient(),
message = message:get_data()
}
-- Make the request
local response = request:send()
-- and handle the result
local disposition = string.format(
'%d %s %s',
response:status_code(),
response:status_reason(),
response:text()
)
if response:status_is_success() then
-- Success!
return disposition
end
-- Failed!
kumo.reject(400, disposition)
end
return sender
end)
kumo.on('get_queue_config', function(domain, tenant, campaign)
if tenant == 'mailgun-user' then
return kumo.make_queue_config {
protocol = {
custom_lua = {
constructor = 'make.mailgun',
},
},
}
end
return kumo.make_queue_config {}
end)
```
+1 -1
View File
@@ -6,7 +6,7 @@ If you need to "smarthost" or route messages through another server, you can do
msg:set_meta('queue', 'imap.server.hostname')
```
This should me located in a 'smtp_server_message_received' function like this:
This should be located in a 'smtp_server_message_received' function like this:
```lua
kumo.on('smtp_server_message_received', function(msg)