Skip to content

Instantly share code, notes, and snippets.

@parterburn
Last active December 18, 2022 18:34
Show Gist options
  • Save parterburn/6f2332a815f6616131e6d74254c40012 to your computer and use it in GitHub Desktop.
Save parterburn/6f2332a815f6616131e6d74254c40012 to your computer and use it in GitHub Desktop.
An example of how to resend messages using the Mailgun API. Per the documentation at https://documentation.mailgun.com/en/latest/api-sending.html#examples. Find your Mailgun Private API Key: https://app.mailgun.com/app/account/security/api_keys
mailgun_private_api_key = "key-TKTK"
urls = %w[
https://se.api.mailgun.net/v3/domains/{{YOUR_DOMAIN_NAME}}/messages/{{MESSAGE_STORAGE_KEY}}?to=foo@bar.com
https://storage-us-west1.api.mailgun.net/v3/domains/{{YOUR_DOMAIN_NAME}}/messages/{{MESSAGE_STORAGE_KEY}}?to=foo@bar.com
]
urls.each do |url|
uri = URI.parse(url)
params = Hash[URI.decode_www_form(uri.query)]
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
request.basic_auth("api", mailgun_private_api_key)
request.add_field('Content-Type', 'application/json')
request.body = params.to_json
response = http.request(request)
puts "#{JSON.parse(response.body).dig('message')} // #{url}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment