This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

LM as SMTP-Server
#2
Run as a resident script with 0 sleep time. Received messages and headers will be visible in LM Logs tab.
Code:
local server = require('socket').tcp()

server:bind('*', 25)
server:settimeout(60)
server:listen()

local function handleclient(client)
  local init = {}
  local headers = {}
  local message = {}
  local state = 'helo'

  client:settimeout(3)
  client:send('220 localhost\r\n')

  local function sendok()
    client:send('250 ok\r\n')
  end

  while true do
    local line = client:receive()
    -- log(line)
    if not line then
      break
    end

    if state == 'helo' then
      local head = line:match('^(%w+)%s+')

      if head == 'HELO' or head == 'EHLO' then
        sendok()
        state = 'init'
      else
        break
      end
    elseif state == 'init' then
      if line == 'DATA' then
        state = 'headers'
        client:send('354 continue\r\n')
      else
        if line ~= '' then
          init[ #init + 1 ] = line
        end

        sendok()
      end
    elseif state == 'headers' then
      if line == '' then
        state = 'message'
      else
        local header, value = line:match('^([^:]+):%s+(.+)$')

        if header and value then
          headers[ header:lower() ] = value
        end
      end
    elseif state == 'message' then
      if line == '.' then
        state = 'quit'
        sendok()
      elseif line ~= '' then
        message[ #message + 1 ] = line
      end
    elseif state == 'quit' then
      if line == 'QUIT' then
        state = 'done'
        client:send('221 bye\r\n')
      end

      break
    end
  end

  if state == 'done' then
    log(init)
    log(headers)
    log(message)
  end

  client:close()
end

while true do
  local client = server:accept()
  if client then
    handleclient(client)
  end
end
Reply


Messages In This Thread
LM as SMTP-Server - by xxyzz - 19.06.2025, 14:37
RE: LM as SMTP-Server - by admin - 21.06.2025, 06:08

Forum Jump: