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

SendGrid: Send product updates with a topic unsubscribe link

See more SendGrid Examples

Use the numeric ID of your existing Product updates unsubscribe group and a verified sender. The ASM tag expands to the recipient-specific link for this group. A click opts the recipient out of this topic; a global unsubscribe applies across topics.

Unsubscribe tags ยท Mail Send

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
-- Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.auth_token = "SENDGRID_API_KEY"
local resp = chilkat.HttpResponse.new()

local message = chilkat.JsonObject.new()
message:update_string("from.email", "sender@example.com")
message:update_string("personalizations[0].to[0].email", "recipient@example.com")
message:update_int("asm.group_id", 12345)
message:update_string("subject", "Product updates: November edition")
message:update_string("content[0].type", "text/html")
message:update_string("content[0].value", "<p>The new catalog is available.</p><p><a href=\"<%asm_group_unsubscribe_raw_url%>\">Unsubscribe from product updates</a></p>")

success = http:http_json("POST", "https://api.sendgrid.com/v3/mail/send", message, "application/json", resp)
if not success then
    if resp.status_code == 0 then
        print(http.last_error_text)
    else
        print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
    end

    return
end

if resp.status_code ~= 202 then
    print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
    return
end

print("Accepted for delivery (HTTP 202).")