Skip to main content

Microsoft Outlook client Python package that uses the requests library.

Project description

outlooklib

Package Description

Microsoft Outlook client Python package that uses the requests library.

[!IMPORTANT]
This packages uses pydantic v1!

Usage

From a script:

import outlooklib
import pandas as pd

client_id = "123"
client_secret = "123"
tenant_id = "123"

client_email = "team.email@company.com"

# Initialize Outlook client
outlook = outlooklib.Outlook(client_id=client_id,
                             tenant_id=tenant_id,
                             client_secret=client_secret,
                             client_email=client_email,
                             client_folder="Inbox")
# Retrieve a list of mail folders
response = outlook.list_folders()
if response.status_code == 200:
    df = pd.DataFrame(response.content)
    print(df)
# Retrieve the top 100 unread messages from the specified folder
response = outlook.list_messages(filter="isRead ne true")
if response.status_code == 200:
    df = pd.DataFrame(response.content)
    print(df)
# Retrieve the top 100 messages from the specified folder, with more than 2 days
import datetime

n_days_ago = (datetime.datetime.now(datetime.UTC) - datetime.timedelta(days=2)).strftime("%Y-%m-%dT%H:%M:%SZ")

response = outlook.list_messages(filter=f"receivedDateTime le {n_days_ago}")
if response.status_code == 200:
    df = pd.DataFrame(response.content)
    print(df)
# Download message attachments
message_id = "A...A=="
response = outlook.download_message_attachment(id=message_id, path=r"C:\Users\admin", index=True)
if response.status_code == 200:
    print("Attachment(s) downloaded successfully")
# Delete a message from the current folder
message_id = "A...A=="
response = outlook.delete_message(id=message_id)
if response.status_code == 204:
    print("Message deleted successfully")
# Delete messages from the current folder, one by one, with more than 3 days
import datetime

x_days_ago = (datetime.datetime.now(datetime.UTC) - datetime.timedelta(days=3)).strftime("%Y-%m-%dT%H:%M:%SZ")
response = outlook.list_messages(filter=f"receivedDateTime le {x_days_ago}")
if response.status_code == 200:
    df = pd.DataFrame(response.content)
    display(df)

    for msg_id in df["id"]:
        response = outlook.delete_message(id=msg_id)
        if response.status_code == 204:
            print(f"Message {msg_id} deleted successfully")
# Send an email with HTML body and optional attachments
response = outlook.send_message(recipients=["peter.parker@example.com"],
                                subject="Web tests",
                                message="Something<br>to talk about...",
                                attachments=None)
if response.status_code == 200:
    print("Email sent")
# Change current folder
outlook.change_folder(id="root")
# Close
del(outlook)

Installation

Install python and pip if you have not already.

Then run:

pip install pip --upgrade

For production:

pip install outlooklib

This will install the package and all of it's python dependencies.

If you want to install the project for development:

git clone https://github.com/aghuttun/outlooklib.git
cd outlooklib
pip install -e ".[dev]"

Docstring

The script's docstrings follow the numpydoc style.

License

BSD License (see license file)

top

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

outlooklib-0.0.12.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

outlooklib-0.0.12-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file outlooklib-0.0.12.tar.gz.

File metadata

  • Download URL: outlooklib-0.0.12.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for outlooklib-0.0.12.tar.gz
Algorithm Hash digest
SHA256 254f9290ba3a04d9a207dc7648fa62e1b243b90c7277393c7dd1bb0e1f5c5518
MD5 7898f6395187fe0ad5ec264dc8c7c869
BLAKE2b-256 1bcd630a83fc3b9e7a648e7dde7abc472c6429d2f117073e8d9ee835fb6acea3

See more details on using hashes here.

File details

Details for the file outlooklib-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: outlooklib-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for outlooklib-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 5c5e607482866a791139d29b8fcbb5f99bcd8b505d75f04820c6f023603fb405
MD5 69f6a292cf61e4b19ce1dbd181c912dc
BLAKE2b-256 4d426332ea05c96134ca4ccac9c5349dac0ab751ee83274df01aef5c8b0a3a32

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page