Spaces:
Running
Running
import os | |
from dotenv import load_dotenv | |
import msal | |
import requests | |
def send_email(reason, email, message) -> None: | |
body = f""" | |
ICD coder (Huggingface) | |
Subject: {reason} | |
Message: {message} | |
Contact Information: {email} | |
""" | |
load_dotenv() | |
client_id = os.getenv("CLIENT_ID") | |
client_secret = os.getenv("CLIENT_SECRET") | |
tenant_id = os.getenv("TENANT_ID") | |
authority = f"https://login.microsoftonline.com/{tenant_id}" | |
sender = os.getenv("MAIL_SENDER") | |
receiver = os.getenv("MAIL_RECIPIENT") | |
cc_receiver = os.getenv("CC_RECIPIENT") | |
app = msal.ConfidentialClientApplication( | |
client_id=client_id, | |
client_credential=client_secret, | |
authority=authority) | |
scopes = ["https://graph.microsoft.com/.default"] | |
result = app.acquire_token_silent(scopes, account=None) | |
if not result: | |
print("No suitable token exists in cache. Let's get a new one from Azure Active Directory.") | |
result = app.acquire_token_for_client(scopes=scopes) | |
if "access_token" in result: | |
endpoint = f'https://graph.microsoft.com/v1.0/users/{sender}/sendMail' | |
email_msg = { | |
'Message': { | |
'Subject': reason, | |
'Body': { | |
'ContentType': 'Text', | |
'Content': body | |
}, | |
'ToRecipients': [{'EmailAddress': {'Address': receiver}}], | |
'CcRecipients': [{'EmailAddress': {'Address': cc_receiver}}] # Added CcRecipients here | |
}, | |
'SaveToSentItems': 'true' | |
} | |
r = requests.post(endpoint, headers={'Authorization': 'Bearer ' + result['access_token']}, json=email_msg) | |
if r.ok: | |
print('Sent email successfully') | |
else: | |
print(r.json()) | |
else: | |
print(result.get("error")) | |
print(result.get("error_description")) | |
print(result.get("correlation_id")) | |