Class: Snails::Mailer::Mailgun

Inherits:
Backend
  • Object
show all
Defined in:
lib/snails/mailer.rb

Instance Method Summary collapse

Methods inherited from Backend

#deliver_many

Constructor Details

#initialize(api_key:, domain_name:) ⇒ Mailgun

Returns a new instance of Mailgun.



175
176
177
178
# File 'lib/snails/mailer.rb', line 175

def initialize(api_key:, domain_name:)
  @key = api_key
  @url = "https://api.mailgun.net/v3/#{domain_name}/messages"
end

Instance Method Details

#deliver(email, options = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/snails/mailer.rb', line 180

def deliver(email, options = {})
  raise "No body!" if email[:body].nil?

  data = {
    from: email[:from],
    to: email[:to],
    subject: email[:subject],
  }

  data[:text] = email[:body] if email[:body]
  data[:html] = email[:html_body] if email[:html_body]

  if data[:text].blank? && data[:html].blank?
    raise ArgumentError, "Either text or html required"
  end

  opts = { username: 'api', password: @key }
  resp = Dagger.post(@url, data, opts)
  resp.ok? ? [resp.data, data[:to]] : nil
end