Outlook Account

class pyOutlook.core.main.OutlookAccount(access_token)

Sets up access to Outlook account for all methods & classes.

Access token required for instantiation. Can be refreshed at a later time using .set_access_token().

Warning

This module does not handle the OAuth process. You must retrieve and refresh tokens separately.

access_token

A string OAuth token from Outlook allowing access to a user’s account

deleted_messages()

Retrieves last ten deleted messages.

Returns:list[Message]
draft_messages()

Retrieves last ten draft messages.

Returns:list[Message]
get_deleted_messages()

Retrieves last ten deleted messages.

Returns:list[Message]

Warning

This method is deprecated, use deleted_messages() instead

get_draft_messages()

Retrieves last ten draft messages.

Returns:list[Message]

Warning

This method is deprecated, use draft_messages() instead

get_folder(folder_id)

Retrieve a Folder object matching the folder ID provided.

Parameters:folder_id – String identifying the Outlook folder to return
Returns:Folder
get_folder_messages(folder)

Retrieve first ten messages from provided folder.

Parameters:folder – String providing the folder ID, from Outlook, to retrieve messages from
Returns:list[Message]
get_folders()

Retrieves a list of folders in the account.

Returns:list[Folder]
get_inbox()

Retrieves first ten messages in account’s inbox.

Returns:List[Message]

Warning

This method is deprecated, use inbox() instead

get_message(message_id) → pyOutlook.core.message.Message

Gets message matching provided id.

Retrieves the Outlook email matching the provided message_id.

Parameters:message_id – A string for the intended message, provided by Outlook
Returns:Message
get_messages()

Get first 10 messages in account, across all folders.

Returns:List[Message]
get_more_messages(page)

Retrieves additional messages, across all folders, indicated by ‘page’ number. get_messages() fetches page 1.

Parameters:page (int) – Integer representing the ‘page’ of results to fetch
Returns:List[Message]
get_sent_messages()

Retrieves last ten sent messages.

Returns:list[Message]

Warning

This method is deprecated, use sent_messages() instead

inbox()

Retrieves first ten messages in account’s inbox.

Returns:List[Message]
new_email()

Creates a NewMessage object.

Returns:NewMessage
send_email(body=None, subject=None, to=None, cc=None, bcc=None, send_as=None, attachment=None)

Sends an email in one method using variables to set the various pieces of the email.

Parameters:
  • body (str) – The body of the email
  • subject (str) – The subject of the email
  • to (list) – A list of email addresses
  • cc (list) – A list of email addresses which will be added to the ‘Carbon Copy’ line
  • bcc (list) – A list of email addresses while be blindly added to the email
  • send_as (str) – A string email address which the OutlookAccount has access to
  • attachment (dict) – A dictionary with three parts [1] ‘name’ - a string which will become the file’s name [2] ‘ext’ - a string which will become the file extension [3] ‘bytes’ - the bytes of the file.
sent_messages()

Retrieves last ten sent messages.

Returns:list[Message]
set_access_token(access_token)

Sets access token.

Set the access token after creating an OutlookAccount object.

Parameters:access_token – A string representing the OAuth token
Returns:None

Message

class pyOutlook.core.message.Message(message_id: str, body: str, subject: str, sender_email: str, sender_name: str, to_recipients: list)

An object representing an email inside of the OutlookAccount.

message_id

A string provided by Outlook identifying this specific email

body

The body content of the email, including HTML formatting

subject

The subject of the email

senderEmail

The email of the person who sent this email

senderName

The name of the person who sent this email, as provided by Outlook

toRecipients

A comma separated string of emails who were sent this email in the ‘To’ field

copy_to(folder_id)

Copies the email to the folder specified by the folder_id.

The folder id must match the id provided by Outlook.

Parameters:folder_id – A string containing the folder ID the message should be copied to
copy_to_deleted()

Copies Message to account’s Deleted Items folder

copy_to_drafts()

Copies Message to account’s Drafts folder

copy_to_inbox()

Copies Message to account’s Inbox

delete_message()

Deletes the email

forward_message(to_recipients, forward_comment)

Forward Message to recipients with an optional comment.

Parameters:
  • to_recipients – Comma separated string list of recipients to send email to.
  • forward_comment – String comment to append to forwarded email.

Examples

>>> email.forward_message('john.doe@domain.com, betsy.donalds@domain.com')
>>> email.forward_message('john.doe@domain.com', 'Hey Joe')
Raises:
  • MiscError – A comma separated string of emails, or one string email, must be provided
  • AuthError – Raised if Outlook returns a 401, generally caused by an invalid or expired access token.
move_to(folder_id)

Moves the email to the folder specified by the folder_id.

The folder id must match the id provided by Outlook.

Parameters:folder_id – A string containing the folder ID the message should be moved to
move_to_deleted()

Moves the email to the account’s Deleted Items folder

move_to_drafts()

Moves the email to the account’s Drafts folder

move_to_inbox()

Moves the email to the account’s Inbox

reply(reply_comment)

Reply to the Message.

