Использую данный код для чтения сообщений по почте
import imaplib
import time
import uuid
from email import email
IMAP_SERVER = 'imap.gmail.com'
IMAP_PORT = '993'
IMAP_USE_SSL = True
class MailBox(object):
def __init__(self, user, password):
self.user = user
self.password = password
if IMAP_USE_SSL:
self.imap = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
else:
self.imap = imaplib.IMAP4(IMAP_SERVER, IMAP_PORT)
def __enter__(self):
self.imap.login(self.user, self.password)
return self
def __exit__(self, type, value, traceback):
self.imap.close()
self.imap.logout()
def get_count(self):
self.imap.select('Inbox')
status, data = self.imap.search(None, 'ALL')
return sum(1 for num in data[0].split())
def fetch_message(self, num):
self.imap.select('Inbox')
status, data = self.imap.fetch(str(num), '(RFC822)')
email_msg = email.message_from_string(data[0][1])
return email_msg
def delete_message(self, num):
self.imap.select('Inbox')
self.imap.store(num, '+FLAGS', r'\Deleted')
self.imap.expunge()
def delete_all(self):
self.imap.select('Inbox')
status, data = self.imap.search(None, 'ALL')
for num in data[0].split():
self.imap.store(num, '+FLAGS', r'\Deleted')
self.imap.expunge()
def print_msgs(self):
self.imap.select('Inbox')
status, data = self.imap.search(None, 'ALL')
for num in reversed(data[0].split()):
status, data = self.imap.fetch(num, '(RFC822)')
print 'Message %s\n%s\n' % (num, data[0][1])
def get_latest_email_sent_to(self, email_address, timeout=300, poll=1):
start_time = time.time()
while ((time.time() - start_time) < timeout):
# It's no use continuing until we've successfully selected
# the inbox. And if we don't select it on each iteration
# before searching, we get intermittent failures.
status, data = self.imap.select('Inbox')
if status != 'OK':
time.sleep(poll)
continue
status, data = self.imap.search(None, 'TO', email_address)
data = [d for d in data if d is not None]
if status == 'OK' and data:
for num in reversed(data[0].split()):
status, data = self.imap.fetch(num, '(RFC822)')
email_msg = email.message_from_string(data[0][1])
return email_msg
time.sleep(poll)
raise AssertionError("No email sent to '%s' found in inbox "
"after polling for %s seconds." % (email_address, timeout))
def delete_msgs_sent_to(self, email_address):
self.imap.select('Inbox')
status, data = self.imap.search(None, 'TO', email_address)
if status == 'OK':
for num in reversed(data[0].split()):
status, data = self.imap.fetch(num, '(RFC822)')
self.imap.store(num, '+FLAGS', r'\Deleted')
self.imap.expunge()
if __name__ == '__main__':
# example:
imap_username = 'mail@gmail.com'
imap_password = 'password'
with MailBox(imap_username, imap_password) as mbox:
print mbox.get_count()
print mbox.print_msgs()
И никак не получается получить адекватное тело сообщения. Мне нужно узнать присутствует ли в нем необходимая информация.
При считывании получаю огромную кучу символов и чисел не связанный между собой.
CxBgABJ8Mj5zMIszAAAAAElFTkSuQmCC
----boundary_4508021_c8f9c33c-eb92-45b9-9eef-da107b5ef321
Content-Type: image/png
Content-Transfer-Encoding: base64
Content-ID: <07d79abf-0f06-4a90-a524-473f57084ff3>
iVBORw0KGgoAAAANSUhEUgAAABMAAAAUCAYAAABvVQZ0AAAAGXRFWHRTb2Z0d2FyZQBB
ZG9iZSBJbWFnZVJlYWR5ccllPAAAA4RpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/
eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+
IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2Jl
IFhNUCBDb3JlIDUuNi1jMTExIDc5LjE1ODMyNSwgMjAxNS8wOS8xMC0wMToxMDoyMCAg
ICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5
LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9
IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHht
bG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3Vy
Y2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly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izejQ9JUBTelSeAx5AMS5
QIO24Cw1gHg9EF8G4nggdgFibSDmhUbIIyA+BcRrQeqABv3C53fGQVs4UtUwgAADAIKW
VJzjGh/3AAAAAElFTkSuQmCC
----boundary_4508021_c8f9c33c-eb92-45b9-9eef-da107b5ef321--
Как понимаю, нужно распарсить
----boundary_4508021_c8f9c33c-eb92-45b9-9eef-da107b5ef321--
Посоветуйте как можно достучаться до необходимой инфы
Заранее благодарю