title: How can I get an email message's text content using Python?
body: <p>Given an RFC822 message in Python 2.6, how can I get the <em>right</em> text/plain content part?  Basically, the algorithm I want is this:</p>
  
  <pre><code>message = email.message_from_string(raw_message)
  if has_mime_part(message, "text/plain"):
      mime_part = get_mime_part(message, "text/plain")
      text_content = decode_mime_part(mime_part)
  elif has_mime_part(message, "text/html"):
      mime_part = get_mime_part(message, "text/html")
      html = decode_mime_part(mime_part)
      text_content = render_html_to_plaintext(html)
  else:
      # fallback
      text_content = str(message)
  return text_content
  </code></pre>
  
  <p>Of these things, I have <code>get_mime_part</code> and <code>has_mime_part</code> down pat, but I'm not quite sure how to get the decoded text from the MIME part.  I can get the <em>encoded</em> text using <code>get_payload()</code>, but if I try to use the <code>decode</code> parameter of the <code>get_payload()</code> method (see <a href="http://docs.python.org/library/email.message.html#email.message.Message.get_payload" rel="noreferrer">the doc</a>) I get an error when I call it on the text/plain part:</p>
  
  <blockquote>
  <pre><code>File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/
  email/message.py", line 189, in get_payload
      raise TypeError('Expected list, got %s' % type(self._payload))
  TypeError: Expected list, got &lt;type 'str'&gt;
  </code></pre>
  </blockquote>
  
  <p>In addition, I don't know how to take HTML and render it to text as closely as possible.</p>