-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReceiveMail.py
117 lines (88 loc) · 3.76 KB
/
ReceiveMail.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
# coding: utf-8
# ! /usr/bin/python
__author__ = 'Shahariar Rabby'
# main recive mail file. All recive mail, check mail, server file import it
# All function define here
# # Recive Mail
# #### Importing all dependency
# In[ ]:
import email
import imaplib
import ctypes
import getpass
import threading
try:
from playsound import playsound
except:
print "Playsound can not found"
# #### User Details Function
# In[ ]:
def user():
# ORG_EMAIL = "@gmail.com"
# FROM_EMAIL = "your email" + ORG_EMAIL
# FROM_PWD = "your pass"
FROM_EMAIL = raw_input("Insert Your Email : ")
#FROM_PWD = getpass.getpass("Input Your Password : ") # getpass is not working in my terminal, try yours too
FROM_PWD = raw_input("Input Your Password : ")
return FROM_EMAIL,FROM_PWD
# ### Login function
# In this function we call user details function and get the user name and password, Than we use those details for IMAP login.
# ** IMAP (Internet Message Access Protocol) is a standard email protocol that stores email messages on a mail server, but allows the end user to view and manipulate the messages as though they were stored locally on the end user's computing device(s).**
# In[ ]:
def login():
FROM_EMAIL,FROM_PWD = user()
mail = imaplib.IMAP4_SSL('imap.gmail.com', 993)
mail.login(FROM_EMAIL, FROM_PWD)
mail.select("INBOX")
print 'Login successful'
return mail
# In[ ]:
mail = login()
def read_email_from_gmail(read=10):
try:
type, data = mail.search(None, 'ALL') #Sharching all message frm inbox
mail_ids = data[0] #Assining all mail id to mail_ids Variable
id_list = mail_ids.split() #putting all mail id to id_list array
first_email_id = int(id_list[0]) #getting first mail id
latest_email_id = int(id_list[-1]) #getting last mail id
for i in range(latest_email_id, latest_email_id - read, -1): #this loop reading last 10 message
typ, data = mail.fetch(i, '(RFC822)') #fatch mail data, and putting it a tuple where i=tuple no and 'RFC822' is mail
for response_part in data: #reading all data from i no message
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1]) #Reading mail
email_subject = msg['subject'] #Email subject
email_from = msg['from'] #Sender address
print 'From : ' + email_from
print 'Subject : ' + email_subject
print "Read mail: https://gmail.com\n"
except Exception, e:
print (str(e)) #printing if there is any error
# ## Mail Server
# **This will start a server that notify user when there is new email**
# In[ ]:
def loop():
mail.select("INBOX") #Selecting inbox
n = 0
(retcode, messages) = mail.search(None, '(UNSEEN)') #sharching unseen mail
if retcode == 'OK': #if unseen
for num in messages[0].split():
n = n + 1
print n #print message number
typ, data = mail.fetch(num, '(RFC822)') #fatching mail
for response_part in data:
if isinstance(response_part, tuple):
original = email.message_from_string(response_part[1])
print original['From']
data = original['Subject']
try:
playsound('demonstrative.wav') #play sound when mail recive
except:
print "New mail reived.\nCan't play audio"
print data
print "Read mail: https://gmail.com"
from sys import stdout
stdout.write(str('#')) #printing mail server is alive
# In[ ]:
def server():
loop()
threading.Timer(15, server).start() #calling function evey 15 sec