Parent

Files

Merb::Mailer

You’ll need a simple config like this in init.rb if you want to actually send mail:

  Merb::Mailer.config = {
    :host   => 'smtp.yourserver.com',
    :port   => '25',
    :user   => 'user',
    :pass   => 'pass',
    :auth   => :plain # :plain, :login, :cram_md5, the default is no auth
    :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
    :tls    => true/false/nil, true enables tls, default nil 
  }

  or

  Merb::Mailer.config = {:sendmail_path => '/somewhere/odd'}
  Merb::Mailer.delivery_method = :sendmail

You could send mail manually like this (but it’s better to use a MailController instead).

  m = Merb::Mailer.new :to => 'foo@bar.com',
                       :from => 'bar@foo.com',
                       :subject => 'Welcome to whatever!',
                       :html => partial(:sometemplate)
  m.deliver!

You can use :text option to specify plain text email body and :html for HTML email body.

Constants

VERSION

Attributes

mail[RW]

Public Class Methods

new(o={}) click to toggle source

Parameters

o Object}>

Configuration commands to send to MailFactory.

     # File lib/merb-mailer/mailer.rb, line 116
116:     def initialize(o={})
117:       self.config = { :sendmail_path => '/usr/sbin/sendmail' } if config.nil?
118:       o[:rawhtml] = o.delete(:html)
119:       m = MailFactory.new()
120:       o.each { |k,v| m.send "#{k}=", v }
121:       @mail = m
122:     end

Public Instance Methods

attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil, type = nil, headers = nil) click to toggle source

Parameters

file_or_files

File(s) to attach.

filename
type<~to_s>

The attachment MIME type. If left out, it will be determined from file_or_files.

headers

Additional attachment headers.

Raises

ArgumentError

file_or_files was not a File or an Array of File instances.

     # File lib/merb-mailer/mailer.rb, line 97
 97:     def attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil,
 98:       type = nil, headers = nil)
 99:       if file_or_files.is_a?(Array)
100:         file_or_files.each do |v|
101:           if v.length < 2
102:             v << v.first.is_a?(File) ? File.basename(v.first.path) : nil
103:           end
104:           @mail.add_attachment_as *v
105:         end
106:       else
107:         if !file_or_files.is_a?(File) && !file_or_files.is_a?(StringIO)
108:           raise ArgumentError, "You did not pass in a file. Instead, you sent a #{file_or_files.class}" 
109:         end
110:         @mail.add_attachment_as(file_or_files, filename, type, headers)
111:       end
112:     end
deliver!() click to toggle source

Delivers the mail with the specified delivery method, defaulting to net_smtp.

    # File lib/merb-mailer/mailer.rb, line 82
82:     def deliver!
83:       send(delivery_method || :net_smtp)
84:     end
net_smtp() click to toggle source

Sends the mail using SMTP.

    # File lib/merb-mailer/mailer.rb, line 57
57:     def net_smtp
58:       smtp = Net::SMTP.new(config[:host], config[:port].to_i)
59:       if config[:tls]
60:         if smtp.respond_to?(:enable_starttls) # 1.9.x
61:           smtp.enable_starttls
62:         elsif smtp.respond_to?(:enable_tls) && smtp.respond_to?(:use_tls?)
63:           smtp.enable_tls(OpenSSL::SSL::VERIFY_NONE) # 1.8.x with tlsmail
64:         else
65:           raise 'Unable to enable TLS, for Ruby 1.8.x install tlsmail'
66:         end
67:       end
68:       smtp.start(config[:domain], config[:user], config[:pass], config[:auth]) { |smtp|
69:         @mail.to = [@mail.to] unless @mail.to.is_a?(Array)
70:         to = @mail.to.inject([]) {|r, e| r + e.split(/[,;]/) }.map {|e| e.strip}
71:         smtp.send_message(@mail.to_s, @mail.from.first, to)
72:       }
73:     end
sendmail() click to toggle source

Sends the mail using sendmail.

    # File lib/merb-mailer/mailer.rb, line 50
50:     def sendmail
51:       sendmail = IO.popen("#{config[:sendmail_path]} #{@mail.to}", 'w+')
52:       sendmail.puts @mail.to_s
53:       sendmail.close
54:     end
test_send() click to toggle source

Tests mail sending by adding the mail to deliveries.

    # File lib/merb-mailer/mailer.rb, line 76
76:     def test_send
77:       deliveries << @mail
78:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.