Skip to content

Commit 4b1caf4

Browse files
committed
edit reading emails tutorial
1 parent 7cb0c97 commit 4b1caf4

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Diff for: python-standard-library/reading-email-messages/reading_emails.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# total number of emails
2424
messages = 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:
@@ -35,10 +35,12 @@
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
@@ -83,9 +85,7 @@
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
9090
imap.close()
9191
imap.logout()

0 commit comments

Comments
 (0)