Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Links:
- # [1] https://gist.github.com/robulouski/7441883
- # [2] https://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/
- # [3] http://www.faqs.org/rfcs/rfc3501.html
- #
- # Если возникает ошибка:
- # imaplib.error: b'[ALERT] Please log in via your web browser:
- # https://support.google.com/mail/accounts/answer/78754 (Failure)'
- #
- # То для ее решения выполняем следующие действия:
- # 1. On your computer, open Gmail.
- # 2. In the top right, click Settings Settings.
- # 3. Click Settings.
- # 4. Click the Forwarding and POP/IMAP tab.
- # 5. In the "IMAP Access" section, select Enable IMAP.
- # 6. Click Save Changes.
- #
- # Потом заходим сюда https://myaccount.google.com/lesssecureapps и
- # переключаем ползунок на ON.
- #
- # Собственно, делаем все то, что написано по ссылке (
- # https://support.google.com/mail/accounts/answer/78754).
- import email
- from imaplib import IMAP4, IMAP4_SSL
- class MailDumper:
- def __init__(self, imap_host, imap_user, imap_pass, imap_port=None,
- imap_ssl=False):
- self.imap_host = imap_host
- self.imap_user = imap_user
- self.imap_pass = imap_pass
- self.imap_port = imap_port
- self.imap_ssl = imap_ssl
- self.connect()
- self.login()
- def connect(self):
- imap_cls = IMAP4_SSL if self.imap_ssl else IMAP4
- if self.imap_port is not None:
- self.client = imap_cls(self.imap_host, self.imap_port)
- else:
- self.client = imap_cls(self.imap_host)
- def login(self):
- self.client.login(self.imap_user, self.imap_pass)
- def logout(self):
- self.client.logout()
- # ...
- def get_folders(self):
- typ, data = self.client.list()
- assert(typ == 'OK')
- return data
- # ...
- def dump(self, mailbox):
- typ, data = self.client.select(mailbox, True)
- assert(typ == 'OK')
- # 'data' is count of messages in mailbox ('EXISTS' response).
- # Это точно не количество!
- print(data)
- # typ, data = self.client.search(None, 'ALL')
- # assert (typ == 'OK')
- # message_ids = data[0].split()
- # print(message_ids)
- typ, data = self.client.uid('search', None, 'ALL')
- assert(typ == 'OK')
- email_uids = data[0].split()
- # Тут нужно создавать треды и сохранять письма
- for email_uid in email_uids:
- typ, data = self.client.uid('fetch', email_uid, '(RFC822)')
- message = email.message_from_bytes(data[0][1])
- print('Subject:', message['subject'])
- print('From:', message['from'])
- for part in message.walk():
- print(part.get_content_type())
- if part.get_content_maintype() == 'text':
- # print(part.get_content_subtype())
- charset = part.get_content_charset('iso-8859-1')
- data = part.get_payload(decode=True)
- print(data.decode(charset))
- else:
- print('[Skipped]')
- print('-' * 40)
- if __name__ == '__main__':
- # Gmail requires use SSL
- dumper = MailDumper('imap.gmail.com', 'sergey.m@upsafe.com', 'wri15hp0fx',
- imap_ssl=True)
- print('Existing Folders:', dumper.get_folders())
- dumper.dump('inbox')
- dumper.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement