Class: Hayabusa::Mail

Inherits:
Object
  • Object
show all
Defined in:
lib/hayabusa_ext/mailing.rb

Overview

This class represents the queued mails.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Mail

Returns a new instance of Mail.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hayabusa_ext/mailing.rb', line 97

def initialize(args)
  @args = args
  
  raise "No hayabusa-object was given (as :hb)." if !@args[:hb].is_a?(Hayabusa)
  raise "No :to was given." if !@args[:to]
  raise "No content was given (:html or :text)." if !@args[:html] and !@args[:text]
  
  #Test from-argument.
  if !@args[:from].to_s.strip.empty?
    #Its ok.
  elsif !@args[:hb].config[:error_report_from].to_s.strip.empty?
    @args[:from] = @args[:hb].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
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



95
96
97
# File 'lib/hayabusa_ext/mailing.rb', line 95

def args
  @args
end

Instance Method Details

#[](key) ⇒ Object

Returns a key from the arguments.



115
116
117
# File 'lib/hayabusa_ext/mailing.rb', line 115

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

#send(args = {}) ⇒ Object

Sends the email to the receiver.



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/hayabusa_ext/mailing.rb', line 120

def send(args = {})
  @args[:hb].log_puts("Sending mail '#{__id__}'.") if @args[:hb].debug
  
  if args[:proc]
    mail = args[:proc].new("Knj::Mailobj", @args[:hb].config[:smtp_args])
  else
    mail = ::Knj::Mailobj.new(@args[:hb].config[:smtp_args])
  end
  
  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 = @args[:from]
  mail.send
  
  @args[:status] = :sent
  @args[:hb].log_puts("Sent email #{self.__id__}") if @args[:hb].debug
  return true
rescue => e
  if @args[:hb].debug
    @args[:hb].log_puts("Could not send email.")
    @args[:hb].log_puts(e.inspect)
    @args[:hb].log_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