Problem with coding of quoted and printed mail in Python

I am extracting emails from Gmail using the following:

def getMsgs():
 try:
    conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
  except:
    print 'Failed to connect'
    print 'Is your internet connection working?'
    sys.exit()
  try:
    conn.login(username, password)
  except:
    print 'Failed to login'
    print 'Is the username and password correct?'
    sys.exit()

  conn.select('Inbox')
  # typ, data = conn.search(None, '(UNSEEN SUBJECT "%s")' % subject)
  typ, data = conn.search(None, '(SUBJECT "%s")' % subject)
  for num in data[0].split():
    typ, data = conn.fetch(num, '(RFC822)')
    msg = email.message_from_string(data[0][1])
    yield walkMsg(msg)

def walkMsg(msg):
  for part in msg.walk():
    if part.get_content_type() != "text/plain":
      continue
    return part.get_payload()

However, some emails I receive almost prevent me from extracting dates (using a regular expression) from encoding characters such as "=" randomly placed in the middle of various text fields. Here is an example where this happens in the date range that I want to extract:

Name: KIRSTI Email: kirsti@blah.blah Phone: + 999 99995192 Total in the party: 4 in total, 0 children Arrival / departure: October 9 =, 2010 - October 13, 2010 - October 13, 2010

Is there any way to remove these encoding characters?

+4
3

/ email.parser , ( !):

from email.parser import FeedParser
f = FeedParser()
f.feed("<insert mail message here, including all headers>")
rootMessage = f.close()

# Now you can access the message and its submessages (if it multipart)
print rootMessage.is_multipart()

# Or check for errors
print rootMessage.defects

# If it a multipart message, you can get the first submessage and then its payload
# (i.e. content) like so:
rootMessage.get_payload(0).get_payload(decode=True)

" Message.get_payload " Message.get_payload, (, , ).

+5

, . , - quopri.decodestring - http://docs.python.org/library/quopri.html

+2

Python3.6 , email.message.Message.get_content() . get_payload(), get_payload() .

, s ( ):

Subject: Ayons asperges pour le =?utf-8?q?d=C3=A9jeuner?=
From: =?utf-8?q?Pep=C3=A9?= Le Pew <pepe@example.com>
To: Penelope Pussycat <penelope@example.com>,
 Fabrette Pussycat <fabrette@example.com>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

    Salut!

    Cela ressemble =C3=A0 un excellent recipie[1] d=C3=A9jeuner.

    [1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718

    --Pep=C3=A9
   =20

-ascii quoted-printable quoted-printable, Content-Transfer-Encoding.

:

import email
from email import policy

msg = email.message_from_string(s, policy=policy.default)

; policy.compat32, , get_content. policy.default , Python3.7 policy.compat32.

The method get_content()handles decoding automatically:

print(msg.get_content())

Salut!

Cela ressemble Γ  un excellent recipie[1] dΓ©jeuner.

[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718

--PepΓ©

If you have a get_content()message, get_content()you must call for individual parts, for example like this:

for part in message.iter_parts():
    print(part.get_content())
0
source

Source: https://habr.com/ru/post/1771866/


All Articles