Python Forum

Full Version: Best replacement for pyzmail in lines 15 and 16
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following to collect emails and write the text to a file. It works. I know they say "If it ain't broke don't fix it." However I've been told pyzmail is old, maybe as Python renews one day it will not work. So I thought I would try and use the module email or imaplib. I import the following at present:

Quote:import sys, os
import pyzmail
import email
from imapclient import IMAPClient
from shutil import copy2



for uid, message_data in server.fetch(unseenMessages, 'RFC822').items():
        email_message = email.message_from_bytes(message_data[b'RFC822'])
        print('UID is ' + str(uid))
        print(email_message.get('Subject'))
        messageSubject = str(email_message.get('Subject'))
        print('Message subject is ' + messageSubject)
        messageBody = str(email_message.get('Body'))
        print('Message body is ' + messageBody)
        #if not len(messageSubject) == 15:
            #continue
        file = messageSubject + '.txt'
        theFile = open(path + file, 'w')
        rawMessage = server.fetch(unseenMessages, ['BODY[]', 'FLAGS'])
        try:
            message = pyzmail.PyzMessage.factory(rawMessage[uid][b'BODY[]'])
            text = message.text_part.get_payload().decode(message.text_part.charset)
            saveText = text.rstrip()
            theFile.write(saveText)
            theFile.close()
        except AttributeError:
            continue
Any tips please about the best way to replace lines 15 and 16? I've been reading the email "get_payload([i[, decode]])" documentation, but I can't really see how it works in practice. I have tried in a shell, but so far no joy, If you know of any links to a simple working example, please let me know.