-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessEmail.py
156 lines (137 loc) · 5.43 KB
/
ProcessEmail.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
#!/usr/bin/env python
'''
This program is designed to run constantly. Receiving emails, adding them to
the models and sending a response email out.
'''
from Message import Message
import email
import getpass
import imaplib
import smtplib
import time
import warnings
import main
class ProcessEmail:
def __init__(self, path=None, corpusName=None):
self.login()
self.path = path
# self.path = "/Users/zschiller/Desktop/Clinton/"
self.corpusName = corpusName
# self.corpusName = "ClintonCorpus"
run = True
last_email_id = 0
self.models = None
while run:
warnings.filterwarnings("ignore")
# print self.mail.list()
# Out: list of "folders" aka labels in gmail.
self.mail.select("inbox") # connect to inbox.
result, data = self.mail.search(None, "ALL")
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest
result, data = self.mail.fetch(latest_email_id, "(RFC822)")
msg = email.message_from_string(data[0][1])
msgSubject = msg['subject']
msgFrom = msg['from']
if ((self.user not in msgFrom) and
("AMR Response:" not in msgSubject) and
(last_email_id != latest_email_id)):
last_email_id = latest_email_id
# fetch the email body (RFC822) for the given ID
msg = email.message_from_string(data[0][1])
fout = open('lastEmail.txt', 'w')
fout.write(str(msg))
fout.close()
replied = self.createResponse()
if replied:
print ("The time is " + str(time.strftime("%I:%M:%S")) +
". AMR response created and sent.")
else:
print ("The time is " + str(time.strftime("%I:%M:%S")) +
". AMR match percentage is too low to send.")
else:
print ("The time is " + str(time.strftime("%I:%M:%S")) +
". No new mail. Will check again in 30 seconds.")
time.sleep(30)
def send(self, message, subject):
server = 'smtp.gmail.com'
recipient = self.user
sender = self.user
newMessage = "To: " + recipient + "\n" + "From: " + \
sender + "\n" + "Subject: " + subject + "\n" + message
session = smtplib.SMTP(server)
session.ehlo()
session.starttls()
session.login(self.user, self.pswd)
session.sendmail(sender, recipient, newMessage)
def login(self):
''' Get login info from user '''
# self.user = "[email protected]"
self.user = raw_input("Please enter your email address: ")
self.pswd = getpass.getpass("Please enter your password: ")
self.mail = imaplib.IMAP4_SSL('imap.gmail.com')
self.mail.login(self.user, self.pswd)
def createResponse(self):
''' Get response from similarity check. If good, send reply '''
new = Message('lastEmail.txt')
# print "Incoming Mail: " + '\n' + new.getBody()
subject = new.getSubject()
subject = "AMR Response: " + subject
# print new.getBody()
tool = 's' # Chose g for Gensim or s for Scikit Learn
modelToUse = "lda"
if tool == 'g':
match, accuracy, self.models = main.emailCheck(
self.path, self.corpusName, modelToUse, "gensim",
new, self.models)
elif tool == 's':
match, accuracy, self.models = main.emailCheck(
self.path, self.corpusName, modelToUse, "scikit",
new, self.models)
else:
match, accuracy = None, None
if match is not None:
# print "Outgoing Mail: " + '\n' + match.getBody()
reply = self.buildReply(match, accuracy)
# print reply
self.send(reply, subject)
return True
return False
def buildReply(self, match, accuracy):
''' Create text of reply email '''
if match.getDate() is None:
date = ""
else:
date = match.getDate()
if match.getSubject() is None:
subj = ""
else:
subj = match.getSubject()
if match.getToAddress() is None:
toAddr = ""
else:
toAddr = match.getToAddress()
if match.getFromAddress() is None:
fromAddr = ""
else:
fromAddr = match.getFromAddress()
if match.getOriginalBody() is None:
origBody = ""
else:
origBody = match.getOriginalBody()
reply = ("This is an AMR response! The accuracy of this match is " +
str(accuracy) + '%\n\n' +
"Date: " + date + '\n' +
"Subject: " + subj + '\n' +
"To: " + toAddr + '\n' +
"From: " + fromAddr + '\n\n' +
origBody)
# print match.getTokens()
return reply
if __name__ == '__main__':
path = raw_input("Please enter the location that you saved your emails to "
"(ex: /users/you/Documents/myEmails/): ")
corpus = raw_input(
"Please enter what the name of your corpus text (i.e. myEmails): ")
proc = ProcessEmail(path, corpus)