Class: Merb::Mailer

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

Overview

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 
}

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 => '[email protected]',
                     :from => '[email protected]',
                     :subject => 'Welcome to whatever!',
                     :body => partial(:sometemplate)
m.deliver!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(o = {}) ⇒ Mailer

Parameters

o<Hash=> Object>

Configuration commands to send to MailFactory.



95
96
97
98
99
100
101
# File 'lib/merb-mailer/mailer.rb', line 95

def initialize(o={})
  self.config = {:sendmail_path => '/usr/sbin/sendmail'} if config.nil? 
  o[:rawhtml] = o.delete(:html)
  m = MailFactory.new()
  o.each { |k,v| m.send "#{k}=", v }
  @mail = m
end

Instance Attribute Details

#mailObject

Returns the value of attribute mail.



43
44
45
# File 'lib/merb-mailer/mailer.rb', line 43

def mail
  @mail
end

Instance Method Details

#attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil, type = nil, headers = nil) ⇒ Object

Parameters

file_or_files<File, Array>

File(s) to attach.

filename<String>
type<~to_s>

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

headers<String, Array>

Additional attachment headers.

Raises

ArgumentError

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



83
84
85
86
87
88
89
90
91
# File 'lib/merb-mailer/mailer.rb', line 83

def attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil, 
  type = nil, headers = nil)
  if file_or_files.is_a?(Array)
    file_or_files.each {|k,v| @mail.add_attachment_as k, *v}
  else
    raise ArgumentError, "You did not pass in a file. Instead, you sent a #{file_or_files.class}" if !file_or_files.is_a?(File)
    @mail.add_attachment_as(file_or_files, filename, type, headers)
  end
end

#deliver!Object

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



68
69
70
# File 'lib/merb-mailer/mailer.rb', line 68

def deliver!
  send(delivery_method || :net_smtp)
end

#net_smtpObject

Sends the mail using SMTP.



54
55
56
57
58
59
# File 'lib/merb-mailer/mailer.rb', line 54

def net_smtp
  Net::SMTP.start(config[:host], config[:port].to_i, config[:domain], 
                  config[:user], config[:pass], config[:auth]) { |smtp|
    smtp.send_message(@mail.to_s, @mail.from.first, @mail.to.to_s.split(/[,;]/))
  }
end

#sendmailObject

Sends the mail using sendmail.



47
48
49
50
51
# File 'lib/merb-mailer/mailer.rb', line 47

def sendmail
  sendmail = IO.popen("#{config[:sendmail_path]} #{@mail.to}", 'w+') 
  sendmail.puts @mail.to_s
  sendmail.close
end

#test_sendObject

Tests mail sending by adding the mail to deliveries.



62
63
64
# File 'lib/merb-mailer/mailer.rb', line 62

def test_send
  deliveries << @mail
end