File tree 1 file changed +6
-6
lines changed
python-standard-library/reading-email-messages
1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change 23
23
# total number of emails
24
24
messages = int (messages [0 ])
25
25
26
- for i in range (messages - 4 , messages - N - 4 , - 1 ):
26
+ for i in range (messages , messages - N , - 1 ):
27
27
# fetch the email message by ID
28
28
res , msg = imap .fetch (str (i ), "(RFC822)" )
29
29
for response in msg :
35
35
if isinstance (subject , bytes ):
36
36
# if it's a bytes, decode to str
37
37
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 )
40
42
print ("Subject:" , subject )
41
- print ("From:" , from_ )
43
+ print ("From:" , From )
42
44
# if the email message is multipart
43
45
if msg .is_multipart ():
44
46
# iterate over email parts
83
85
open (filepath , "w" ).write (body )
84
86
# open in the default browser
85
87
webbrowser .open (filepath )
86
-
87
88
print ("=" * 100 )
88
-
89
89
# close the connection and logout
90
90
imap .close ()
91
91
imap .logout ()
You can’t perform that action at this time.
0 commit comments