I have written a script to parse an email. It works fine when receiving letters from Mac OS X Mail client (just this one tested so far), but my parser failes when letters contain unicode letters in their body part.

For example, I have sent a message with content `ąčę`.

And here is my part of script which parses body and attachments at the same time:

```
p = FeedParser()
p.feed(msg)
msg = p.close()
attachments = []
body = None
for part in msg.walk():
  if part.get_content_type().startswith('multipart/'):
    continue
  try:
    filename = part.get_filename()
  except:
    # unicode letters in filename, set default name then
    filename = 'Mail attachment'

  if part.get_content_type() == "text/plain" and not body:
    body = part.get_payload(decode=True)
  elif filename is not None:
    content_type = part.get_content_type()
    attachments.append(ContentFile(part.get_payload(decode=True), filename))

if body is None:
    body = ''

```

Well, I mentioned that it works with letters from OS X Mail, but with Gmail letters it doesn't.

The traceback:

> Traceback (most recent call last):
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/core/handlers/base.py", line 116, in get\_response
>  response = callback(request, \*callback\_args, \*\*callback\_kwargs)
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 77, in wrapped\_view
>  return view\_func(\*args, \*\*kwargs)
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/views/decorators/http.py", line 41, in inner
>  return func(request, \*args, \*\*kwargs)
>  File "/Users/aemdy/PycharmProjects/rezervavau/bms/messages/views.py", line 66, in accept
>  Message.accept(request.POST.get('msg'))
>  File "/Users/aemdy/PycharmProjects/rezervavau/bms/messages/models.py", line 261, in accept
>  thread=thread
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/manager.py", line 149, in create
>  return self.get\_query\_set().create(\*\*kwargs)
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/query.py", line 391, in create
>  obj.save(force\_insert=True, using=self.db)
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/base.py", line 532, in save
>  force\_update=force\_update, update\_fields=update\_fields)
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/base.py", line 627, in save\_base
>  result = manager.\_insert([self], fields=fields, return\_id=update\_pk, using=using, raw=raw)
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/manager.py", line 215, in \_insert
>  return insert\_query(self.model, objs, fields, \*\*kwargs)
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/query.py", line 1633, in insert\_query
>  return query.get\_compiler(using=using).execute\_sql(return\_id)
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 920, in execute\_sql
>  cursor.execute(sql, params)
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/backends/util.py", line 47, in execute
>  sql = self.db.ops.last\_executed\_query(self.cursor, sql, params)
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/backends/postgresql\_psycopg2/operations.py", line 201, in last\_executed\_query
>  return cursor.query.decode('utf-8')
>  File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/encodings/utf\_8.py", line 16, in decode
>  return codecs.utf\_8\_decode(input, errors, True)
> UnicodeDecodeError: 'utf8' codec can't decode byte 0xe0 in position 115: invalid continuation byte

My script gives me the following body `����`. How can I decode it to get `ąčę` back?