require "mail" class String def forceUTF8Encoding if RUBY_VERSION < '1.9.0' # Ruby 1.8 really only support 7 bit ASCII well. Only do the line-end # clean-up. gsub(/\r\n/, "\n") else begin # Ensure that the text has LF line ends and is UTF-8 encoded. force_encoding("UTF-8") # Ligne ajouté par FNI encode('UTF-8', :universal_newline => true) rescue # The encoding of the String is broken. Find the first broken line and # report it. lineCtr = 1 each_line do |line| begin print lineCtr ; print "\n" if lineCtr == 52 print "ici" end line.encode('UTF-8') rescue line = line.encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => '') raise Encoding::UndefinedConversionError, "UTF-8 encoding error in line #{lineCtr}: #{line}" end lineCtr += 1 end end end end end mail = Mail.read("testrb.eml.txt") mail.attachments.each do | attachment |\ filename = attachment.filename print "Found " + filename #File.open("toto.tji", "w+b", 0644) {|f| f.write attachment.decoded}\ end # print "mail body decoded:" # print mail.body.decoded print "attachment body decoded:\n" sheet=mail.attachments[0].body.decoded print sheet print "Force UTF8\n" print sheet.forceUTF8Encoding