Class: Padrino::Mailer::Base

Inherits:
Object
  • Object
show all
Defined in:
padrino-mailer/lib/padrino-mailer/base.rb

Overview

This is the abstract class that other mailers will inherit from in order to send mail.

You can set the default delivery settings from your app through:

set :delivery_method, :smtp => {
  :address         => 'smtp.yourserver.com',
  :port            => '25',
  :user_name       => 'user',
  :password        => 'pass',
  :authentication  => :plain
}

or sendmail:

set :delivery_method, :sendmail

or for tests:

set :delivery_method, :test

and all delivered mail will use these settings unless otherwise specified.

Define a mailer in your application:

# app/mailers/sample_mailer.rb
MyAppName.mailers :sample do
  defaults :content_type => 'html'
  email :registration do |name, age|
    to      '[email protected]'
    from    '[email protected]'
    subject 'Welcome to the site!'
    locals  :name => name
    render  'registration'
  end
end

Use the mailer to deliver messages:

deliver(:sample, :registration, "Bob", "21")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, name, &block) ⇒ Base

Constructs a Mailer base object with specified options.

Parameters:

  • app (Sinatra::Application)

    The application tied to this mailer.

  • name (Symbol)

    The name of this mailer.

  • block (Proc)

    The email definitions block.

See Also:



58
59
60
61
62
63
64
# File 'padrino-mailer/lib/padrino-mailer/base.rb', line 58

def initialize(app, name, &block)
  @mailer_name = name
  @messages    = {}
  @defaults    = {}
  @app         = app
  instance_eval(&block)
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



45
46
47
# File 'padrino-mailer/lib/padrino-mailer/base.rb', line 45

def app
  @app
end

#delivery_settingsObject

Returns the value of attribute delivery_settings.



45
46
47
# File 'padrino-mailer/lib/padrino-mailer/base.rb', line 45

def delivery_settings
  @delivery_settings
end

#mailer_nameObject

Returns the value of attribute mailer_name.



45
46
47
# File 'padrino-mailer/lib/padrino-mailer/base.rb', line 45

def mailer_name
  @mailer_name
end

#messagesObject

Returns the value of attribute messages.



45
46
47
# File 'padrino-mailer/lib/padrino-mailer/base.rb', line 45

def messages
  @messages
end

Instance Method Details

#defaults(attributes = nil) ⇒ Object

Defines the default attributes for a message in this mailer (including app-wide defaults).

Examples:

mailer :alternate do
  defaults :from => '[email protected]', :to => '[email protected]'
  email(:foo) do; end
end

Parameters:

  • attributes (Hash) (defaults to: nil)

    The hash of message options to use as default.



109
110
111
112
113
114
115
# File 'padrino-mailer/lib/padrino-mailer/base.rb', line 109

def defaults(attributes=nil)
  if attributes.nil? # Retrieve the default values
    @app.respond_to?(:mailer_defaults) ? @app.mailer_defaults.merge(@defaults) : @defaults
  else # updates the default values
    @defaults = attributes
  end
end

#email(name, &block) ⇒ Object Also known as: message

Defines a mailer object allowing the definition of various email messages that can be delivered.

Examples:

email :birthday do |name, age|
  subject "Happy Birthday!"
  to   '[email protected]'
  from '[email protected]'
  locals 'name' => name, 'age' => age
  render 'birthday'
end

Parameters:

  • name (Symbol)

    The name of this email message.

  • block (Proc)

    The message definition (i.e subject, to, from, locals).



83
84
85
86
87
88
89
90
91
92
93
94
# File 'padrino-mailer/lib/padrino-mailer/base.rb', line 83

def email(name, &block)
  raise "The email '#{name}' is already defined" if self.messages[name]
  self.messages[name] = Proc.new { |*attrs|
    message = app.settings._padrino_mailer::Message.new(self.app)
    message.mailer_name = mailer_name
    message.message_name = name
    message.defaults = self.defaults if self.defaults.any?
    message.delivery_method(*delivery_settings)
    message.instance_exec(*attrs, &block)
    message
  }
end