Class: Mailgun::DeliveryMethod
- Inherits:
-
Object
- Object
- Mailgun::DeliveryMethod
- Defined in:
- lib/mailgun-rails.rb
Instance Attribute Summary collapse
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
- #api_host ⇒ Object
- #api_key ⇒ Object
- #deliver!(mail) ⇒ Object
-
#initialize(settings) ⇒ DeliveryMethod
constructor
A new instance of DeliveryMethod.
Constructor Details
#initialize(settings) ⇒ DeliveryMethod
Returns a new instance of DeliveryMethod.
14 15 16 |
# File 'lib/mailgun-rails.rb', line 14 def initialize(settings) self.settings = settings end |
Instance Attribute Details
#settings ⇒ Object
Returns the value of attribute settings.
12 13 14 |
# File 'lib/mailgun-rails.rb', line 12 def settings @settings end |
Instance Method Details
#api_host ⇒ Object
18 19 20 |
# File 'lib/mailgun-rails.rb', line 18 def api_host self.settings[:api_host] end |
#api_key ⇒ Object
22 23 24 |
# File 'lib/mailgun-rails.rb', line 22 def api_key self.settings[:api_key] end |
#deliver!(mail) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mailgun-rails.rb', line 26 def deliver!(mail) body = Curl::PostField.content("message", mail.encoded) body.remote_file = "message" body.content_type = "application/octet-stream" data = [] data << body mail.destinations.each do |destination| data << Curl::PostField.content("to", destination) end curl = Curl::Easy.new("https://api:#{self.api_key}@api.mailgun.net/v2/#{self.api_host}/messages.mime") curl.multipart_form_post = true curl.http_post(*data) if curl.response_code != 200 begin error = ActiveSupport::JSON.decode(curl.body_str)["message"] rescue error = "Unknown Mailgun Error" end raise Mailgun::DeliveryError.new(error) end end |