Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:40:14+00:00 2026-06-14T13:40:14+00:00

I have written a script to parse an email. It works fine when receiving

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:40:16+00:00Added an answer on June 14, 2026 at 1:40 pm

    Well, I found a solution myself. I will do some testing now and will let you guys now if anything fails.

    I needed to decode the body again:

    body = part.get_payload(decode=True).decode(part.get_content_charset())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a script that fires a jQuery POST to retrieve data from
I have written a script which works, but I'm guessing isn't the most efficient.
I have written a perl script to send email using the gmail's smtp server:
Hey everyone, I have written a script that downloads a zip file from a
I have a simple script written to process received email. Here are the tested
I have written the following simple script to parse reddit/r/documentaries require 'open-uri' require 'nokogiri'
I have written a python script to parse an HTML page, get some strings
I have a client-side script written in jQuery that is sending text/xml data to
I have made a custom ajax script to parse the JSON returned from the
i have written this python program. whenever i run the script using parameters like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.