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

Reply to a message in its existing Gmail thread

See more GMail REST API Examples

Live send. Supply the Gmail ID of a received message and use an authorized From address. Fetch its raw MIME so Chilkat.Email can parse encoded headers and recipient addresses. Send a plain-text reply to Reply-To, falling back to From. The reply goes to the original sender or reply address.

The request supplies Gmail’s threadId and matching Subject. The MIME In-Reply-To contains the original RFC Message-ID. References preserves the conversation ancestry and appends that Message-ID. Select a message with a valid Message-ID and review its recipient before running this sending example. Authorize both gmail.readonly and gmail.send, or use gmail.modify as shown below.

Authorize the application with https://www.googleapis.com/auth/gmail.modify. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.

local token = chilkat.JsonObject.new()
local success = token:load_file("gmail-token.json")
if not success then
    print("Could not read gmail-token.json from the private working directory.")
    return
end

if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
    print("An OAuth access token is required.")
    return
end

local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = token:string_of("access_token")
local resp = chilkat.HttpResponse.new()
local reply = chilkat.JsonObject.new()
local url = chilkat.StringBuilder.new()
url:append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
url:append("GMAIL_MESSAGE_ID")
url:append("?format=raw")
success = http:http_no_body("GET", url:get_as_string(), resp)
if resp.status_code == 0 then
    print("Gmail did not return an HTTP response.")
    return
end

if (not success) or ((resp.status_code < 200) or (resp.status_code >= 300)) then
    print("Gmail HTTP " .. resp.status_code)
    success = reply:load(resp.body_str)
    if success then
        print(reply:string_of("error.message"))
    end

    return
end

success = reply:load(resp.body_str)
if not success then
    print("Expected a Gmail JSON response.")
    return
end

if (reply:json_type_of("threadId") ~= 1) or (reply:string_of_equals("threadId", "", true)) then
    print("Expected threadId in the response.")
    return
end

if (reply:json_type_of("raw") ~= 1) or (reply:string_of_equals("raw", "", true)) then
    print("Expected raw in the response.")
    return
end

local threadId = reply:string_of("threadId")
local originalMime = chilkat.BinData.new()
success = reply:bytes_of("raw", "base64url", originalMime)
if not success then
    print(reply.last_error_text)
    return
end

local original = chilkat.Email.new()
success = original:set_from_mime_bd(originalMime)
if not success then
    print(original.last_error_text)
    return
end

local messageIdHeader = original:get_header_field("Message-ID")
if (not original.last_method_success) or (messageIdHeader == "") then
    print("The original message needs a Message-ID for this threaded reply.")
    return
end

local email = chilkat.Email.new()
email.from = "Sender <sender@example.com>"
-- Use the parsed Reply-To mailbox list when present, otherwise use From.
local replyAddress = original.reply_to
if replyAddress == "" then
    replyAddress = original.from
end

success = email:add_multiple_to(replyAddress)
if not success then
    print(email.last_error_text)
    return
end

if email.num_to == 0 then
    print("The original message has no usable reply address.")
    return
end

email.subject = original.subject
email.charset = "utf-8"
email.body = "Thank you for your message."
email:add_header_field("In-Reply-To", messageIdHeader)
local references = chilkat.StringBuilder.new()
local previousReferences = original:get_header_field("References")
if (original.last_method_success) and (previousReferences ~= "") then
    references:append(previousReferences)
    references:append(" ")
end

references:append(messageIdHeader)
email:add_header_field("References", references:get_as_string())
local mime = chilkat.BinData.new()
success = email:get_mime_bd(mime)
if not success then
    print(email.last_error_text)
    return
end

local request = chilkat.JsonObject.new()
success = request:update_bd("raw", "base64url", mime)
if not success then
    print(request.last_error_text)
    return
end

success = request:update_string("threadId", threadId)
if not success then
    print(request.last_error_text)
    return
end

success = http:http_json("POST", "https://gmail.googleapis.com/gmail/v1/users/me/messages/send", request, "application/json", resp)
if resp.status_code == 0 then
    print("Gmail did not return an HTTP response.")
    return
end

if (not success) or ((resp.status_code < 200) or (resp.status_code >= 300)) then
    print("Gmail HTTP " .. resp.status_code)
    success = reply:load(resp.body_str)
    if success then
        print(reply:string_of("error.message"))
    end

    return
end

success = reply:load(resp.body_str)
if not success then
    print("Expected a Gmail JSON response.")
    return
end

if (reply:json_type_of("id") ~= 1) or (reply:string_of_equals("id", "", true)) then
    print("Expected id in the response.")
    return
end

print("Sent reply ID: " .. reply:string_of("id"))
print("Thread ID: " .. reply:string_of("threadId"))