lib/keystone/mail/send.rb in keystone-0.0.29 vs lib/keystone/mail/send.rb in keystone-0.0.30

- old
+ new

@@ -1,83 +1,35 @@ +# -*- coding: utf-8 -*- require 'net/smtp' +require "nkf" if __FILE__ ==$0 $: << '../..' require 'keystone' end module Keystone module Mail class Send - attr_accessor :message, :smtp_addr, :smtp_port, :retry_cnt + 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 - # - # 普通なことをしたいならTMailを使いましょう - # class Send - 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 - debug "@message=#{@message}" - set_option(opt) - end +#{body} +EOT - # - # from could be String or Array - # opt - # :smtp_addr - # :smtp_port - # :retry - # :mail_from_text - # :encoding - # - 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 - - def send - src = @message.to_src - debug src - try_cnt = 0 - begin - m = Net::SMTPSession.new(@smtp_addr, @smtp_port) - m.start() - m.sendmail(src ,@message.mail_from ,@message.mail_to) - m.finish - rescue => e - debug "try_cnt:#{try_cnt}" - try_cnt += 1 - sleep 1 - retry if @retry_cnt >= try_cnt - raise e + body = NKF.nkf("-Wj", body).force_encoding("ASCII-8BIT") + + Net::SMTP.start(host, port) do |smtp| + smtp.send_mail body, from, to end end - - 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 end end -end - -if __FILE__ ==$0 - require 'rubygems' - require 'pit' - - config = Pit.get("keystone_test") - - Keystone::Mail::Send.send( - config["mailfrom01"], - config["mailto01"], - "件名かんさいーーーーでんきほーーーあんky−−−かいいい−−かいいい−−かいいいいいいいい終わり", - "body日本語", - {:mail_from_text=>"メール送信者名ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー終わり",:retry_cnt=>3,:encoding=>:sjis} - ) end