Skip to content

Commit 70956c2

Browse files
author
vinod (mercury)
committed
create, delete, rename mailboxes
1 parent 727f90d commit 70956c2

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

pygmail.py

+33-12
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
1-
import imaplib, re
1+
#Fri Jul 23 12:51:57 IST 2010
2+
# pygmail.py - A Python Library For Gmail
23

3-
class PyGmail(object):
4+
import imaplib, re
45

6+
class pygmail(object):
57
def __init__(self):
68
self.IMAP_SERVER='imap.gmail.com'
79
self.IMAP_PORT=993
810
self.M = None
11+
self.response = None
12+
self.mailboxes = []
913

10-
def login (self, username, password):
14+
def login(self, username, password):
1115
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
1318

1419
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
1724

1825
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]
2128

2229
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)
2532
return unreadCount
2633

2734
def get_imap_quota(self):
2835
quotaStr = self.M.getquotaroot("Inbox")[1][1][0]
2936
r = re.compile('\d+').findall(quotaStr)
3037
if r == []:
31-
r.append(0)
32-
r.append(0)
38+
r.append(0)
39+
r.append(0)
3340
return float(r[1])/1024, float(r[0])/1024
3441

3542
def get_mails_from(self, uid, folder='Inbox'):
@@ -42,3 +49,17 @@ def get_mail_from_id(self, id):
4249
status, response = self.M.fetch(id, '(body[header.fields (subject)])')
4350
return response
4451

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

Comments
 (0)