Class: Kiss::Mailer
- 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
- .send(options) ⇒ Object
-
.send_via_sendmail(options) ⇒ Object
Attempts to send message using /usr/sbin/sendmail.
-
.send_via_smtp(options) ⇒ Object
Attempts to send message using Net::SMTP.
Instance Method Summary collapse
-
#initialize(new_options = {}) ⇒ Mailer
constructor
Creates new email message object.
- #merge_options(new_options = {}) ⇒ Object
-
#prepare_email_message(new_options = {}) ⇒ Object
Renders email template to string, unless message option is already set to a string value.
-
#send(new_options = {}) ⇒ Object
Attempts to send message using SMTP, unless :engine option is set to :sendmail.
Methods included from TemplateMethods
Methods included from DatabaseAccessors
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
Constructor Details
#initialize(new_options = {}) ⇒ Mailer
Creates new email message object.
61 62 63 64 |
# File 'lib/kiss/mailer.rb', line 61 def initialize( = {}) @_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() = .clone if [:sendmail] || ([:engine] == :sendmail) || ![:server] send_via_sendmail() else send_via_smtp() 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() puts [:message] IO.popen([:sendmail] || @@sendmail_path, "w") do |pipe| pipe.puts([: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() require 'net/smtp' unless defined?(Net::SMTP) Net::SMTP.start( [:server] || 'localhost', [:port] || 25, [:domain] || nil, [:account] || [:username] || nil, [:password] || nil, [:auth] || :plain ) do |smtp| smtp.sendmail([:message], [:from], [: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 ( = {}) if [:controller] @_controller = [:controller] @_options = @_controller.mailer_config.merge(@_options) end @_options.merge!() @_request = [: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 ( = {}) () 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 = [: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 = [name] value = value.join(', ') if value.is_a?(Array) new_headers_by_name[headercase_name] = value end end ([: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") [: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( = {}) () (@_controller.mailer_override) self.class.send() end |