Class: Kiss::Mailer

Inherits:
Object show all
Includes:
ControllerAccessors, DatabaseAccessors, RequestAccessors, TemplateMethods
Defined in:
lib/kiss/mailer.rb

Overview

:server, :port, :domain, :account, :password, :auth - for SMTP login :from/to - used by SMTP to send message (ignored by sendmail) :message - message headers/body text :template - name of email template to use to create message :data/vars - values to inject into email template :sendmail_path - filesystem path to sendmail executable

Constant Summary collapse

@@sendmail_path =
"/usr/sbin/sendmail -t"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TemplateMethods

#data, #data=, #set

Methods included from DatabaseAccessors

#database

Methods included from RequestAccessors

#app, #cookies, #debug, #env, #file_cache, #host, #login, #models, #path_info, #protocol, #pub, #query_string_with_params, #redirect_action, #redirect_url, #request_url_with_params, #response, #send_file, #send_response, #session, #set_cookie, #set_exception_cache, #start_benchmark, #stop_benchmark

Methods included from ControllerAccessors

#database, #environment, #models, #public_dir, #upload_dir

Methods included from KissAccessors

#html_escape, #url_escape

Constructor Details

#initialize(new_options = {}) ⇒ Mailer

Creates new email message object.



61
62
63
64
# File 'lib/kiss/mailer.rb', line 61

def initialize(new_options = {})
  @_options = {}
  merge_options(new_options)
end

Class Method Details

.send(options) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/kiss/mailer.rb', line 16

def send(options)
  options = options.clone
  
  if options[:sendmail] || (options[:engine] == :sendmail) || !options[:server]
    send_via_sendmail(options)
  else
    send_via_smtp(options)
  end
end

.send_via_sendmail(options) ⇒ Object

Attempts to send message using /usr/sbin/sendmail. NOTE: sendmail ignores :from and :to options, using From and To headers from the message



29
30
31
32
33
34
# File 'lib/kiss/mailer.rb', line 29

def send_via_sendmail(options)
  puts options[:message]
  IO.popen(options[:sendmail] || @@sendmail_path, "w") do |pipe|
    pipe.puts(options[:message])
  end
end

.send_via_smtp(options) ⇒ Object

Attempts to send message using Net::SMTP.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kiss/mailer.rb', line 37

def send_via_smtp(options)
  require 'net/smtp' unless defined?(Net::SMTP)
  
  Net::SMTP.start(
    options[:server] || 'localhost',
    options[:port] || 25,
    options[:domain] || nil,
    options[:account] || options[:username] || nil,
    options[:password] || nil,
    options[:auth] || :plain
  ) do |smtp|
		smtp.sendmail(options[:message], options[:from], options[:to])
	end
end

Instance Method Details

#merge_options(new_options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/kiss/mailer.rb', line 66

def merge_options(new_options = {})
  if new_options[:controller]
    @_controller = new_options[:controller]
    @_options = @_controller.mailer_config.merge(@_options)
  end
  
  @_options.merge!(new_options)
  @_request = new_options[:request] || @_request
end

#prepare_email_message(new_options = {}) ⇒ Object

Renders email template to string, unless message option is already set to a string value. Also adds missing headers.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/kiss/mailer.rb', line 78

def prepare_email_message(new_options = {})
  merge_options(new_options)

  unless @_options[:message].is_a?(String)
    if template = @_options[:template]
      @@template_class ||= Kiss::Template.get_root_class(controller, controller.email_template_dir)
      @data = @_options[:data] || @_options[:vars]
      @_options[:message] = @@template_class.get_subclass_from_path('/'+template).new(request, self).call
    else
      raise 'no email message or template found to prepare'
    end
  end
  
  headers, body = options[:message].split(/\n\n/, 2)
  header_lines = headers.split(/\n/)
  headers_by_name = {}
  headers.each do |header|
    name, value = header.split(/:\s*/, 2)
    headers_by_name[name] = value
  end
  
  new_headers_by_name = {}
  [:from, :to].each do |name|
    headercase_name = name.to_s.headercase
    unless headers_by_name[headercase_name]
      value = options[name]
      value = value.join(', ') if value.is_a?(Array)
      new_headers_by_name[headercase_name] = value
    end
  end
  
  (options[:headers] || {}).each_pair do |name, value|
    new_headers_by_name[name.to_s.headercase_name] = value
  end
  
  new_headers_by_name.each_pair do |name, value|
    header_line = "#{name}: #{value}"
    header_lines.unshift header_line
  end
  
  headers = header_lines.join("\n")
  options[:message] = "#{headers}\n\n#{body}"

  return @_options[:message]
end

#send(new_options = {}) ⇒ Object

Attempts to send message using SMTP, unless :engine option is set to :sendmail.



126
127
128
129
130
131
132
# File 'lib/kiss/mailer.rb', line 126

def send(new_options = {})
  merge_options(new_options)
  merge_options(@_controller.mailer_override)
  
  prepare_email_message
  self.class.send(options)
end