module Hanami::Mailer
@since 0.1.0
Constants
- CONTENT_TYPES
Content types mapping
@since 0.1.0 @api private
- VERSION
@since 0.1.0
Attributes
@api private @since 0.1.0
@since 0.1.0
Public Class Methods
Configure the framework. It yields the given block in the context of the configuration
@param blk [Proc] the configuration block
@since 0.1.0
@see Hanami::Mailer::Configuration
@example
require 'hanami/mailer' Hanami::Mailer.configure do root '/path/to/root' end
# File lib/hanami/mailer.rb, line 65 def self.configure(&blk) configuration.instance_eval(&blk) self end
Test deliveries
This is a collection of delivered messages, used when delivery_method
is set on :test
@return [Array] a collection of delivered messages
@since 0.1.0
@see Hanami::Mailer::Configuration#delivery_mode
@example
require 'hanami/mailer' Hanami::Mailer.configure do delivery_method :test end.load! # In testing code Signup::Welcome.deliver Hanami::Mailer.deliveries.count # => 1
# File lib/hanami/mailer.rb, line 118 def self.deliveries Mail::TestMailer.deliveries end
Override Ruby's hook for modules. It includes basic Hanami::Mailer
modules to the given Class. It sets a copy of the framework configuration
@param base [Class] the target mailer
@since 0.1.0 @api private
@see www.ruby-doc.org/core/Module.html#method-i-included
# File lib/hanami/mailer.rb, line 80 def self.included(base) conf = configuration conf.add_mailer(base) base.class_eval do extend Dsl extend ClassMethods include Utils::ClassAttribute class_attribute :configuration self.configuration = conf.duplicate end conf.copy!(base) end
Load the framework
@since 0.1.0 @api private
# File lib/hanami/mailer.rb, line 126 def self.load! Mail.eager_autoload! configuration.load! end
Initialize a mailer
@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
# File lib/hanami/mailer.rb, line 209 def initialize(locals = {}) @locals = locals @format = locals.fetch(:format, nil) @charset = locals.fetch(:charset, self.class.configuration.default_charset) @mail = build prepare end
Public Instance Methods
Delivers a multipart email, by looking at all the associated templates and render them.
@since 0.1.0 @api private
# File lib/hanami/mailer.rb, line 233 def deliver mail.deliver rescue ArgumentError => exception raise MissingDeliveryDataError if exception.message =~ /SMTP (From|To) address/ raise end
Render a single template with the specified format.
@param format [Symbol] format
@return [String] the output of the rendering process.
@since 0.1.0 @api private
# File lib/hanami/mailer.rb, line 225 def render(format) self.class.templates(format).render(self, @locals) end
Protected Instance Methods
@api private @since 0.1.0
# File lib/hanami/mailer.rb, line 279 def method_missing(method_name) @locals.fetch(method_name) { super } end
Prepare the email message when a new mailer is initialized.
This is a hook that can be overwritten by mailers.
@since 0.1.0
@example
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')
# File lib/hanami/mailer.rb, line 274 def prepare end
Private Instance Methods
@api private @since 0.1.0
# File lib/hanami/mailer.rb, line 312 def __dsl(method_name) case result = self.class.__send__(method_name) when Symbol __send__(result) else result end end
@api private @since 0.1.0
# File lib/hanami/mailer.rb, line 323 def __part(format) return unless __part?(format) Mail::Part.new.tap do |part| part.content_type = "#{CONTENT_TYPES.fetch(format)}; charset=#{charset}" part.body = render(format) end end
@api private @since 0.1.0
# File lib/hanami/mailer.rb, line 334 def __part?(format) @format == format || (!@format && !self.class.templates(format).nil?) end
# File lib/hanami/mailer.rb, line 292 def build Mail.new.tap do |m| m.return_path = __dsl(:return_path) 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
@api private @since 0.4.0
# File lib/hanami/mailer.rb, line 341 def respond_to_missing?(method_name, _include_all) @locals.key?(method_name) end