Azure Communication Services

ACS is a useful tool for sending email if you have an ARDI server with cloud (or at least Azure) access.

Once you've created an Azure Communication Services resource in Azure, you can also create an Email Communication Service following the guide here - https://learn.microsoft.com/en-gb/azure/communication-services/concepts/email/prepare-email-communication-resource.

Once set up and ready, you can use the following push.py script in your ARDI sites reports folder.

import os
import json
import traceback
import base64
 
#Get the file content as a Base64 Encoded String
def GetFileContent(fname):
    content = None
    with open(fname,'rb') as img:
        content = base64.b64encode(img.read())
 
    return content
 
#Get the content of the file sent to us
pth = os.path.dirname(__file__) + "/content.json"
f = open(pth,'r')
content = f.read()
f.close()
 
content = json.loads(content)
 
#Extract all of the options (azure key and email address)
messages = content['messages']
comkey = content["options"]["azurekey"]
fromaddr = content["options"]["azureemail"]
 
#Import any required libraries
from azure.communication.email import EmailClient
 
failures = None
 
try:
    #Setup email object
    connection_string = "endpoint=https://cservices.australia.communication.azure.com/;accesskey=" + comkey
    client = EmailClient.from_connection_string(connection_string)
 
    #For every message to be sent...
    for msg in messages:
        try:
            toaddress = msg['to']
            recip = []            
            recip.append({"address": toaddress['name']})
 
            #Create an object describing the message to send...
            message = {
                "senderAddress": str(fromaddr),
                "recipients":  {
                    "to": recip,
                },
                "content": {
                    "subject": msg['title'],
                    "plainText": msg['content'],
                    "html": msg['richcontent']
                },
                "attachments": []
            }
 
            #Add attachments to the email
            for at in msg['attachments']:
                nitem = {}
                ct = GetFileContent(at['path'])
                if ct is not None:
                    nitem['name'] = at['name']
                    nitem['contentType'] = at['contenttype']
                    nitem['contentInBase64'] = ct.decode()
                    message['attachments'].append(nitem)
 
            #Send the actual message
            poller = client.begin_send(message)
            result = poller.result()
 
        except:
            if failures is None:
                failures = "Failed To Send"
            else:
                failures += " / Failed To Send"
            traceback.print_exc()
 
except Exception as ex:
    failures = "Unable to Connect"
 
 
if failures is None:
    print("----")
    print("SUCCESS")
else:
    print("----")
    print("FAILURE:" + failures)