-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgui.py
171 lines (127 loc) · 4.36 KB
/
gui.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import sys
import imaplib
import getpass
import email
import email.header
import datetime
import os
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from form import Ui_Dialog
reload(sys)
sys.setdefaultencoding('utf8')
curr_dir ='.'
EMAIL_ACCOUNT= None
EMAIL_FOLDER = "INBOX" # or "[Gmail]/All Mail" or "[Gmail]/Sent Mail"
mailinfo = []
class MyDialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.OK)
self.ui.listWidget.itemClicked.connect(self.showmessage)
def showmessage(self, item):
msgnumber = str(item.text()).split()[1]
msgindex = mailinfo.index(msgnumber)
print mailinfo[msgindex + 1]
print mailinfo[msgindex + 2]
print mailinfo[msgindex + 3]
print mailinfo[msgindex + 4]
boxy = QMessageBox()
boxy.setText('Title : %s\nFrom : %s' %((mailinfo[msgindex + 1]), (mailinfo[msgindex + 2])))
boxy.setInformativeText('%s\n\n%s attachment(s) on this message' % ((mailinfo[msgindex + 3]), (mailinfo[msgindex + 4])))
boxy.setWindowTitle('Message %s' % (mailinfo[msgindex]))
boxy.setStandardButtons(QMessageBox.Ok)
boxy.exec_()
def OK(self):
EMAIL_ACCOUNT = self.ui.lineEdit.text()
print 'OK pressed.'
self.auth(EMAIL_ACCOUNT)
def auth(self, EMAIL_ACCOUNT):
M = imaplib.IMAP4_SSL('imap.gmail.com')
try:
rv, data = M.login(EMAIL_ACCOUNT, self.ui.lineEdit_2.text())
except imaplib.IMAP4.error:
choice=QtGui.QMessageBox.question(self, 'Error', "LOGIN FAILED!!!", QtGui.QMessageBox.Retry, QtGui.QMessageBox.Exit)
if choice=QtGui.QMessageBox.Exit:
#print "LOGIN FAILED!!! "
sys.exit(1)
elif choice=QtGui.QMessageBox.Retry:
auth(self, EMAIL_ACCOUNT)
print data
search_email = self.ui.lineEdit_3.text()
curr_dir = '.'
if search_email not in os.listdir(curr_dir):
os.mkdir(search_email)
curr_dir = './'+search_email
rv, mailboxes = M.list()
if rv == 'OK':
print "Mailboxes located."
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
print "Now processing mailbox ...\n"
self.process_mailbox(M, search_email, curr_dir)
M.close()
else:
print "ERROR: Unable to open mailbox ", rv
M.logout()
def process_mailbox(self, M, search_email, curr_dir):
rv, data = M.search(None, '(OR (TO %s) (FROM %s))' % (search_email, search_email))
if rv != 'OK':
print "No messages found!"
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print "ERROR getting message", num
return
localinfo = []
print '-----'
msg = email.message_from_string(data[0][1])
decode = email.header.decode_header(msg['Subject'])[0]
subject = unicode(decode[0])
print 'Message %s : %s' % (num, subject)
self.ui.listWidget.addItem(QListWidgetItem('Message %s : %s' % (num, subject)))
localinfo.append(num)
localinfo.append(subject)
sender = msg['from'].split()[-1]
sender = sender[1:]
sender = sender[:-1]
print 'Sender : ', sender
localinfo.append(sender)
#print 'Raw Date:', msg['Date']
for part in msg.walk():
if part.get_content_type() == 'text/plain':
print part.get_payload()
localinfo.append(part.get_payload())
# Kepping up to local time
date_tuple = email.utils.parsedate_tz(msg['Date'])
if date_tuple:
local_date = datetime.datetime.fromtimestamp(
email.utils.mktime_tz(date_tuple))
print "Dated : ", \
local_date.strftime("%a, %d %b %Y %H:%M:%S")
fcount = 0
for part in msg.walk():
if(part.get('Content-Disposition' ) is not None ) :
filename = part.get_filename()
print filename
final_path= os.path.join(curr_dir + filename)
if not os.path.isfile(final_path) :
fp = open(curr_dir+"/"+(filename), 'wb')
fp.write(part.get_payload(decode=True))
fcount += 1
fp.close()
localinfo.append(fcount)
global mailinfo
mailinfo.append(localinfo)
print '%d attachment(s) fetched' %fcount
print '-----\n\n'
mailinfo = [item for sublist in mailinfo for item in sublist]
print mailinfo
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyDialog()
myapp.show()
sys.exit(app.exec_())