Created
April 15, 2013 15:23
-
-
Save derrickreimer/5388902 to your computer and use it in GitHub Desktop.
Custom variables for Mailgun
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 <[email protected]>" | |
data["to"] = "[email protected]" | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 <[email protected]>" | |
data["to"] = "[email protected]" | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 <[email protected]>" | |
data["to"] = "[email protected]" | |
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 |
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
hey! How did you parse the variables in the template? I'm stuck here too