Module: Hanami::Mailer::ClassMethods

Defined in:
gems/gems/hanami-mailer-1.3.1/lib/hanami/mailer.rb,
gems/gems/hanami-mailer-1.3.3/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    'noreply@example.com'
    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: 'user@example.com')

# 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

See Also:

Since:

  • 0.1.0

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