Spaces:
Running
Running
File size: 1,969 Bytes
42d005b 661c72d 42d005b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
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"))
|