python-cyrus

import cyruslib

# without SSL
imap = cyruslib.CYRUS("imap://localhost:143")

# with SSL
imap = cyruslib.CYRUS("imaps://localhost:993")
# Admin login (virtdomains: yes)
imap.login("admin@example.com", "password")

# Logon as user with admin password (virtdomains: yes)
# SSL required!
imap.login_plain("admin@example.com", "password", "user@example.com")

# Admin login (virtdomains: no)
imap.login("admin", "password")

# Logon as user with admin password (virtdomains: no)
# SSL required!
imap.login_plain("admin", "password", "user")
try:
    imap = cyruslib.CYRUS("imap://localhost:143")
    imap.login("admin@example.com", "password")
    # do anything here (cm, dm, lam, ...)
except cyruslib.CYRUSError, e:
    print "%s: %s" % (e[1], e[2])
#  To list all mailboxes                       lm()
#  To list users top mailboxes                 lm("user/%")
#  To list all users mailboxes                 lm("user/*")
#  To list users mailboxes startwith a word    lm("user/word*")
#  To list global top folders                  lm("%")
#  To list global startwith a word             unsupported by server
#      suggestion (take care)                  lm("word*")

for mbx in imap.lm():
    print mbx
for mbx in imap.lm("user/rei/*"): print mbx
>    user/rei/caf&AOk-
>    user/rei/ma&AOc-a

imap.setEncoding('utf-8') # utf-8, iso-8859-1
for mbx in imap.lm("user/rei/*"): print mbx
>    user/rei/café
>    user/rei/maça
# unixhierarchysep: yes
imap.cm("user/rei")

# unixhierarchysep: no
imap.cm("user.rei")

# or delimiter independent
imap.cm("user%srei" % imap.SEP)

# also
imap.cm("user%srei%sSent" % (imap.SEP, imap.SEP))

# Global Mailbox (also availble to others commands)
imap.cm("Notices")
# User Mailbox
imap.dm("user/rei")
imap.rename("user/rei/sent-mail", "user/rei/Sent")

# Changing Imap-Partition (partition-label2 should be configured imapd.conf)
imap.rename("user/rei", "user/rei", "label2")
ACLdict = imap.lam("user/rei")
for acl in ACLdict.keys():
    print "%s: %s" % (acl, ACLdict.get(acl))
# virtdomains: yes
imap.sam("user/rei", "johndoe@example.com", "lrsw")

# virtdomains: no
imap.sam("user/rei", "johndoe", "lrsw")
# If quota does not exist then Exception is called
print imap.lq("user/rei")

for mailbox in imap.lm("user/%"):
    try:
        used, limit = imap.lq(mailbox)
        print "%s:\t%s\t%s\t%s%%" % (mailbox, used, limit, used * 100/ limit)
    except:
        print "%s:\t%s\t%s\t%s%%" % (mailbox, 0, 0, 0)
# Value in Kbytes
imap.sq("user/rei", "10240000")

# Unlimited
imap.sq("user/rei", "0")

# for all
for mbx in imap.lm("user/%"):
    if mbx not in ['user/rei', 'user/boss']:
        imap.sq(mbx, "102400")
# Server Annotation - ALL
print imap.getannotation("", "*")

# also
annotations = imap.getannotation("", "*").get("")
for annot in annotations:
    print "%s: %s" % (annot, annotations.get(annot))

# especific
print imap.getannotation("", "/motd")
print imap.getannotation("", "/vendor/cmu/cyrus-imapd/shutdown")

# Mailbox Annotation - ALL
print imap.getannotation("user/rei", "*")
# From ALL subfolders
print imap.getannotation("user/rei/*", "*")

# also
annotations = imap.getannotation("user/rei/*", "*")
for mailbox in annotations.keys():
    for annot in annotations.get(mailbox).keys():
        print "%s: %s: %s" % (mailbox, annot, annotations.get(mailbox).get(annot))

# especific
print imap.getannotation("user/rei", "/vendor/cmu/cyrus-imapd/expire")
print imap.getannotation("user/rei", "/vendor/cmu/cyrus-imapd/partition")

# also
print imap.getannotation("user/rei", "/vendor/cmu/cyrus-imapd/partition").get("user/rei").get("/vendor/cmu/cyrus-imapd/partition")
# Server Annotation
imap.setannotation("", "/motd", "The server will be unavailable tomorrow.")
imap.setannotation("", "/vendor/cmu/cyrus-imapd/shutdown", "The server is unavailble until 9am.")
# Unset
imap.setannotation("", "/motd", "")
imap.setannotation("", "/vendor/cmu/cyrus-imapd/shutdown", "")

# Mailbox Annotation
imap.setannotation("user/rei/Trash", "/vendor/cmu/cyrus-imapd/expire", "60")
imap.setannotation("user/rei/Spam", "/vendor/cmu/cyrus-imapd/expire", "30")
# Unset
imap.setannotation("user/rei/Trash", "/vendor/cmu/cyrus-imapd/expire", "")
imap.setannotation("user/rei/Spam", "/vendor/cmu/cyrus-imapd/expire", "")
imap.reconstruct("user/rei")
imap = cyruslib.CYRUS("imaps://localhost:993")
imap.login_plain("admin@example.com", "password", "rei@example.com")
imap.lsub()
imap = cyruslib.CYRUS("imap://localhost:143")
imap.login("admin@example.com", "password")
imap.cm("user%srei" % imap.SEP)
imap.cm("user%srei%sTrash" % (imap.SEP, imap.SEP))
imap.cm("user%srei%sSent" % (imap.SEP, imap.SEP))
imap.cm("user%srei%sDrafts" % (imap.SEP, imap.SEP))
imap.cm("user%srei%sSpam" % (imap.SEP, imap.SEP))
del imap

# ssl only
imap = cyruslib.CYRUS("imaps://localhost:993")
imap.login_plain("admin@example.com", "password", "rei@example.com")
imap.subscribe("INBOX")
imap.subscribe("INBOX%sTrash" % imap.SEP)
imap.subscribe("INBOX%sSent" % imap.SEP)
imap.subscribe("INBOX%sDrafts" % imap.SEP)
imap.subscribe("INBOX%sSpam" % imap.SEP)
del imap
imap = cyruslib.CYRUS("imaps://localhost:993")
imap.login_plain("admin@example.com", "password", "rei@example.com")
imap.unsubscribe("INBOX%sSpam" % imap.SEP)

  Authors
  Reinaldo de Carvalho (reinaldoc{NOSPAM}gmail.com)

Support This Project hacker emblem SourceForge.net Logo