2. Getting started¶
2.1. Connecting to the server¶
To be able to do anything, of course we first need to connect to a Kopano server. For non-interactive use, we recommend to do this as follows:
import kopano server = kopano.Server()
If the Kopano server is on another system, we can specify how to connect to it as follows:
import kopano server = kopano.Server( server_socket='<https://remoteip:237/kopano>', sslkey_file='</etc/kopano/ssl/server.pem>', sslkey_pass='<password>' )
Python-kopano will first look at any specified settings, then /etc/kopano/admin.cfg, and if there is nothing, it will fall back to the local server.
2.2. Opening a store¶
Now that we are connected to a Kopano server, let us open a certain user store.
store = server.user('<username>').store
One can also use the following shortcut, so that an explicit server object is not needed. Such shortcuts are especially meant to be used in interactive-mode (that is, from a Python shell). In non-interactive mode, we recommend to always use an explicit server object.
store = kopano.user('<username>').store
2.3. Opening a folder¶
A folder can be opened from a store using a folder name (or path):
inbox = store.folder('Inbox') other_folder = store.folder('Inbox/some_subfolder')
For special folders, there is usually a simpler way:
inbox = store.inbox junk = store.junk
To know the number of items in a folder:
count = inbox.count
Note that it is usually not needed to use an explicit store object. If there’s no ambiguity, we can directly use the user object.
inbox = user.inbox
2.4. Looping over folder items and their properties¶
Let’s loop over all items in a certain folder:
for item in user.junk: print(item.received, item.subject)
A MAPI item is basically just a collection of MAPI properties. Here’s how to loop over them:
for prop in item: print(prop, prop.value)
Let’s access a specific property, which holds the subject of the respective item:
from MAPI.Tags import PR_SUBJECT_W # python-mapi print(item.prop(PR_SUBJECT_W).value)
2.5. Sending an email¶
item = user.outbox.create_item( email@example.com', subject='test email', body='Hi john!') item.send()