Class: Padrino::Mailer::Base

### Home Classes Methods

### In Files

- lib/padrino-mailer/base.rb

### Parent

Object

### Methods

- ::new
- #defaults
- #email
- #message

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Padrino
- Padrino::Mailer
- Padrino::Mailer::Base
- Padrino::Mailer::Helpers
- Padrino::Mailer::Helpers::ClassMethods
- Padrino::Mailer::Mime
- Mail
- Mail::Message

No matching classes.

# Padrino::Mailer::Base

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      'user@domain.com'
        from    'admin@site.com'
        subject 'Welcome to the site!'
        locals  :name => name
        render  'registration'
      end
    end

Use the mailer to deliver messages:

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

### Attributes

app[RW]
delivery_settings[RW]
mailer_name[RW]
messages[RW]

### Public Class Methods

new(app, name, &block) click to toggle source

Constructs a `Mailer` base object with specified options.

@param [Sinatra::Application] app

    The application tied to this mailer.

@param [Symbol] name

    The name of this mailer.

@param [Proc] block

    The +email+ definitions block.

@see Padrino::Mailer::Helpers::ClassMethods#mailer

    # File lib/padrino-mailer/base.rb, line 58
    def initialize(app, name, &block)
      @mailer_name = name
      @messages    = {}
      @defaults    = {}
      @app         = app
      instance_eval(&block)
    end

### Public Instance Methods

defaults(attributes=nil) click to toggle source

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

@param [Hash] attributes

    The hash of message options to use as default.

@example

    mailer :alternate do
      defaults :from => 'padrino@from.com', :to => 'padrino@to.com'
      email(:foo) do; end
    end

    # File lib/padrino-mailer/base.rb, line 107
    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) click to toggle source

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

@param [Symbol] name

    The name of this email message.

@param [Proc] block

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

@example

    email :birthday do |name, age|
      subject "Happy Birthday!"
      to   'john@fake.com'
      from 'noreply@birthday.com'
      locals 'name' => name, 'age' => age
      render 'birthday'
    end

    # File lib/padrino-mailer/base.rb, line 83
    def email(name, &block)
      raise "The email '#{name}' is already defined" if self.messages[name].present?
      self.messages[name] = Proc.new { |*attrs|
        message = app.settings._padrino_mailer::Message.new(self.app)
        message.defaults = self.defaults if self.defaults.any?
        message.delivery_method(*delivery_settings)
        message.instance_exec(*attrs, &block)
        message
      }
    end

Also aliased as: message
message(name, &block) click to toggle source
Alias for: email

[Validate]

Generated with the Darkfish Rdoc Generator 2.