Lua Requires Chilkat v11.0.0+
Lua
Insert Email into GMail Folder
See more GMail REST API Examples
Directly inserts a message into only this user's mailbox similar to IMAP APPEND, bypassing most scanning and classification. Does not send a message.This example will create a Chilkat Email object and upload/insert it into GMail. Then adds the "INBOX" label to the email to make it appear in INBOX
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local email = chilkat.Email.new()
-- This example chooses to load an email from a file.
local success = email:load_eml("qa_data/eml/testEmail.eml")
if not success then
print(email.last_error_text)
return
end
-- Get the email as MIME.
local sbMime = chilkat.StringBuilder.new()
email:get_mime_sb(sbMime)
local http = chilkat.Http.new()
local resp = chilkat.HttpResponse.new()
http.auth_token = "GMAIL-ACCESS-TOKEN"
-- Upload to GMail (does not send, but just inserts).
-- IMPORTANT: After uploading/inserting, you won't see the message in the Inbox.
-- In your web browser, if you go to "All Mail", you will see it.
local url = "https://www.googleapis.com/upload/gmail/v1/users/me/messages?uploadType=media"
http.uncommon_options = "SendGzipped"
success = http:http_sb("POST", url, sbMime, "utf-8", "message/rfc822", resp)
if not success then
print(http.last_error_text)
return
end
http.uncommon_options = ""
-- A status code of 200 indicates success.
print("Response status code: " .. resp.status_code)
print("Response body:")
print(resp.body_str)
-- The response body contains JSON.
-- A sample successful JSON response:
-- {
-- "id": "166f583051d36144",
-- "threadId": "166f5529e079a456"
-- }
local json = chilkat.JsonObject.new()
json:load(resp.body_str)
local id = json:string_of("id")
-- Add the Inbox label to the inserted email so that it appears in Inbox.
print("---- Adding the Inbox label to the newly inserted email. ----")
-- Create the following JSON to be sent in a POST to modify labels for this message.
-- {
-- "addLabelIds": [
-- "INBOX"
-- ]
-- }
local json2 = chilkat.JsonObject.new()
json2:update_string("addLabelIds[0]", "INBOX")
http:set_url_var("id", id)
url = "https://www.googleapis.com/gmail/v1/users/me/messages/{$id}/modify"
success = http:http_json("POST", url, json2, "application/json", resp)
if not success then
print(http.last_error_text)
return
end
-- A status code of 200 indicates success.
print("Response status code: " .. resp.status_code)
print("Response body:")
print(resp.body_str)
-- The response body contains JSON.
-- A sample successful JSON response:
-- {
-- "id": "166f583051d36144",
-- "threadId": "166f583051d36144",
-- "labelIds": [
-- "INBOX"
-- ]
-- }