Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mail.message parse
#1
Hello,

I would like to read mail from a pop3 server and parse it in to a email.message object.
The program below reads the message correctly.
But the parsing fails to work correctly.
The line totalMsg.keys() is supposed to return all the keys in the message, ie "subject, to, from etc" but it only has one key in.
Please let me know if you have any idea on how to fix this.

Thanks for your help.

import poplib
import email
import email.parser
import email.policy
 
pop3server="servername"
pop3user="username"
pop3pass="password"
 
 
#connect
server=poplib.POP3(pop3server)
 
#login
server.user(pop3user)
server.pass_(pop3pass)
resp, items, octets = server.list()
 
numMsg = len(items)
 
parser=email.parser.BytesFeedParser()
 
if numMsg > 0:
    print ('You have {} new messages'.format(numMsg))
    #get first message
    header,mailmesg,octets=server.retr(1)
 
    #convert message to email.message.Message
    for j in mailmesg:
        parser.feed(j)
 
else:
    print ('No new mail')
    exit()
 
totalMsg=parser.close()
 
#print the message
print (totalMsg)
print ('keys in message:',totalMsg.keys())

Thanks solved the issue.
You have to add \r\n after each line when you feed to the parser.
#bestdocumentationever
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to query e-mail server b4iknew 3 6,061 Sep-12-2019, 06:10 AM
Last Post: buran

Forum Jump:

User Panel Messages

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