File tree Expand file tree Collapse file tree 1 file changed +6
-6
lines changed
python-standard-library/reading-email-messages Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change 2323# total number of emails
2424messages = int (messages [0 ])
2525
26- for i in range (messages - 4 , messages - N - 4 , - 1 ):
26+ for i in range (messages , messages - N , - 1 ):
2727 # fetch the email message by ID
2828 res , msg = imap .fetch (str (i ), "(RFC822)" )
2929 for response in msg :
3535 if isinstance (subject , bytes ):
3636 # if it's a bytes, decode to str
3737 subject = subject .decode ()
38- # email sender
39- from_ = msg .get ("From" )
38+ # decode email sender
39+ From , encoding = decode_header (msg .get ("From" ))[0 ]
40+ if isinstance (From , bytes ):
41+ From = From .decode (encoding )
4042 print ("Subject:" , subject )
41- print ("From:" , from_ )
43+ print ("From:" , From )
4244 # if the email message is multipart
4345 if msg .is_multipart ():
4446 # iterate over email parts
8385 open (filepath , "w" ).write (body )
8486 # open in the default browser
8587 webbrowser .open (filepath )
86-
8788 print ("=" * 100 )
88-
8989# close the connection and logout
9090imap .close ()
9191imap .logout ()
You can’t perform that action at this time.
0 commit comments