Notes

HTML can be inserted in the string and will be interpreted properly by Outlook.

Parameters:reply_comment – String message to send with email.
reply_all(reply_comment)

Replies to everyone on the email, including those on the CC line.

With great power, comes great responsibility.

Parameters:reply_comment – The string comment to send to everyone on the email.

NewMessage

class pyOutlook.internal.createMessage.NewMessage(token)

A constructor for new emails.

Each method, excluding send(), returns the NewMessage object allowing chaining of methods.

add_attachment(file_bytes, file_name, file_extension)

Adds an attachment to the email.

Warning

This method is deprecated, use NewMessage.attach() instead. If using this method, you must base64 encode the file_bytes.

attach(file_bytes, file_name, file_extension)

Adds an attachment to the email.

Warning

This method does minimal escaping of input for the file_name. Slashes and periods will both be replaced by dashs (/), (.) > (-). Outlook may cut off portions of the file name due to some characters, if you encounter one, please create an issue on this module’s GitHub: https://github.com/JensAstrup/pyOutlook/issues.

Notes

You can send any file with this method, so long as the content is provided in bytes. Not doing so may lead to malformed attachments. You can also send bytes in a format that was not created in the same extension as the one provided to this method. For example, CSV bytes sent through with an xlsx extension will be attached and usable as an Excel document.

Parameters:
  • file_bytes – The bytes of the file to send
  • file_name – The name of the file, as a string and leaving out the extension, that should be sent
  • file_extension – The extension type (pdf, csv, etc) of the attachment. Do not include the ‘.’ in the string.
Returns:

NewMessage

bcc(recipients)

The list of email addresses that should be ‘blind’ copied on this email.

Parameters:recipients – A comma separated string of email addresses, or a list of strings.
Returns:NewMessage
cc(recipients)

The list of email addresses that should be copied on this email.

Parameters:recipients – A comma separated string of email addresses, or a list of strings.
Returns:NewMessage
send()

Sends the email containing the information provided in the other methods.

Raises:
  • SendError – Occurs if a subject or to recipients are not defined
  • MiscError – Occurs if Outlook responds with anything other than a 202, generally as a result of an expired
  • or invalid access token.
send_as(email)

Send the email via a separate email address, which the OutlookAccount has access to.

Parameters:email – A string providing the secondary email address that this email should be sent through.
Returns:NewMessage
set_body(body)

Sets the body of the email.

Parameters:body – str
Returns:NewMessage
set_subject(subject)

Sets the subject for the email.

This method is required in order to send the email.

Parameters:subject – str
Returns:NewMessage
to(recipients)

The list of email addresses this email should be sent to.

This method is required in order to send the email. The recipients parameter can either be a single address string or a comma separated list of addresses provided as a string.

Examples

>>> email.to('john@domain.com')
>>> email.to('john@domain.com, jane@domain.com')
Parameters:recipients – A comma separated string of email addresses, or a list of strings.
Returns:NewMessage

Folder

class pyOutlook.core.folders.Folder(folder_id, folder_name, parent_id, child_folder_count, unread_count, total_items)

An object representing a Folder in the OutlookAccount provided.

folder_id

The static id generated by Outlook to identify this folder.

folder_name

The name of this folder as displayed in the account

parent_id

The id of the folder which houses this Folder object

child_folder_count

The number of child folders inside this Folder

unread_count

The number of unread messages inside this Folder

total_items

A sum of all items inside Folder

copy_folder(destination_folder)

Copies the Folder into the provided destination folder.

Raises:

AuthError – Raised if Outlook returns a 401, generally caused by an invalid or expired access token.

Parameters:
  • destination_folder – A string containing the id of the folder, as provided by Outlook, that this Folder
  • be copied to. (should) –
Returns:

A new Folder representing the newly created folder.

create_child_folder(folder_name)

Creates a child folder within the Folder it is called from and returns the new Folder object.

Parameters:folder_name – The name of the folder to create

Returns: Folder

delete_folder()

Deletes this Folder.

Warning

This deletes the folder inside of the account provided - not the Folder object!

Raises:AuthError – Raised if Outlook returns a 401, generally caused by an invalid or expired access token.
get_subfolders()

Retrieve all child Folders inside of this Folder.

Raises:AuthError – Raised if Outlook returns a 401, generally caused by an invalid or expired access token.
Returns:List[Folder]
move_folder(destination_folder)

Move the Folder into a different folder.

This makes the Folder provided a child folder of the destination_folder.

Raises:AuthError – Raised if Outlook returns a 401, generally caused by an invalid or expired access token.
Parameters:destination_folder – An id, provided by Outlook, specifying the folder that should become the parent
Returns:A new Folder object representing the folder that is now inside of the destination_folder.
rename_folder(new_folder_name)

Renames the Folder to the provided name.

Parameters:new_folder_name – A string of the replacement name.
Raises:AuthError – Raised if Outlook returns a 401, generally caused by an invalid or expired access token.
Returns:A new Folder representing the folder with the new name on Outlook.