#!/usr/local/bin/ruby
#  encoding: utf-8
#  d_commentmail.rb

#______________________________________________________________ DaybookCommentMail
class DaybookCommentMail
  include ReadWrite
 def initialize(myhash)
   @smtpserver='xxxxxxx'
   @sender = "xxxxxxxx"
   @recipient = "midorex@gmail.com"
   @subject = "Daybook_Comment"
   #-------------------------------
   @path_log_mail = myhash["f_log_mail"]
 end
 def comment_sendmail(para_data)
   body = ["\n\n",Time.now.to_s,"--\n"]
    para_data.each{|key ,value| body << ["[","#{key}", ",", "#{value}","]"].to_s }
    body << "\n"
    str_mail = p_make_body(body.to_s)

    require 'net/smtp'
    begin
      #Net::SMTP.start(@smtpserver, 25) {|smtp|
       #smtp.send_mail(str_mail, @sender, @recipient)
      #}
    rescue SocketError
       body << "++++++++++++SocketError++++++++++++" 
    rescue Errno::ECONNREFUSED, Net::ProtocolError
       body <<  "++++++++++++Error++++++++++++++++"
    end
       m_log_write_file(@path_log_mail, body)   
 end
 private
 def p_make_body(body)
   require "nkf"
   subj = NKF.nkf('-j', @subject)
   subj = subj.split(//,1).pack('m'); subj = subj.chomp
   subject_iso = "=?ISO-2022-JP?B?#{subj}?="
   body_n = NKF.nkf('-j', body.to_s)
   @mail = ""
   @mail << "From: " + @sender + "\n"
   @mail << "To: " + @recipient + "\n"
   @mail << "Subject: " + subject_iso + "\n"
   @mail << "X-Mailer: myruby"+ "\n"
   @mail << "Mime-Version: 1.0"+ "\n"
   @mail << "Content-Type: text/plain; charset=iso-2022-jp" + "\n"
   @mail << "Content-Transfer-Encoding: 7bit"+ "\n"
   @mail << "Date: " + Time.now.strftime('%a, %d %b %Y %H:%M:%S %z') + "\n"
   @mail << "\n"
   @mail << body_n
   return @mail
 end  
end