How to read email in dataiku?
Is there any way to read emails (subject, sender, cc, bcc, body & etc.) from my outlook(work) inbox\folders in Dataiku?
Best Answer
-
Turribeach Dataiku DSS Core Designer, Neuron, Dataiku DSS Adv Designer, Registered, Neuron 2023, Circle Member Posts: 2,626 NeuronFirst of all Outlook it’s an email client, not a server. So you need to target the server which is usually Microsoft Exchange but it will depend on your company. Dataiku doesn’t have a built-in capability to read emails from Exchange.
chatGPT answer:
Use the Microsoft Graph API. Exchange Online no longer supports new apps using legacy EWS, and Graph provides the simplest, supported path.1. Register an Azure AD app
- Go to Azure Portal → Azure Active Directory → App registrations → New registration.
- Add a redirect URI if you plan to use interactive authentication.
- Under API permissions:
- Add Microsoft Graph → Delegated: Mail.Read (or Mail.ReadWrite).
- Or Application permission Mail.Read if you want unattended server-side access.
- Grant admin consent.
2. Authenticate
Use MSAL for Python.
Delegated (user sign-in)
from msal import PublicClientApplication
import requestsclient_id = "<APP_ID>"
tenant_id = "<TENANT_ID>"
scopes = ["https://graph.microsoft.com/Mail.Read"]
app = PublicClientApplication(client_id, authority=f"https://login.microsoftonline.com/{tenant_id}")
result = app.acquire_token_interactive(scopes=scopes)
access_token = result["access_token"]
Application (daemon) authentication
from msal import ConfidentialClientApplication
client_id = "<APP_ID>"
client_secret = "<CLIENT_SECRET>"
tenant_id = "<TENANT_ID>"
scopes = ["https://graph.microsoft.com/.default"]
app = ConfidentialClientApplication(client_id,
client_credential=client_secret,
authority=f"https://login.microsoftonline.com/{tenant_id}"
)
token = app.acquire_token_for_client(scopes=scopes)
access_token = token["access_token"]
3. Read mail with Microsoft Graph
import requests
headers = {"Authorization": f"Bearer {access_token}"}
# Get top 10 messages
resp = requests.get( "https://graph.microsoft.com/v1.0/me/messages?$top=10",
headers=headers
)
messages = resp.json()["value"]
for m in messages:
print(m["subject"], m["sender"]["emailAddress"]["address"])
4. Application permissions (if reading another mailbox)
Use:
https://graph.microsoft.com/v1.0/users/<user@domain .com>/messages
Alternative: EWS (not recommended, legacy)
Python library: exchangelib. Works only if EWS still enabled in your tenant.
from exchangelib import Account, Credentials, Configuration, DELEGATE
creds = Credentials("user@domain .com", "password")
config = Configuration(server="outlook.office365.com", credentials=creds)
account = Account("user@domain .com", config=config, autodiscover=False, access_type=DELEGATE)
for item in account.inbox.all()[:10]:
print(item.subject)