Class: MultiMail::Sender::Mailgun
- Inherits:
-
Object
- Object
- MultiMail::Sender::Mailgun
- Includes:
- Base
- Defined in:
- lib/multi_mail/mailgun/sender.rb
Overview
Mailgun's outgoing mail sender.
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
Attributes included from Base
Instance Method Summary collapse
-
#deliver!(mail) ⇒ Object
Delivers a message via the Mailgun API.
-
#initialize(options = {}) ⇒ Mailgun
constructor
Initializes a Mailgun outgoing email sender.
-
#parameters ⇒ Hash
Returns the additional parameters for the API call.
Constructor Details
#initialize(options = {}) ⇒ Mailgun
Initializes a Mailgun outgoing email sender.
17 18 19 20 21 |
# File 'lib/multi_mail/mailgun/sender.rb', line 17 def initialize( = {}) super @api_key = settings.delete(:api_key) @domain = settings.delete(:domain) end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/multi_mail/mailgun/sender.rb', line 9 def api_key @api_key end |
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
9 10 11 |
# File 'lib/multi_mail/mailgun/sender.rb', line 9 def domain @domain end |
Instance Method Details
#deliver!(mail) ⇒ Object
Delivers a message via the Mailgun API.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/multi_mail/mailgun/sender.rb', line 50 def deliver!(mail) = MultiMail::Message::Mailgun.new(mail).to_mailgun_hash.merge(parameters) connection = Faraday.new do |conn| conn.basic_auth 'api', api_key conn.request :multipart conn.request :url_encoded conn.adapter Faraday.default_adapter end response = connection.post("https://api.mailgun.net/v2/#{domain}/messages", ) case response.status when 401 raise InvalidAPIKey, response.body when 400, 404 body = JSON.load(response.body) case body['message'] when "'from' parameter is missing" raise MissingSender, body['message'] when "'to' parameter is missing" raise MissingRecipients, body['message'] when "Need at least one of 'text' or 'html' parameters specified" raise MissingBody, body['message'] else raise InvalidRequest, body['message'] end when 200 body = JSON.load(response.body) else raise response.body end if settings[:return_response] body else self end end |
#parameters ⇒ Hash
Returns the additional parameters for the API call.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/multi_mail/mailgun/sender.rb', line 26 def parameters parameters = settings.dup parameters.delete(:return_response) [:opens, :clicks].each do |sym| if tracking.key?(sym) parameter = :"o:tracking-#{sym}" case tracking[sym] when 'yes', 'no', 'htmlonly' parameters[parameter] = tracking[sym] when true parameters[parameter] = 'yes' when false parameters[parameter] = 'no' end # ignore nil end end parameters end |