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
 
#Load an attachment and convert it to Base64
def GetFileContent(fname):
    content = None
    with open(fname,'rb') as img:
        content = base64.b64encode(img.read())
 
    return content   
 
#Load the 'content.json', which gives us all of the messages that need to be sent
pth = os.path.dirname(__file__) + "/content.json"
f = open(pth,'r')
content = f.read()
f.close()
 
#Decode the JSON
messages = json.loads(content)
 
#Import the Azure EmailClient library
from azure.communication.email import EmailClient
 
try:
    connection_string = "endpoint=<YOUR ENDPOINT INFO & KEY HERE>"
    client = EmailClient.from_connection_string(connection_string)
 
    #Go through the messages that need to be sent
    for msg in messages:
        try:
            #Make a new email message
            message = {
                "senderAddress": "DoNotReply@<YOUR AZURE EMAIL DOMAIN HERE>",
                "recipients":  {
                    "to": [{"address": msg['to'] + '@<YOUR NORMAL EMAIL DOMAIN HERE>' }],
                },
                "content": {
                    "subject": msg['title'],
                    "plainText": msg['content'],
                    "html": msg['richcontent']
                },
                "attachments": []
            }
 
            #Add each of the attachments
            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:
            traceback.print_exc()
            pass
 
except Exception as ex:
    print(ex)