Webhook / POST-Request on new mail

I have some emails that should send some info or notification to a webhook, when new email arrives.

Is there any way to achive that? This should work for specific inboxes as well as in catchall inboxes.

I already tried to follow the Push Notification Framework tutorial in the Dovecot documentation and modified /etc/dovecot/conf.d/90-plugin.conf:

mail_plugins = $mail_plugins mail_lua notify push_notification push_notification_lua

plugin {
  #setting_name = value
  push_notification_driver = lua:file=/etc/dovecot/dovecot-push.lua
  push_notification_driver2 = ox:url=https://www.example.com/webhook-endpoint
  #push_notification_driver3 = dlog
  push_lua_url = https://www.example.com/webhook-endpoint
}

So I tried both lua and ox.

The lua files content is:

-- To use
--
-- plugin {
--  push_notification_driver = lua:file=/etc/dovecot/dovecot-push.lua
--  push_lua_url = https://Bearer:<push-token>@<egroupware-domain>/egroupware/push
-- }
--
-- server is sent a PUT message with JSON body like push_notification_driver = ox:url=<push_lua_url> user_from_metadata
-- plus additionally the events MessageAppend, MessageExpunge, FlagsSet and FlagsClear
-- MessageTrash and MessageRead are ignored, so are empty or NonJunk FlagSet/Clear from TB
--
-- Needs lua-socket and lua-json packages plus dovecot-lua!
--

local f = io.open("/var/mail/webhook.log", "a")
f:write("Called " .. os.date('%Y-%m-%d %H:%M:%S') .. "\n")
f:flush()
f:close()

local http = require "socket.http"
local ltn12 = require "ltn12"
local json = require "json"

function table_get(t, k, d)
  return t[k] or d
end

function dovecot_lua_notify_begin_txn(user)
    local meta = user:metadata_get("/private/vendor/vendor.dovecot/http-notify")
    if (meta == nil or meta:sub(1,5) ~= "user=")
    then
        meta = nil;
    else
        meta = meta:sub(6)
    end
    return {user=user, event=dovecot.event(), ep=user:plugin_getenv("push_lua_url"), messages={}, meta=meta}
end

function dovecot_lua_notify_event_message_new(ctx, event)
    -- check if there is a push token registered
--    if (ctx.meta == nil) then
--        return
--    end
    -- get mailbox status
    local mbox = ctx.user:mailbox(event.mailbox)
    mbox:sync()
    local status = mbox:status(dovecot.storage.STATUS_RECENT, dovecot.storage.STATUS_UNSEEN, dovecot.storage.STATUS_MESSAGES)
    mbox:free()
    table.insert(ctx.messages, {
        user = ctx.meta,
        ["imap-uidvalidity"] = event.uid_validity,
        ["imap-uid"] = event.uid,
        folder = event.mailbox,
        event = event.name,
        from = event.from,
        subject = event.subject,
        snippet = event.snippet,
        unseen = status.unseen,
        messages = status.messages
    })
end

function dovecot_lua_notify_event_message_append(ctx, event)
  dovecot_lua_notify_event_message_new(ctx, event)
end

function dovecot_lua_notify_end_txn(ctx)
    -- report all states
    for i,msg in ipairs(ctx.messages) do
        local e = dovecot.event(ctx.event)
        e:set_name("lua_notify_mail_finished")
        reqbody = json.encode(msg)
        e:log_debug(ctx.ep .. " - sending " .. reqbody)
        res, code = http.request({
            method = "POST",
            url = ctx.ep,
            source = ltn12.source.string(reqbody),
            headers={
                ["content-type"] = "application/json; charset=utf-8",
                ["content-length"] = tostring(#reqbody)
            }
        })
        e:add_int("result_code", code)
        e:log_info("Mail notify status " .. tostring(code))
    end
end

So I added something to write to a log file as well to see if the script at least is called. But nothing happened.

Only when I trigger it manually by calling doveadm mailbox metadata set -u user@example.com -s "" /private/vendor/vendor.dovecot/http-notify user=11@3 I see a line added in the log file (don’t know what this really does).

For that I also actived metadata by modifying /etc/dovecot/conf.d/99-metadata.conf:

# Store METADATA information within user's Maildir directory
mail_attribute_dict = file:%h/Maildir/dovecot-attributes
protocol imap {
    imap_metadata = yes
}

The lua script itself should also be callable and the right group:

root@box:/etc/dovecot# ll dovecot-push.lua
-rw-rwx--- 1 mail dovecot 6969 Jan 20 11:15 dovecot-push.lua*

Even after restarting dovecot, that a new email did neither trigger the ox, nor the lua notification.

What am I missing?

I would prefer a push notification rather than pulling the imap inbox that every minute.