-
Notifications
You must be signed in to change notification settings - Fork 0
/
email-list.py
119 lines (89 loc) · 3.84 KB
/
email-list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import smtplib
import time
import imaplib
import email
import sys
import tempfile
#Opens and reads any emails from Gmail
#In order to use fill in the required fields
#Make sure you adjust Gmail settings accordingly
#by going to settings -> forwarding and POP/IMAP -> enable IMAP
#and also allow access for less secure apps.
def read_email_from_gmail():
FROM_EMAIL = "[email protected]" #Enter the email name
FROM_PWD = "emailonvoice" #Enter email password
SMTP_SERVER = "imap.gmail.com"
NUM_TO_READ = 5 #Replace with number of earliest emails desired
try:
#Establish a connection and login to the Gmail account
mail = imaplib.IMAP4_SSL(SMTP_SERVER)
mail.login(FROM_EMAIL,FROM_PWD)
#Look for all emails in the inbox
mail.select('inbox')
type, data = mail.search(None, 'ALL')
x = 0
idList = []
#Get a list of all the email ids, reverse it so that
#the newest ones are at the front of the list
for id in data[0].rsplit():
idList.append(id)
idList = list(reversed(idList))
#Fetch the first NUM_to_READ email subject lines and
#their recipients
for id in idList:
typ, data = mail.fetch(id, '(RFC822)')
if x >= NUM_TO_READ:
break
else:
x += 1
msg = email.message_from_bytes(data[0][1])
print('Message #', x)
email_subject = msg['subject']
email_from = msg['from']
print('From : ' + email_from)
print('Subject : ' + email_subject + '\n')
#Allow the user to read the content of any emails
while(1):
message_to_read = input("Which message would you like to open? (-1 to quit) ")
message_to_read = int(message_to_read)
#Basic error checking
while (message_to_read < 1) or (message_to_read > NUM_TO_READ):
if (message_to_read == -1):
sys.exit()
print("Please enter a valid message #")
message_to_read = input("Which message would you like to open?")
#Parse the desired email
typ, data = mail.fetch(idList[message_to_read-1], '(RFC822)')
msg = email.message_from_bytes(data[0][1])
#print(msg)
print('\nReading message:\n')
for part in msg.walk():
if part.get_content_type() == "text/plain":
#print(part.get_payload(decode=True).decode('utf-8'))
file = part.get_payload(decode=True).decode('utf-8')
#print(file)
for line in file:
print(line, end = '', flush = True)
print('\n')
#Create a temp file to write email contents to
#and then read from the temporary file
#Note we can replace all the file code with a print statement
#and disregard the whole issue with the temp file
#This was simply for the purpose of using a tempfile with python
'''
tmp = tempfile.NamedTemporaryFile()
with open(tmp.name, 'w') as f:
for part in msg.walk():
if part.get_content_type() == "text/plain":
#print(part.get_payload(decode=True).decode('utf-8'))
file = part.get_payload(decode=True).decode('utf-8')
print(file)
f.write(part.get_payload(decode=True).decode('utf-8'))
with open(tmp.name, 'r') as f:
f.seek(0)
for line in f:
print(line, end = '', flush = True)
'''
except:
sys.exit()
read_email_from_gmail()