Skip to content

aghuttun/outlooklib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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"

# 0. 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"
)
# 1. Retrieve a list of mail folders
response = outlook.list_folders()

if response.status_code == 200:
    df = pd.DataFrame(response.content)
    display(df)
# 2.1. 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)
    display(df)
# 2.2. 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)
# 3. 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")
# 4.1. 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")
# 4.2. 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")
# 5. 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")
# 6. 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

About

Microsoft Outlook client Python package that uses the requests library.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages