module Hanami::Mailer::ClassMethods

@since 0.1.0

Public Instance Methods

deliver(locals = {}) click to toggle source

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.

@param locals [Hash] a set of objects that acts as context for the rendering @option :format [Symbol] specify format to deliver @option :charset [String] charset

@since 0.1.0

@see Hanami::Mailer::Configuration#default_charset

@example

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")
# File lib/hanami/mailer.rb, line 197
def deliver(locals = {})
  new(locals).deliver
end