Sha256: 9c9b33b671ecfacb52c254d1551857c7a24b5050a95c3594ccec0559976e7c92

Contents?: true

Size: 1.72 KB

Versions: 101

Compression:

Stored size: 1.72 KB

Contents

class Knj::Mail
  def initialize(paras = {})
    @paras = {
      "smtp_host" => "localhost",
      "smtp_port" => 25,
      "smtp_user" => nil,
      "smtp_passwd" => nil,
      "smtp_domain" => ENV["HOSTNAME"]
    }
    
    if paras.is_a?(Hash)
      paras.each do |key, value|
        @paras[key] = value
      end
    end
    
    self.send if @paras["send"]
  end
  
  def html=(value)
    @paras["html"] = value
  end
  
  def text=(value)
    @paras["text"] = value
  end
  
  def from=(value)
    @paras["from"] = value
  end
  
  def subject=(value)
    @paras["subject"] = value
  end
  
  def to=(value)
    @paras["to"] = value.untaint
  end
  
  def send
    raise "No email has been defined to send to." if !@paras["to"]
    raise "No subject has been defined." if !@paras["subject"]
    raise "No content has been defined." if !@paras["text"] and !@paras["html"]
    
    mail = TMail::Mail.new
    mail.to = @paras["to"]
    mail.subject = @paras["subject"]
    mail.date = Time.new
    
    mail.from = @paras["from"] if @paras["from"]
    
    if @paras["html"]
      mail.set_content_type("text", "html")
      mail.body = @paras["html"]
    elsif @paras["text"]
      mail.body = @paras["text"]
    end
    
    smtp_start = Net::SMTP.new(@paras["smtp_host"], @paras["smtp_port"])
    smtp_start.enable_ssl if @paras["ssl"]
    smtp_start.enable_starttls if @paras["tls"]
    
    if !@paras["smtp_domain"]
      if @paras["smtp_host"]
        @paras["smtp_domain"] = @paras["smtp_host"]
      else
        raise "SMTP domain not given."
      end
    end
    
    smtp_start.start(@paras["smtp_domain"], @paras["smtp_user"], @paras["smtp_passwd"]) do |smtp|
      smtp.send_message(mail.to_s, @paras["from"], @paras["to"])
    end
  end
end

Version data entries

101 entries across 101 versions & 1 rubygems

Version Path
knjrbfw-0.0.29 lib/knj/mail.rb
knjrbfw-0.0.28 lib/knj/mail.rb
knjrbfw-0.0.27 lib/knj/mail.rb
knjrbfw-0.0.26 lib/knj/mail.rb
knjrbfw-0.0.25 lib/knj/mail.rb
knjrbfw-0.0.24 lib/knj/mail.rb
knjrbfw-0.0.23 lib/knj/mail.rb
knjrbfw-0.0.22 lib/knj/mail.rb
knjrbfw-0.0.21 lib/knj/mail.rb
knjrbfw-0.0.20 lib/knj/mail.rb
knjrbfw-0.0.19 lib/knj/mail.rb
knjrbfw-0.0.18 lib/knj/mail.rb
knjrbfw-0.0.17 lib/knj/mail.rb
knjrbfw-0.0.16 lib/knj/mail.rb
knjrbfw-0.0.15 lib/knj/mail.rb
knjrbfw-0.0.14 lib/knj/mail.rb
knjrbfw-0.0.13 lib/knj/mail.rb
knjrbfw-0.0.12 lib/knj/mail.rb
knjrbfw-0.0.11 lib/knj/mail.rb
knjrbfw-0.0.10 lib/knj/mail.rb