skip to Main Content

I am trying to send an email using Graph API and Python. I tried doing it with graph explorer and it worked. I found this example: https://github.com/vgrem/Office365-REST-Python-Client#working-with-outlook-api

from office365.graph_client import GraphClient

client = GraphClient(acquire_token_func)

client.me.send_mail(
    subject="Meet for lunch?",
    body="The new cafeteria is open.",
    to_recipients=["[email protected]"]
).execute_query()

Here’s my code:

import msal

dict_ = {'client_id': 'foo', 'secret': 'bar', 'tenant_id': 'etc'}

def acquire_token():
    authority_url = f'https://login.microsoftonline.com/{dict_["tenant_id"]}'
    app = msal.ConfidentialClientApplication(
        authority=authority_url,
        client_id=dict_["client_id"],
        client_credential=dict_["secret"]
    )
    token = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
    return token


from office365.graph_client import GraphClient

client = GraphClient(acquire_token)

client.me.send_mail(
    subject="Meet for lunch?",
    body="The new cafeteria is open.",
    to_recipients=['[email protected]']
).execute_query()

Even though it’s exactly like in the example I still get:

TypeError: send_mail() got an unexpected keyword argument 'subject'

Can you help me fix this or provide a different way of sending an email?

2

Answers


  1. You are using client id and secret, in that case you cannot call client.me.send_mail.

    me can be called only when you are logged as a user.

    Try to use

    client.users[<mail>].send_mail(
        subject="Meet for lunch?",
        body="The new cafeteria is open.",
        to_recipients=['[email protected]']
    ).execute_query()
    
    Login or Signup to reply.
  2. Is it safe to use Office365 REST Python Client? No issues were found when the Office365-REST-Python-Client python package was scanned for known vulnerabilities and a missing license. Hence the bundle was considered as protected to utilize.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search