<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -115,6 +115,7 @@ class Message
     @mail.subject
   end
 
+
   def body
     @body ||= begin
       if @mail.multipart?
@@ -123,8 +124,9 @@ class Message
         scan_parts(@mail)
         @body = @body.join(&quot;\n&quot;)
       else
-        @mail.body
+        @body = @mail.body
       end
+      @mail.charset ? @body : convert_to_utf8(@body)
     end
   end
 
@@ -224,4 +226,19 @@ protected
     }
     s
   end
+
+  # Attempts to run iconv conversions in common charsets to UTF-8.  Needed for 
+  # those crappy emails that don't properly specify a charset in the headers. 
+  ICONV_CONVERSIONS = %w(utf-8 ISO-8859-1 ISO-8859-2 ISO-8859-3 ISO-8859-4 ISO-8859-5 ISO-8859-6 ISO-8859-7 ISO-8859-8 ISO-8859-9
+    ISO-8859-15 GB2312)
+  def convert_to_utf8(s)
+    ICONV_CONVERSIONS.each do |from|
+      begin
+        return Iconv.iconv(ICONV_CONVERSIONS[0], from, s).to_s
+      rescue Iconv::IllegalSequence
+      ensure
+        s
+      end
+    end
+  end
 end
\ No newline at end of file</diff>
      <filename>app/models/message.rb</filename>
    </modified>
    <modified>
      <diff>@@ -138,7 +138,7 @@ describe Message do
         @message.body.should == &quot;Dear Sirs, \r\nWe are given to understand that you are  Manufacturer of  plstic  Bottles\r\nAdd&#65306; blah China&quot;
       end
     end
-    
+
     describe &quot;utf-8 encoded headers&quot; do
       before :all do
         @raw     = mail(&quot;utf-8&quot;)</diff>
      <filename>spec/models/message_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>827e038468ebff6f936430f2aa9e315ad302304c</id>
    </parent>
  </parents>
  <author>
    <name>rick</name>
    <email>technoweenie@gmail.com</email>
  </author>
  <url>http://github.com/entp/astrotrain/commit/3df35c6580bcb4c84848c5b1115e8928ea3da288</url>
  <id>3df35c6580bcb4c84848c5b1115e8928ea3da288</id>
  <committed-date>2009-07-06T15:54:32-07:00</committed-date>
  <authored-date>2009-07-06T15:54:32-07:00</authored-date>
  <message>attempt to convert mail bodies with bad/unspecified charsets into utf-8</message>
  <tree>3e981228515f68988b671db1e4b4a096f25eb446</tree>
  <committer>
    <name>rick</name>
    <email>technoweenie@gmail.com</email>
  </committer>
</commit>
