Class: Knjappserver::Mail

Inherits:
Object
  • Object
show all
Defined in:
lib/include/class_knjappserver_mailing.rb

Overview

This class represents the queued mails.

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Mail

Returns a new instance of Mail.



91
92
93
94
95
96
97
# File 'lib/include/class_knjappserver_mailing.rb', line 91

def initialize(args)
	@args = args
	
	raise "No knjappserver-object was given (as :kas)." if !@args[:kas].is_a?(Knjappserver)
	raise "No :to was given." if !@args[:to]
    raise "No content was given (:html or :text)." if !@args[:html] and !@args[:text]
end

Instance Method Details

#[](key) ⇒ Object

Returns a key from the arguments.



100
101
102
# File 'lib/include/class_knjappserver_mailing.rb', line 100

def [](key)
	return @args[key]
end

#send(args = {}) ⇒ Object

Sends the email to the receiver.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/include/class_knjappserver_mailing.rb', line 105

def send(args = {})
    STDOUT.print "Sending mail '#{__id__}'.\n" if @args[:kas].debug
    
    if @args[:from]
      from = @args[:from]
    elsif @args[:kas].config[:error_report_from]
      from = @args[:kas].config[:error_report_from]
    else
      raise "Dont know where to take the 'from'-paramter from - none given in appserver config or mail-method-arguments?"
    end
    
    if args["proc"]
      args["proc"].static("Object", "require", "knj/mailobj")
      mail = args["proc"].new("Knj::Mailobj", @args[:kas].config[:smtp_args])
      mail._pm_send_noret("to=", @args[:to])
      mail._pm_send_noret("subject=", @args[:subject]) if @args[:subject]
      mail._pm_send_noret("html=", Knj::Strings.email_str_safe(@args[:html])) if @args[:html]
      mail._pm_send_noret("text=", Knj::Strings.email_str_safe(@args[:text])) if @args[:text]
      mail._pm_send_noret("from=", from)
      mail._pm_send_noret("send")
    else
      require "knj/mailobj"
      mail = Knj::Mailobj.new(@args[:kas].config[:smtp_args])
      mail.to = @args[:to]
      mail.subject = @args[:subject] if @args[:subject]
      mail.html = Knj::Strings.email_str_safe(@args[:html]) if @args[:html]
      mail.text = Knj::Strings.email_str_safe(@args[:text]) if @args[:text]
      mail.from = from
      mail.send
    end
    
    @args[:status] = :sent
    STDOUT.print "Sent email #{self.__id__}\n" if @args[:kas].debug
    return true
  rescue => e
    if @args[:kas].debug
      STDOUT.print "Could not send email.\n"
      STDOUT.puts e.inspect
      STDOUT.puts e.backtrace
    end
    
    @args[:errors][e.class.name] = {:count => 0} if !@args[:errors].has_key?(e.class.name)
    @args[:errors][e.class.name][:count] += 1
    raise e if @args[:errors][e.class.name][:count] >= 5
    @args[:status] = :error
    @args[:error] = e
    
    return false
end