Python Forum
Best replacement for pyzmail in lines 15 and 16 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Best replacement for pyzmail in lines 15 and 16 (/thread-13842.html)



Best replacement for pyzmail in lines 15 and 16 - Pedroski55 - Nov-03-2018

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.