-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathemail_client.py
More file actions
57 lines (52 loc) · 1.9 KB
/
email_client.py
File metadata and controls
57 lines (52 loc) · 1.9 KB
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
import imaplib
class EmailClient(object):
def __init__(self, config):
self._config = config
self.connect(self._config)
def connect(self, config):
try:
self._client = imaplib.IMAP4_SSL(config.get('domain_name'))
self._client.login(config.get('email_address'), config.get('password'))
self.mailbox(config.get('mailbox'))
except Exception as e:
raise e
def mailbox(self, mailbox_name):
try:
rv, data = self._client.select(mailbox_name)
if rv != 'OK':
raise Exception("Invalid mailbox: ", mailbox_name)
except Exception as e:
raise e
def search(self, search_str):
try:
rv, data = self._client.search(None, search_str)
if rv != 'OK':
raise Exception("No email with given search criteria ", search_str)
return data[0].split()
except Exception as e:
raise e
def get_email(self, email_id):
try:
rv, data = self._client.fetch(email_id, '(RFC822)')
if rv != 'OK':
raise Exception("No email found for email id ", email_id)
return data[0][1]
except Exception as e:
raise e
def search(self, search_str):
try:
print search_str
rv, data = self._client.search(None, search_str)
if rv != 'OK':
raise Exception("No email with given search criteria ", search_str)
return data[0].split()
except Exception as e:
raise e
def get_email(self, email_id):
try:
rv, data = self._client.fetch(email_id, '(RFC822)')
if rv != 'OK':
raise Exception("No email found for email id ", email_id)
return data[0][1]
except Exception as e:
raise e