Module: Hanami::Mailer::ClassMethods

Defined in:
lib/hanami/mailer.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#deliver(locals = {}) ⇒ Object

Delivers a multipart email message.

When a mailer defines a html and txt template, they are both delivered.

In order to selectively deliver only one of the two templates, use Signup::Welcome.deliver(format: :txt)

All the given locals, excepted the reserved ones (:format and charset), are available as rendering context for the templates.

Examples:

require 'hanami/mailer'

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

module Billing
  class Invoice
    include Hanami::Mailer

    from    '[email protected]'
    to      :recipient
    subject :subject_line

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

    private

    def recipient
      user.email
    end

    def subject_line
      "Invoice - #{ invoice.number }"
    end
  end
end

invoice = Invoice.new
user    = User.new(name: 'L', email: '[email protected]')

# Deliver both text, HTML parts and the attachment
Billing::Invoice.deliver(invoice: invoice, user: user)

# Deliver only the text part and the attachment
Billing::Invoice.deliver(invoice: invoice, user: user, format: :txt)

# Deliver only the text part and the attachment
Billing::Invoice.deliver(invoice: invoice, user: user, format: :html)

# Deliver both the parts with "iso-8859"
Billing::Invoice.deliver(invoice: invoice, user: user, charset: "iso-8859")

Parameters:

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

    a set of objects that acts as context for the rendering

  • :format (Hash)

    a customizable set of options

  • :charset (Hash)

    a customizable set of options

See Also:

Since:

  • 0.1.0



197
198
199
# File 'lib/hanami/mailer.rb', line 197

def deliver(locals = {})
  new(locals).deliver
end