-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_mail.py
88 lines (63 loc) · 2.65 KB
/
fetch_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
'''
Created on Mar 18, 2014
@author: Dario Bonino <[email protected]>
Copyright (c) 2014 Dario Bonino
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
'''
import imaplib, email, tts, time
def check_mail(user, pwd):
'''
Checks a gmail mailbox for new messages, and if any unread mail is found
provides a vocal rendering of the mail sender and subject
'''
# define the mail server (IMAP) in our case
mail = imaplib.IMAP4_SSL('imap.gmail.com')
# set the login parameters
mail.login(user, pwd)
# select the inbox
mail.select('inbox')
# Search for all new mail
status, email_ids = mail.search(None, '(UNSEEN)')
# get the e-mail ids
if(len(email_ids[0].strip()) > 0):
ids = email_ids[0].split(" ")
# if there are unseen e-mails
unreadcount = len(ids)
if(unreadcount > 0):
if(unreadcount > 1):
tts.say("There are %d new messages" % (len(ids)))
else:
tts.say("There is one new message")
# iterate over unread messages
for i in range(0, unreadcount):
# message number
tts.say("Message %d:" % (i + 1))
# get the full email contents
status, data = mail.fetch(ids[i], "(RFC822)")
# get the raw mail message
raw_email = data[0][1]
# parse the mail message
msg = email.message_from_string(raw_email)
# warn about the message
tts.say("You've got a new message from ")
# render sender names
for sender in msg.get_all('from', []):
tts.say(sender)
# render subject
tts.say("with subject")
tts.say(msg.get('Subject', ''))
mail.logout()
if __name__ == "__main__":
while(True):
# check for any new mail
check_mail('[email protected]', ' uxrqsyiqiwewbont ')
# wait 10s before next check
time.sleep(5)