Class: Hermes::MailgunProvider

Inherits:
Provider
  • Object
show all
Defined in:
lib/providers/mailgun/mailgun_provider.rb

Instance Attribute Summary

Attributes inherited from Provider

#credentials, #defaults, #deliverer, #weight

Instance Method Summary collapse

Methods inherited from Provider

#initialize, #message_failure, #message_success, #provider_name, required_credentials

Methods included from Extractors

#complex_extract, #extract_from, #extract_html, #extract_text, #extract_to

Constructor Details

This class inherits a constructor from Hermes::Provider

Instance Method Details

#clientObject



38
39
40
# File 'lib/providers/mailgun/mailgun_provider.rb', line 38

def client
  Mailgun::Client.new(self.credentials[:api_key])
end

#mailgun_message(rails_message) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/providers/mailgun/mailgun_provider.rb', line 16

def mailgun_message(rails_message)
  message = Mailgun::MessageBuilder.new

  # basics
  message.set_from_address(extract_from(rails_message))
  message.add_recipient(:to, extract_to(rails_message))
  message.set_subject(rails_message[:subject])
  message.set_html_body(extract_html(rails_message))
  message.set_text_body(extract_text(rails_message))
  message.set_message_id(rails_message.message_id)

  # optionals
  message.set_from_address('h:reply-to', rails_message[:reply_to].formatted.first) if rails_message[:reply_to]

  # and any attachments
  rails_message.attachments.try(:each) do |attachment|
    message.add_attachment(Hermes::EmailAttachment.new(attachment))
  end

  return message
end

#send_message(rails_message) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/providers/mailgun/mailgun_provider.rb', line 5

def send_message(rails_message)
  domain = rails_message.mailgun_domain || self.defaults[:domain]
  message = self.mailgun_message(rails_message)

  if self.deliverer.should_deliver?
    self.client.send_message(domain, message)
  end

  self.message_success(rails_message)
end