Skip to content

Instantly share code, notes, and snippets.

@derrickreimer
Created April 15, 2013 15:23
Show Gist options
  • Save derrickreimer/5388902 to your computer and use it in GitHub Desktop.
Save derrickreimer/5388902 to your computer and use it in GitHub Desktop.
Custom variables for Mailgun
# Attempt 1:
# Send variables using v:[my variable]
api_key = "key-zxy" # my key
api_url = "https://api.mailgun.net/v2/drip.mailgun.org"
# Using the Faraday gem
connection = Faraday.new(:url => api_url) do |builder|
builder.request :url_encoded
builder.response :json, :content_type => /\bjson$/
builder.adapter Faraday.default_adapter
end
connection.basic_auth("api", api_key)
data = {}
data["from"] = "Sender <sender@example.com>"
data["to"] = "recipient@example.com"
data["subject"] = "Subject"
data["text"] = "Text body"
data["html"] = "HTML body"
data["o:tracking"] = true
data["o:tracking-clicks"] = true
data["o:tracking-opens"] = true
# These are my custom variables I need to send with messages
data["v:delivery_id"] = 123
data["v:account_id"] = 456
connection.post do |req|
req.url "messages"
req.params = data
end
# The message gets sent, but custom variables do not appear in the webhook
# Attempt 2:
# Serialize variables into one param
api_key = "key-zxy" # my key
api_url = "https://api.mailgun.net/v2/drip.mailgun.org"
# Using the Faraday gem
connection = Faraday.new(:url => api_url) do |builder|
builder.request :url_encoded
builder.response :json, :content_type => /\bjson$/
builder.adapter Faraday.default_adapter
end
connection.basic_auth("api", api_key)
data = {}
data["from"] = "Sender <sender@example.com>"
data["to"] = "recipient@example.com"
data["subject"] = "Subject"
data["text"] = "Text body"
data["html"] = "HTML body"
data["o:tracking"] = true
data["o:tracking-clicks"] = true
data["o:tracking-opens"] = true
# These are my custom variables I need to send with messages
data["v:my-vars"] = MultiJson.dump({
"account_id" => 123,
"delivery_id" => 456
})
connection.post do |req|
req.url "messages"
req.params = data
end
# The message gets sent, but custom variables do not appear in the webhook
# Attempt 3:
# Set serialized variables as a header
api_key = "key-zxy" # my key
api_url = "https://api.mailgun.net/v2/drip.mailgun.org"
# Using the Faraday gem
connection = Faraday.new(:url => api_url) do |builder|
builder.request :url_encoded
builder.response :json, :content_type => /\bjson$/
builder.adapter Faraday.default_adapter
end
connection.basic_auth("api", api_key)
data = {}
data["from"] = "Sender <sender@example.com>"
data["to"] = "recipient@example.com"
data["subject"] = "Subject"
data["text"] = "Text body"
data["html"] = "HTML body"
data["o:tracking"] = true
data["o:tracking-clicks"] = true
data["o:tracking-opens"] = true
# These are my custom variables I need to send with messages
data["h:X-Mailgun-Variables"] = MultiJson.dump({
"account_id" => 123,
"delivery_id" => 456
})
connection.post do |req|
req.url "messages"
req.params = data
end
# The message gets sent, but custom variables do not appear in the webhook
@SpareShade
Copy link

it doesn't seem to be possible...
... at least not so far ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment