Module: Hanami::Mailer

Includes:
Utils::ClassAttribute
Defined in:
gems/gems/hanami-1.3.1/lib/hanami/mailer/glue.rb,
gems/gems/hanami-mailer-1.3.1/lib/hanami/mailer/dsl.rb,
gems/gems/hanami-mailer-1.3.1/lib/hanami/mailer/version.rb,
gems/gems/hanami-mailer-1.3.1/lib/hanami/mailer/template.rb,
gems/gems/hanami-mailer-1.3.1/lib/hanami/mailer/configuration.rb,
gems/gems/hanami-mailer-1.3.1/lib/hanami/mailer/rendering/template_name.rb,
gems/gems/hanami-mailer-1.3.1/lib/hanami/mailer/rendering/templates_finder.rb,
gems/gems/hanami-mailer-1.3.1/lib/hanami/mailer.rb

Overview

Hanami::Mailer

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods Classes: Error, MissingDeliveryDataError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

def method_missing(method_name)
  @locals.fetch(method_name) { super }
end

Instance Attribute Details

#mailObject (readonly, protected)

Since:

  • 0.1.0

def mail
  @mail
end

Class Method Details

.configure(&blk) ⇒ Object

Configure the framework. It yields the given block in the context of the configuration

Examples:

require 'hanami/mailer'

Hanami::Mailer.configure do
  root '/path/to/root'
end

Parameters:

  • blk (Proc)

    the configuration block

See Also:

Since:

  • 0.1.0

def self.configure(&blk)
  configuration.instance_eval(&blk)
  self
end

.deliveriesArray

Test deliveries

This is a collection of delivered messages, used when delivery_method is set on :test

Examples:

require 'hanami/mailer'

Hanami::Mailer.configure do
  delivery_method :test
end.load!

# In testing code
Signup::Welcome.deliver
Hanami::Mailer.deliveries.count # => 1

Returns:

  • (Array)

    a collection of delivered messages

See Also:

Since:

  • 0.1.0

def self.deliveries
  Mail::TestMailer.deliveries
end

Instance Method Details

#buildObject (private)

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

Since:

  • 0.1.0

def build
  Mail.new.tap do |m|
    m.from     = __dsl(:from)
    m.to       = __dsl(:to)
    m.cc       = __dsl(:cc)
    m.bcc      = __dsl(:bcc)
    m.reply_to = __dsl(:reply_to)
    m.subject  = __dsl(:subject)

    m.charset   = charset
    m.html_part = __part(:html)
    m.text_part = __part(:txt)

    m.delivery_method(*Hanami::Mailer.configuration.delivery_method)
  end
end

#initialize(locals = {}) ⇒ Object

Initialize a mailer

Parameters:

  • locals (Hash) (defaults to: {})

    a set of objects that acts as context for the rendering

Since:

  • 0.1.0

def initialize(locals = {})
  @locals  = locals
  @format  = locals.fetch(:format, nil)
  @charset = locals.fetch(:charset, self.class.configuration.default_charset)
  @mail    = build
  prepare
end

#prepareObject (protected)

Prepare the email message when a new mailer is initialized.

This is a hook that can be overwritten by mailers.

Examples:

require 'hanami/mailer'

module Billing
  class Invoice
    include Hanami::Mailer

    subject 'Invoice'
    from    'noreply@example.com'
    to      ''

    def prepare
      mail.attachments['invoice.pdf'] = File.read('/path/to/invoice.pdf')
    end

    private

    def recipient
      user.email
    end
  end
end

invoice = Invoice.new
user    = User.new(name: 'L', email: 'user@example.com')

Since:

  • 0.1.0

def prepare
end