Пример #1
0
def mark_as_read(folder=args.folder):
    if not folder:
        raise FatalError('A folder is required!')

    server = imap_connect_and_login(config['IMAP_HOST'], config['IMAP_USER'], config['IMAP_PASS'])
    imap_select_folder(server, args.folder)
    code, data = server.search(None, '(Unseen)')

    messages = data[0].split()

    for msg in messages:
        server.store(msg, '+FLAGS', '\\Seen')
import re
import os
import email
from helpers import mail_config, imap_connect_and_login

IMAP_FOLDER = 'Purchases'
FOLDER_TO_SAVE = 'C:\\Users\\niek\\ownCloud\\Documents\\Invoices'

config = mail_config()

server = imap_connect_and_login(config['IMAP_HOST'], config['IMAP_USER'], config['IMAP_PASS'])
server.select(IMAP_FOLDER)

resp_code, data = server.search(None, '(UNSEEN)')
messages = data[0].split()

for msg in messages:
    code, data = server.fetch(msg, '(RFC822)')
    text = data[0][1]
    message = email.message_from_bytes(text)

    sender = message['From'].replace('"', '')
    name = sender[0:sender.rfind(' <')]
    sender = sender[sender.find('<') + 1:sender.rfind('>')]
    domain = sender.replace(sender[0:sender.find('@') + 1], '')

    for part in message.walk():
        if part.get_content_maintype() == 'multipart' or part.get('Content-Disposition') is None:
            server.store(msg, '-FLAGS', '\\Seen')
            continue