Class: Knj::Mailobj

Inherits:
Object show all
Defined in:
lib/knj/mailobj.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Mailobj

Returns a new instance of Mailobj.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/knj/mailobj.rb', line 2

def initialize(args = {})
  @args = {
    "smtp_host" => "localhost",
    "smtp_port" => 25,
    "smtp_user" => nil,
    "smtp_passwd" => nil,
    "smtp_domain" => ENV["HOSTNAME"]
  }
  
  if args.is_a?(Hash)
    args.each do |key, value|
      @args[key] = value
    end
  end
  
  self.send if @args["send"]
end

Instance Method Details

#from=(value) ⇒ Object



28
29
30
# File 'lib/knj/mailobj.rb', line 28

def from=(value)
  @args["from"] = value
end

#html=(value) ⇒ Object



20
21
22
# File 'lib/knj/mailobj.rb', line 20

def html=(value)
  @args["html"] = value
end

#sendObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/knj/mailobj.rb', line 40

def send
  raise "No email has been defined to send to." if !@args["to"]
  raise "No subject has been defined." if !@args["subject"]
  raise "No content has been defined." if !@args["text"] and !@args["html"]
  
  require "mail"
  
  mail = Mail.new
  mail.to = @args["to"]
  mail.subject = @args["subject"]
  mail.date = Time.now
  mail.from = @args["from"] if @args["from"]
  
  if @args["html"]
    tha_html = @args["html"]
    mail.html_part do
      content_type "text/html; charset=UTF-8"
      body tha_html
    end
  end
  
  if @args["text"]
    tha_text = @args["text"]
    mail.text_part do
      body tha_text
    end
  end
  
  smtp_start = Net::SMTP.new(@args["smtp_host"], @args["smtp_port"])
  smtp_start.enable_ssl if @args["ssl"]
  smtp_start.enable_starttls if @args["tls"]
  
  if !@args["smtp_domain"]
    if @args["smtp_host"]
      @args["smtp_domain"] = @args["smtp_host"]
    else
      raise "SMTP domain not given."
    end
  end
  
  smtp_start.start(@args["smtp_domain"], @args["smtp_user"], @args["smtp_passwd"]) do |smtp|
    smtp.send_message(mail.to_s, @args["from"], @args["to"])
  end
end

#subject=(value) ⇒ Object



32
33
34
# File 'lib/knj/mailobj.rb', line 32

def subject=(value)
  @args["subject"] = value
end

#text=(value) ⇒ Object



24
25
26
# File 'lib/knj/mailobj.rb', line 24

def text=(value)
  @args["text"] = value
end

#to=(value) ⇒ Object



36
37
38
# File 'lib/knj/mailobj.rb', line 36

def to=(value)
  @args["to"] = value.untaint
end