1
- import imaplib , re
1
+ #Fri Jul 23 12:51:57 IST 2010
2
+ # pygmail.py - A Python Library For Gmail
2
3
3
- class PyGmail ( object ):
4
+ import imaplib , re
4
5
6
+ class pygmail (object ):
5
7
def __init__ (self ):
6
8
self .IMAP_SERVER = 'imap.gmail.com'
7
9
self .IMAP_PORT = 993
8
10
self .M = None
11
+ self .response = None
12
+ self .mailboxes = []
9
13
10
- def login (self , username , password ):
14
+ def login (self , username , password ):
11
15
self .M = imaplib .IMAP4_SSL (self .IMAP_SERVER , self .IMAP_PORT )
12
- status , message = self .M .login (username , password )
16
+ rc , self .response = self .M .login (username , password )
17
+ return rc
13
18
14
19
def get_mailboxes (self ):
15
- status , folders = self .M .list ()
16
- return folders
20
+ rc , self .response = self .M .list ()
21
+ for item in self .response :
22
+ self .mailboxes .append (item .split ()[- 1 ])
23
+ return rc
17
24
18
25
def get_mail_count (self , folder = 'Inbox' ):
19
- status , count = self .M .select (folder , readonly = 1 )
20
- return count [0 ]
26
+ rc , self . response = self .M .select (folder )
27
+ return self . response [0 ]
21
28
22
29
def get_unread_count (self , folder = 'Inbox' ):
23
- status , message = self .M .status (folder , "(UNSEEN)" )
24
- unreadCount = re .search ("UNSEEN (\d+)" , message [0 ]).group (1 )
30
+ rc , self . response = self .M .status (folder , "(UNSEEN)" )
31
+ unreadCount = re .search ("UNSEEN (\d+)" , self . response [0 ]).group (1 )
25
32
return unreadCount
26
33
27
34
def get_imap_quota (self ):
28
35
quotaStr = self .M .getquotaroot ("Inbox" )[1 ][1 ][0 ]
29
36
r = re .compile ('\d+' ).findall (quotaStr )
30
37
if r == []:
31
- r .append (0 )
32
- r .append (0 )
38
+ r .append (0 )
39
+ r .append (0 )
33
40
return float (r [1 ])/ 1024 , float (r [0 ])/ 1024
34
41
35
42
def get_mails_from (self , uid , folder = 'Inbox' ):
@@ -42,3 +49,17 @@ def get_mail_from_id(self, id):
42
49
status , response = self .M .fetch (id , '(body[header.fields (subject)])' )
43
50
return response
44
51
52
+ def rename_mailbox (self , oldmailbox , newmailbox ):
53
+ rc , self .response = self .M .rename (oldmailbox , newmailbox )
54
+ return rc
55
+
56
+ def create_mailbox (self , mailbox ):
57
+ rc , self .response = self .M .create (mailbox )
58
+ return rc
59
+
60
+ def delete_mailbox (self , mailbox ):
61
+ rc , self .response = self .M .delete (mailbox )
62
+ return rc
63
+
64
+ def logout (self ):
65
+ self .M .logout ()
0 commit comments