pyOutlook interacts with Outlook messages and folders through the class OutlookAccount(). The OutlookAccount acts as a gatekeeper to the other methods available, and stores the access token associated with an account.
from pyOutlook import OutlookAccount account_one = OutlookAccount('token 1') account_two = OutlookAccount('token 2')
From here you can access any of the methods as documented in the pyOutlook section. Here are two examples of accessing an inbox and sending a new email.
Through the OutlookAccount class you can call one of many methods -
These methods return a list of Message objects, allowing you to access the attributes therein.
inbox = account.inbox() inbox.body >>> 'A very fine body'
As above, you can send emails through the OutlookAccount class. There are two methods for sending emails - one allows chaining of methods and the other takes all arguments upfront and immediately sends.
You can create an instance of a
Message and then send from there.
from pyOutlook import * # or from pyOutlook.core.message import Message account = OutlookAccount('token') message = Message(account, 'A body', 'A subject', [Contact('firstname.lastname@example.org')]) message.attach(bytes('some bytes', 'utf-8'), 'bytes.txt') message.send()
This returns a
body = 'I\'m sending an email through Python. <br> Best, <br>Me' email = account.new_email(body=body, subject='Hey there', to=Contact('email@example.com')) email.sender = Contact('firstname.lastname@example.org') email.send()
Note that HTML formatting is accepted in the message body.
message = account.inbox() message.sender.name >>> 'Dude'
When providing recipients to
Message you can provide them either as a list
of strings, or a list of
Contacts. I prefer the latter, as there are further
options in the Outlook API for interacting with Contacts - functionality for those may be added in the future.