Class: Snails::Mailer::MailgunBackend

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

Defined Under Namespace

Classes: Attachment

Instance Method Summary collapse

Methods inherited from Backend

#deliver_many

Constructor Details

#initialize(options = {}) ⇒ MailgunBackend

Returns a new instance of MailgunBackend.



212
213
214
215
216
# File 'lib/snails/mailer.rb', line 212

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

Instance Method Details

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



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/snails/mailer.rb', line 218

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

  if email[:attachments]
    data[:attachment] = email[:attachments].map do |att|
      Attachment.new(att[:content], att[:filename], att[:content_type])
    end
  end

  resp = RestClient.post(@url, data)
  return resp.code == 200 ? [resp.body, data[:to]] : nil
end