#!/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('&','&').gsub('<','<').gsub('>','>') #.gsub('"','%quot;')
}.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/,''))
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/,'<br> ')}<br>\n"}
r += "<hr>\n"
r += NKF.nkf('-e',@mail.body.map{|i|i.gsub(/\n/,'')}.join("<br>\n"))
r += "<hr>\n"
r
end
end
if __FILE__==$0 # ライブラリテスト用コード
puts Mailread.new('/home/n9d/Maildir/cur/1069475291.24669.binran2:2,RS').to_html
end
#!/usr/bin/ruby -Ke
require './mailread.rb'
require 'cgi'
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 '</title>'
print "</head>\n<body>\n"
if cgi.has_key?('f')
puts Mailread.new(cgi['f'][0]).to_html
end