Friday, March 1, 2024

 

This is an automation for one of the routine tasks in DevOps:

import requests

def send_email():

    # Get an access token from Azure Active Directory

    # (You'll need to set up an app registration and obtain client ID and secret)

    scopes = ["https://graph.microsoft.com/.default"]

    # Acquire the token using your client ID and secret

    # ...

 

    # Define the email message

    userId = "your_user_id"

    endpoint = f"https://graph.microsoft.com/v1.0/users/{userId}/sendMail"

    toUserEmail = "recipient@example.com"

    email_msg = {

        "Message": {

            "Subject": "Test Sending Email from Python",

            "Body": {

                "ContentType": "Text",

                "Content": "This is a test email."

            },

            "ToRecipients": [

                {"EmailAddress": {"Address": toUserEmail}}

            ]

        },

        "SaveToSentItems": "true"

    }

 

    # Send the email

    r = requests.post(

        endpoint,

        headers={'Authorization': 'Bearer ' + result['access_token']},

        json=email_msg

    )

 

    if r.ok:

        print('Sent email successfully')

    else:

        print(r.json())

 

send_email()

 

Previous articles:IaCResolutionsPart84.docx

No comments:

Post a Comment