Lua Event: Before MAIL FROM Accepted

This event fires when Inbucket is evaluating an SMTP MAIL FROM command.

Example

This example denies mail that is not from james*@example.com:

local logger = require("logger")

function inbucket.before.mail_from_accepted(session)
	local from = session.from.address
	logger.info(string.format("Inspecting mail from %s", from),
		{ remote_ip = session.remote_addr })

	-- Only allow mail from example.com, using pattern matching.
	if not string.match(from, [[@example%.com$]]) then
		logger.warn("Rejecting blocked origin domain", { addr = from })

		-- Returning `smtp.deny()` causes the `MAIL FROM` command to be rejected.
		return smtp.deny()
	end

	-- Only allow sending users starting with `james`.
	if string.find(from, "james") ~= 1 then
		logger.warn("Rejecting blocked sender", { addr = from })

		-- Returning `smtp.deny(<code>, "message")` causes the `MAIL FROM` command
		-- to be rejected with a custom error.
		return smtp.deny(554, "We only accept mail from james")
	end

	-- Returning `smtp.defer()` instructs Inbucket to continue processing this
	-- message, respecting it's built-in logic and environment variable config.
	-- Using `smtp.allow()` would force Inbucket to accept this `MAIL FROM`.
	return smtp.defer()
end

Example SMTP session

220 inbucket Inbucket SMTP ready
HELO localhost
250 Great, let's get this show on the road
MAIL FROM:<bob@example.com>
554 We only accept mail from james

Inbucket log output

1:17PM INF Starting SMTP session module=smtp remote=127.0.0.1:51726 session=1
1:17PM INF Inspecting mail from bob@example.com module=lua remoteip=127.0.0.1
1:17PM WRN Rejecting blocked sender addr=bob@example.com module=lua
1:17PM WRN Extension denied mail from <bob@example.com> module=smtp remote=127.0.0.1:51726 session=1