forked from jamescoverdale/docker-forticlient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimap.py
executable file
·63 lines (51 loc) · 1.45 KB
/
imap.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
#!/usr/bin/env python
#
# Very basic example of using Python and IMAP to iterate over emails in a
# gmail folder/label. This code is released into the public domain.
#
# RKI July 2013
# http://www.voidynullness.net/blog/2013/07/25/gmail-email-with-python-via-imap/
#
import sys
import imaplib
import getpass
import email
import email.header
import datetime
import time
EMAIL_ACCOUNT = sys.argv[1]
EMAIL_PASSWORD = sys.argv[2]
EMAIL_SMTP = sys.argv[3]
EMAIL_FOLDER = "INBOX"
def process_mailbox(M):
found = 0
while found != 1:
rv, data = M.search(None, "(UNSEEN)", "SUBJECT", "AuthCode:")
if rv != 'OK':
print "No messages found!"
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
found = 1
if rv != 'OK':
print "ERROR getting message", num
return
msg = email.message_from_string(data[0][1])
decode = email.header.decode_header(msg['Subject'])[0]
subject = unicode(decode[0])
sub, authcode = subject.split(": ")
print '%s' % (authcode)
found = 1
time.sleep(5)
M = imaplib.IMAP4_SSL(EMAIL_SMTP)
try:
rv, data = M.login(EMAIL_ACCOUNT, EMAIL_PASSWORD)
except imaplib.IMAP4.error:
sys.exit(1)
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
process_mailbox(M)
M.close()
else:
print "ERROR: Unable to open mailbox ", rv
M.logout()