Class: Ruport::Mailer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ruport/util/mailer.rb

Overview

Overview

This class uses SMTP to provide a simple mail sending mechanism. It also uses MailFactory to provide attachment and HTML email support.

Defined Under Namespace

Classes: InvalidMailerConfigurationError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mailer_label = :default) ⇒ Mailer

Creates a new Mailer object. Optionally, you can select a mailer by label

Example:

a = Mailer.new        # uses the :default mailer
a = Mailer.new :foo   # uses :foo mail config from Ruport::Config


32
33
34
35
36
37
# File 'lib/ruport/util/mailer.rb', line 32

def initialize( mailer_label=:default )
  select_mailer(mailer_label)
  mail_object.from = @mailer.address if mail_object.from.to_s.empty?
  rescue
    raise "you need to specify a mailer to use"
end

Class Method Details

.add_mailer(name, options) ⇒ Object

:call-seq:

add_mailer(mailer_name, options)

Creates or retrieves a mailer configuration. Available options:

:host

The SMTP host to use.

:address

Address the email is being sent from.

:user

The username to use on the SMTP server

:password

The password to use on the SMTP server. Optional.

:port

The SMTP port to use. Optional, defaults to 25.

:auth_type

SMTP authorization method. Optional, defaults to :plain.

:mail_class

If you don’t want to use the default MailFactory object, you can pass another mailer to use here.

Example (creating a mailer config):

add_mailer :alternate, :host => "mail.test.com", 
                       :address => "[email protected]",
                       :user => "test", 
                       :password => "blinky"
                       :auth_type => :cram_md5

Example (retreiving a mailer config):

mail_conf = mailers[:alternate]  #=> <OpenStruct ..>
mail_conf.address                #=> [email protected]


72
73
74
75
# File 'lib/ruport/util/mailer.rb', line 72

def add_mailer(name,options)
  mailers[name] = OpenStruct.new(options)
  check_mailer(mailers[name],name)
end

.default_mailerObject

Alias for mailers[:default].



78
79
80
# File 'lib/ruport/util/mailer.rb', line 78

def default_mailer
  mailers[:default]
end

.mailersObject

Returns all the mailers defined



83
# File 'lib/ruport/util/mailer.rb', line 83

def mailers; @mailers ||= {}; end

Instance Method Details

#deliver(options = {}) ⇒ Object

Sends the message.

Example:

mailer.deliver :from => "[email protected]",
               :to   => "[email protected]"


101
102
103
104
105
106
107
108
109
# File 'lib/ruport/util/mailer.rb', line 101

def deliver(options={})           
  to = options.delete(:to)
  mail_object.to = Array(to).join(",")
  options.each { |k,v| send("#{k}=",v) if respond_to? "#{k}=" }
  
  Net::SMTP.start(@host,@port,@host,@user,@password,@auth) do |smtp|
    smtp.send_message((options[:mail_object] || mail_object).to_s, options[:from], to)
  end
end