単件のメールを読むライブラリ

 #!/usr/bin/ruby -Ke
 require 'mailread'
 require 'nkf'
 class Mailread
   def initialize(file='')
     @mail=Mail.new(file)
   end
   def decode(s='')
     NKF.nkf('-me',s).map{|i|
       i.gsub(&#39;&&#39;,&#39;&&#39;).gsub(&#39;<&#39;,&#39;<&#39;).gsub(&#39;>&#39;,&#39;>&#39;) #.gsub(&#39;"&#39;,&#39;%quot;&#39;)
     }.join
   end
   def to_html
     @from=decode(@mail.header.delete("From"))
     @date=@mail.header.delete("Date")
     @to=decode(@mail.header.delete("To"))
     @subject=decode(@mail.header.delete("Subject").gsub(/\n/,&#39;&#39;))
     r = "<table>\n"
     r+="<tr><td>差出人</td><td>#{@from}</td></tr>\n"
     r+="<tr><td>日付</td><td>#{@date}</td></tr>\n"
     r+="<tr><td>宛先</td><td>#{@to}</td></tr>\n"
     r+="<tr><td>件名</td><td>#{@subject}</td></tr>\n"
     r += "</table>\n"
     @mail.header.each_pair{|k,v|r+="#{k}: #{decode(v).gsub(/\n/,&#39;<br>  &#39;)}<br>\n"}
     r += "<hr>\n"
     r += NKF.nkf(&#39;-e&#39;,@mail.body.map{|i|i.gsub(/\n/,&#39;&#39;)}.join("<br>\n"))
     r += "<hr>\n"
     r
   end
 end
 if __FILE__==$0                 # ライブラリテスト用コード
   puts Mailread.new(&#39;/home/n9d/Maildir/cur/1069475291.24669.binran2:2,RS&#39;).to_html
 end
 #!/usr/bin/ruby -Ke
 require &#39;./mailread.rb&#39;
 require &#39;cgi&#39;
 cgi=CGI.new
 print "content-type: text/html\n"
 print "\n\n"
 print %Q(<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.d\
 td">)
 print %Q(<html lang="ja-JP">)
 print %Q(<head>\n<meta http-equiv="content-type" content="text/html; charset=EUC-JP">\n<title>)
 puts &#39;</title>&#39;
 print "</head>\n<body>\n"
 if cgi.has_key?(&#39;f&#39;)
   puts Mailread.new(cgi[&#39;f&#39;][0]).to_html
 end