-
-
Save mwpastore/2d0231bcd39df4b8932b745f7d24fc31 to your computer and use it in GitHub Desktop.
Mailgun delivery method for Ruby Mail gem
This file contains 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
MAILGUN_DOMAIN=sandboxwhatevs.mailgun.org | |
MAILGUN_API_KEY=key-whatevs |
This file contains 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
gem 'mail' | |
gem 'mailgun', require: false |
This file contains 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
# frozen_string_literal: true | |
require 'mail/network/delivery_methods/mailgun' # or wherever you put mailgun.rb | |
Mail.defaults do | |
delivery_method Mail::Mailgun, | |
domain: ENV.fetch('MAILGUN_DOMAIN'), | |
api_key: ENV.fetch('MAILGUN_API_KEY') | |
end |
This file contains 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
# frozen_string_literal: true | |
require 'mail/check_delivery_params' | |
require 'mailgun' | |
module Mail | |
class Mailgun | |
include Mail::CheckDeliveryParams | |
def initialize(values) | |
self.settings = { :api_key => nil, :domain => nil }.merge!(values) | |
end | |
attr_accessor :settings | |
def deliver!(mail) | |
message = check_delivery_params(mail)[-1] | |
client = ::Mailgun::Client.new settings[:api_key] | |
result = client.send_message settings[:domain], | |
to: mail.destinations, | |
message: message | |
settings[:return_response] ? result : self | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment