Skip to content

Commit c4fe9ab

Browse files
author
James Campbell
committed
added comments properly and formatting to imaplib
1 parent 208153d commit c4fe9ab

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

imaplib-example.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import datetime
2-
import smtplib
1+
"""
2+
What: Connect and get unread mail from imap example
3+
Author: James Campbell
4+
Date: 5 August 2019
5+
"""
36
import time
47
import imaplib
58
import email
69
import sys
10+
import os
11+
import html2text
712
# -------------------------------------------------
813
#
914
# Utility to read email from Gmail Using Python
@@ -14,27 +19,24 @@
1419
FROM_PWD = sys.argv[1]
1520
SMTP_SERVER = "imap.gmail.com"
1621
SMTP_PORT = 993
17-
18-
import email
19-
import imaplib
20-
import os
21-
import html2text
22-
import time
23-
detach_dir = 'locationWhereYouWantToSaveYourAttachments'
22+
detach_dir = '~/Downloads'
2423

2524

2625
def get_body(email_message):
26+
"""Get body of email message."""
2727
for payload in email_message.get_payload():
2828
break
2929
return payload.get_payload()
3030

3131

3232
def two_way_email(server, uname, pwd):
33+
"""Fetch and read latest unseen messages."""
3334
username = uname
3435
password = pwd
36+
readonly = True
3537
mail = imaplib.IMAP4_SSL(server)
3638
mail.login(username, password)
37-
mail.select("inbox")
39+
mail.select("inbox", readonly)
3840
try:
3941
result, data = mail.uid('search', None, '(UNSEEN)')
4042
inbox_item_list = data[0].split()

0 commit comments

Comments
 (0)