Class: Railgun::Mailer

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

Overview

Railgun::Mailer is an ActionMailer provider for sending mail through Mailgun.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Mailer

Initialize the Railgun mailer.

Parameters:

  • config (Hash)

    Hash of config values, typically from `app_config.action_mailer.mailgun_config`


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/railgun/mailer.rb', line 19

def initialize(config)
  @config = config

  [:api_key, :domain].each do |k|
    raise Railgun::ConfigurationError.new("Config requires `#{k}` key", @config) unless @config.has_key?(k)
  end

  @mg_client = Mailgun::Client.new(config[:api_key])
  @domain = @config[:domain]

  # To avoid exception in mail gem v2.6
  @settings = { return_response: true }

  if (@config[:fake_message_send] || false)
    Rails.logger.info "NOTE: fake message sending has been enabled for mailgun-ruby!"
    @mg_client.enable_test_mode!
  end
end

Instance Attribute Details

#configObject

Hash

config ->

Requires *at least* `api_key` and `domain` keys.

14
15
16
# File 'lib/railgun/mailer.rb', line 14

def config
  @config
end

#domainObject

Hash

config ->

Requires *at least* `api_key` and `domain` keys.

14
15
16
# File 'lib/railgun/mailer.rb', line 14

def domain
  @domain
end

#settingsObject

Hash

config ->

Requires *at least* `api_key` and `domain` keys.

14
15
16
# File 'lib/railgun/mailer.rb', line 14

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object


38
39
40
41
42
43
44
45
46
47
# File 'lib/railgun/mailer.rb', line 38

def deliver!(mail)
  mg_message = Railgun.transform_for_mailgun(mail)
  response = @mg_client.send_message(@domain, mg_message)

  if response.code == 200 then
    mg_id = response.body['id']
    mail.message_id = mg_id
  end
  response
end

#mailgun_clientObject


49
50
51
# File 'lib/railgun/mailer.rb', line 49

def mailgun_client
  @mg_obj
end