Home > Software design >  Downloading emails with UTF-8 B encoded header
Downloading emails with UTF-8 B encoded header

Time:11-09

I have a problem with a code which is supposed to download your emails in eml files.

Its supposed to go through the INBOX email listing, retrieve the email content and attachments(if any) and create an .eml file which contains all that.

What it does is that it works with content type of text and the majority multiparts. If an email in the listing contains utf-8B in its header, it simply acts like its the end of the email listing, without displaying any error.

The code in question is:

result, data = p.uid('search',None, search_criteria) # search_criteria is defined earlier in code
if result == 'OK':
  data = get_newer_emails_first(data) # get_newer_emails_first() is a function defined to return the list of UIDs in reverse order (newer first)

  context['emailsum'] = len(data) # total amount of emails based on the search_criteria parameter.
for num in data:
  mymail2 = {}
  result,data1 = p.iud('fetch', num, '(RFC822)')
  email_message = email.message_from_bytes(data[0][1])
  fullemail = email_message.as_bytes()

  default_charset = 'ASCII' 
  if email_message.is_multipart():
     m_subject = make_header(decode_header(email_message['Subject']))
  else:
     m_subject = r''.join([ six.text_type(t[0], t[1] or default_charset) for t in email.header.decode_header(email_message['Subject']) ])

m_from = string(make_header(decode_header(email_message['From'])))
m_date = email_message['Date']

I have done my tests and discovered that while the fullemail variable contains the email properly (thus it reads the data from the actual email successfully), the problem should be in the if else immediately after, but I cannot find what the problem is exactly.

Any ideas?

PS: I accidentally posted this question as a guest, but I opted to delete it and repost it from my account.

CodePudding user response:

Apparently the error lay in my code in the silliest of ways.

Instead of:

m_from = string(make_header(decode_header(email_message['From'])))
m_date = email_message['Date']

It should be:

m_from = str(make_header(decode_header(email_message['From'])))
m_date = str(make_header(decode_header(email_message['Date'])))
  • Related