-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiving_mail.py
160 lines (151 loc) · 5.21 KB
/
receiving_mail.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
# author
__author__ = "Sanjay Singh"
__email__ = "[email protected]"
# libraries
import sys
import imaplib
import getpass
import email
import time
from email.header import decode_header
# importing package files
import controller
# global variables
all_msgID = []
total_msg = 0
keep_waiting = True
M = ''
msg_payload = ''
local_program_file = "local_program_file.py"
# function for updating local_program_file
def update_local_program_file():
global local_program_file
f = open(local_program_file, "w")
f.write(controller.program)
f.close()
# cleaning received email (removing previous replies from email body)
def get_clean_msg():
global msg_payload
program = ''
chunks = msg_payload.split(">EOM<")
controller.program = chunks[0]
chunks = chunks[1].split("\n")
msg = ""
for i in range(len(chunks)):
if(chunks[i].strip() == str(">EOM<")):
break
msg = msg + chunks[i].strip() + "\n"
msg_payload = msg.strip()
if(msg_payload == '$r'):
update_local_program_file()
# function for reading new msg
def new_msg():
global all_msgID, total_msg, keep_waiting, M, msg_payload
try:
# for selecting INBOX
typ, data = M.select("INBOX")
# getting all mails inside INBOX
rv, data = M.search(None, "ALL")
# iterating one by one over all mails in INBOX
for num in data[0].split():
# getting response and data
rv, data = M.fetch(num, '(RFC822)')
# getting email content
email_content = data[0][1]
msg = email.message_from_bytes(email_content)
# getting email subject
msg_subject = str(decode_header(msg['Subject'])[0][0])
# getting Message-ID
msg_id = msg['Message-ID']
# appending msg id
if(msg_id not in all_msgID):
all_msgID.append(msg_id)
print(">> Subject: " + msg_subject + " <" + str(time.ctime()) + ">")
# getting mail text body
msg_payload = str(msg.get_payload()).strip()
# removing undesired things (like previous replies)
get_clean_msg()
print(">> Message Received: " + msg_payload)
# finish waiting for new email
keep_waiting = False
# getting total number of msgs
total_msg = len(all_msgID)
except Exception as e:
print(">> Process Failed.. <" + str(time.ctime()) + "> " + str(e))
# function for reading mailbox
def read_mailbox():
global all_msgID, total_msg, keep_waiting, M
try:
# for selecting INBOX
typ, data = M.select("INBOX")
# getting all mails inside INBOX
rv, data = M.search(None, "ALL")
# iterating one by one over all mails in INBOX
for num in data[0].split():
# getting response and data
rv, data = M.fetch(num, '(RFC822)')
# getting email content
email_content = data[0][1]
msg = email.message_from_bytes(email_content)
# getting Message-ID
msg_id = msg['Message-ID']
# appending msg id
if(msg_id not in all_msgID):
all_msgID.append(msg_id)
# getting total number of msgs
total_msg = len(all_msgID)
print(">> Message List Initialized <" + str(time.ctime()) + ">")
except Exception as e:
print(">> Process Failed.. <" + str(time.ctime()) + "> " + str(e))
# function for checking INBOX
def check_mail():
global all_msgID, total_msg, keep_waiting, M
try:
# for selecting INBOX
typ, data = M.select("INBOX")
# getting all mails inside INBOX
rv, data = M.search(None, "ALL")
# checking for initial read of mailbox
if(len(all_msgID) == 0):
# read available msgs
read_mailbox()
if(len(data[0].split()) > total_msg):
print(">> Received New Message! <" + str(time.ctime()) + ">")
new_msg()
if(len(data[0].split()) < total_msg):
print(">> Re-initializing Message List <" + str(time.ctime()) + ">")
all_msgID = []
read_mailbox()
except Exception as e:
print(">> Process Failed.. <" + str(time.ctime()) + ">" + str(e))
# main function
def main_function():
global all_msgID, total_msg, keep_waiting, M, msg_payload
# initializing global variables
all_msgID = []
total_msg = 0
keep_waiting = True
M = ''
msg_payload = ''
try:
# reading params files
params = open("params.txt", "r").read()
# separating params
params = params.split(",")
source_userid = params[0]
password = params[1]
target_userid = params[2]
subject = params[3]
# gmail email api
M = imaplib.IMAP4_SSL('imap.gmail.com')
# making login
M.login(source_userid, password)
# checking mailbox (INBOX)
print(">> Receiving Email: Running...")
while(keep_waiting == True):
check_mail()
if(keep_waiting == False):
break
time.sleep(10)
except imaplib.IMAP4.error:
print(">> LOGIN FAILED!!! <" + str(time.ctime()) + ">")