Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

MX Lookup Mail Server Domain by Email Address

How to find the mail server for a given email address. Returns the domain name of the primary mail server.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local dns = chilkat.Dns.new()
local json = chilkat.JsonObject.new()

local emailAddr = "bob@example.com"

-- This gets all MX domains for an email address.  (Typically one domain.)
-- The preferred domain will be at index 0 (see below).
local success = dns:query("MX", emailAddr, json)
if not success then
    print(dns.last_error_text)
    return
end

local i = 0
local count_i = json:size_of_array("answer.mx")
while i < count_i do
    json.i = i
    local domain = json:string_of("answer.mx[i].domain")
    print(domain)
    i = i + 1
end