Class: Keystone::Mail::Send

Inherits:
Object show all
Defined in:
lib/keystone/mail/send.rb,
lib/keystone/mail__old/send.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, opt = {}) ⇒ Send

普通なことをしたいならTMailを使いましょう

class Send


17
18
19
20
21
22
23
# File 'lib/keystone/mail__old/send.rb', line 17

def initialize(message=nil,opt={})
  @smtp_addr, @smtp_port, @retry_cnt = "127.0.0.1", 25, 0
  @message = message
  @message = Keystone::Mail::MessageFactory.create(opt) if @message == nil
  Keystone::Base::Logger.instance.debug "@message=#{@message}"
  set_option(opt)
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



12
13
14
# File 'lib/keystone/mail__old/send.rb', line 12

def message
  @message
end

#retry_cntObject

Returns the value of attribute retry_cnt.



12
13
14
# File 'lib/keystone/mail__old/send.rb', line 12

def retry_cnt
  @retry_cnt
end

#smtp_addrObject

Returns the value of attribute smtp_addr.



12
13
14
# File 'lib/keystone/mail__old/send.rb', line 12

def smtp_addr
  @smtp_addr
end

#smtp_portObject

Returns the value of attribute smtp_port.



12
13
14
# File 'lib/keystone/mail__old/send.rb', line 12

def smtp_port
  @smtp_port
end

Class Method Details

.send(from, to, subject, body, opt = {}) ⇒ Object

from could be String or Array opt

:smtp_addr 
:smtp_port 
:retry
:mail_from_text 
:encoding


34
35
36
37
38
39
40
41
# File 'lib/keystone/mail__old/send.rb', line 34

def self.send(from, to, subject, body, opt={})
  queue = self.new(nil,opt)
  queue.message.mail_to = to
  queue.message.mail_from = from
  queue.message.subject = subject
  queue.message.body = body
  queue.send
end

.sendmail(from, to, subject, body, host = "localhost", port = 25) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/keystone/mail/send.rb', line 13

def self.sendmail(from, to, subject, body, host = "localhost", port = 25)
  body = <<EOT
From: #{from}
To: #{to.to_a.join(",\n ")}
Subject: #{NKF.nkf("-WMm0j", subject)}
Date: #{Time::now.strftime("%a, %d %b %Y %X %z")}
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-2022-JP
Content-Transfer-Encoding: 7bit

#{body}
EOT

  body = NKF.nkf("-Wj", body).force_encoding("ASCII-8BIT")

  Net::SMTP.start(host, port) do |smtp|
    smtp.send_mail body, from, to
  end
end

Instance Method Details

#sendObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/keystone/mail__old/send.rb', line 43

def send
  src = @message.to_src
  Keystone::Base::Logger.instance.debug src.encoding
  Keystone::Base::Logger.instance.debug src
  try_cnt = 0
  begin
    m = Net::SMTPSession.new(@smtp_addr, @smtp_port)
    m.start()
    mail_to = []
    @message.mail_to.each{|m_to|mail_to << m_to.encode("US-ASCII")}
    mail_from = @message.mail_from.encode("US-ASCII")
    
    m.sendmail(src ,mail_from ,mail_to)
    m.finish
  rescue => e
    Keystone::Base::Logger.instance.debug "try_cnt:#{try_cnt}"
    try_cnt += 1
    sleep 1
    retry if @retry_cnt >= try_cnt
    raise e
  end
end

#set_option(opt) ⇒ Object



66
67
68
69
70
# File 'lib/keystone/mail__old/send.rb', line 66

def set_option(opt)
  @smtp_addr = opt[:smtp_addr] if opt.key?(:smtp_addr)
  @smtp_port = opt[:smtp_port] if opt.key?(:smtp_port)
  @retry_cnt = Integer(opt[:retry_cnt]) if opt.key?(:retry_cnt)
end