Ejemplo n.º 1
0
def file_imap(pmail):
    """
    Получение объекта письма.
    """
    title = get_title(pmail)
    date_ = get_date(pmail)
    from_ = get_from(pmail)
    to_ = get_to(pmail)

    for part in pmail.walk():
        if part.get_content_maintype() == 'multipart':
            continue
        if part.get('Content-Disposition') is None:
            continue
        filename = decode(part.get_filename())
        print "FIND FILE - ", filename
        if bool(filename):
            filename = name_for_file(part, DIR_ATTACH)
            filepath = os.path.join(DIR_ATTACH, filename)
            if not os.path.isfile(filepath):
                print filename
            fp = open(filepath, 'wb')
            fp.write(part.get_payload(decode=True))
            fp.close()

            m = MailObject(title=title, date_=date_, from_=from_, to_=to_,
                           file_=filepath)

            return m
Ejemplo n.º 2
0
def files_imap(pmail):
    title = get_title(pmail)
    date_ = get_date(pmail)
    from_ = get_from(pmail)
    to_ = get_to(pmail)

    text = ""
    files = []

    # body = email.message_from_file(pmail)

    for part in pmail.walk():

        if (part.get_content_maintype() == "text" and
                part.get_content_type() == "text/plain"):
            text = part.get_payload(decode=True)
            try:
                text = text.decode("windows-1251")
            except UnicodeEncodeError:
                pass

        if part.get_content_maintype() == 'multipart':
            continue

        if part.get('Content-Disposition') is None:
            continue

        filename = decode(part.get_filename())
        filename = rus_to_eng(filename)

        print "FIND FILE - ", filename

        if bool(filename):
            filename = name_for_file(part, PATH_TO_GENERATE_INVOICE)
            filepath = os.path.join(PATH_TO_GENERATE_INVOICE, filename)
            link = os.path.join(PATH_WEB, filename)

            if not os.path.isfile(filepath):
                print filename

            fp = open(filepath, 'wb')
            fp.write(part.get_payload(decode=True))
            fp.close()
            files.append({'name': filename, 'path': filepath, 'link': link})

    m = MailObjectNew(title=title, date_=date_, from_=from_, to_=to_,
                      files=files, text=text)

    return m