Module: MailPlugger

Defined in:
lib/mail_plugger.rb,
lib/mail_plugger/error.rb,
lib/mail_plugger/railtie.rb,
lib/mail_plugger/version.rb,
lib/mail_plugger/mail_helper.rb,
lib/mail_plugger/delivery_method.rb

Defined Under Namespace

Modules: MailHelper Classes: DeliveryMethod, Error, Railtie

Constant Summary collapse

VERSION =
'1.8.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientObject (readonly)

Returns the value of attribute client.



19
20
21
# File 'lib/mail_plugger.rb', line 19

def client
  @client
end

.default_delivery_optionsObject (readonly)

Returns the value of attribute default_delivery_options.



19
20
21
# File 'lib/mail_plugger.rb', line 19

def default_delivery_options
  @default_delivery_options
end

.default_delivery_systemObject

Returns the value of attribute default_delivery_system.



16
17
18
# File 'lib/mail_plugger.rb', line 16

def default_delivery_system
  @default_delivery_system
end

.delivery_optionsObject (readonly)

Returns the value of attribute delivery_options.



19
20
21
# File 'lib/mail_plugger.rb', line 19

def delivery_options
  @delivery_options
end

.delivery_settingsObject (readonly)

Returns the value of attribute delivery_settings.



19
20
21
# File 'lib/mail_plugger.rb', line 19

def delivery_settings
  @delivery_settings
end

.delivery_systemsObject (readonly)

Returns the value of attribute delivery_systems.



19
20
21
# File 'lib/mail_plugger.rb', line 19

def delivery_systems
  @delivery_systems
end

.rotatable_delivery_systemsObject (readonly)

Returns the value of attribute rotatable_delivery_systems.



19
20
21
# File 'lib/mail_plugger.rb', line 19

def rotatable_delivery_systems
  @rotatable_delivery_systems
end

.sending_methodObject

Returns the value of attribute sending_method.



16
17
18
# File 'lib/mail_plugger.rb', line 16

def sending_method
  @sending_method
end

Class Method Details

.configureObject

Configure MailPlugger.

Examples:

using Rails ‘config/initializers/mail_plugger.rb`


MailPlugger.configure do |config|
  # It should be defined with the plug_in method.
  config.default_delivery_system = 'api_client'
  # It can be:
  #  - default_delivery_system
  #  - plugged_in_first
  #  - random
  #  - round_robin
  config.sending_method = 'default_delivery_system'
end


41
42
43
44
45
# File 'lib/mail_plugger.rb', line 41

def configure
  yield self
rescue NoMethodError => e
  raise Error::WrongConfigureOption, e.message
end

.plug_in(delivery_system) ⇒ Object

Plug in SMTP(s) or defined API(s) class(es).

Examples:

using Rails ‘config/initializers/mail_plugger.rb`


# Using SMTP:

MailPlugger.plug_in('smtp_client') do |smtp|
  smtp.delivery_settings = {
    smtp_settings: {
      address: 'smtp.server.com',
      port: 587,
      domain: 'test.domain.com',
      enable_starttls_auto: true,
      user_name: 'test_user',
      password: '1234',
      authentication: :plain
    }
  }
end

# Using API:

# The defined API class should have an 'initialize' and a 'deliver'
# method.
class DefinedApiClientClass
  def initialize(options = {})
    @settings = { api_key: '12345' }
    @options = options
  end

  def deliver
    API.new(@settings).client.post(generate_mail_hash)
  end

  private

  def generate_mail_hash
    {
      to: generate_recipients,
      from: {
        email: @options[:from].first
      },
      subject: @options[:subject],
      content: [
        {
          type: 'text/plain',
          value: @options[:text_part]
        },
        {
          type: 'text/html; charset=UTF-8',
          value: @options[:html_part]
        }
      ],
      tags: [
        @options[:tag]
      ]
    }
  end

  def generate_recipients
    @options[:to].map do |to|
      {
        email: to
      }
    end
  end
end

MailPlugger.plug_in('api_client') do |api|
  api.client = DefinedApiClientClass
  # Default delivery options for the plugged in client.
  api.default_delivery_options = { tag: 'test_tag' }
  # It will search these options in the Mail::Message object.
  api.delivery_options = [:to, :from, :subject, :text_part, :html_part]
  api.delivery_settings = { return_response: true }
end

Parameters:

  • delivery_system (String/Symbol)

    the name of the SMTP/API



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mail_plugger.rb', line 126

def plug_in(delivery_system)
  check_value(delivery_system)

  @delivery_system = delivery_system
  (@delivery_systems ||= []) << delivery_system
  @rotatable_delivery_systems = @delivery_systems.cycle

  yield self
rescue NoMethodError => e
  raise Error::WrongPlugInOption, e.message
end