Python Forum
Best replacement for pyzmail in lines 15 and 16
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best replacement for pyzmail in lines 15 and 16
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  String replacement in DB WJSwan 0 713 Dec-28-2022, 05:31 AM
Last Post: WJSwan
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,973 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Extract the largest value from a group without replacement (beginner) preliator 1 2,039 Aug-12-2020, 01:56 PM
Last Post: DPaul
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,707 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Simple automated SoapAPI Call with single variable replacement from csv asaxty 1 2,058 Jun-30-2020, 06:38 PM
Last Post: asaxty
  line replacement help mdalireza 8 3,357 Nov-11-2019, 12:54 PM
Last Post: mdalireza
  xml replacement with python josesalazmit 3 8,318 Feb-24-2019, 07:28 PM
Last Post: stullis
  Is pathlib a viable replacement for os.path? j.crater 4 9,909 Jan-13-2018, 09:49 AM
Last Post: Gribouillis
  Using python for text replacement omar 1 3,785 Dec-22-2016, 01:36 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020