Send image for email

Dear orthanc community

I have the following doubt

Can orthanc be configured to send an image with the viewer lite by email? for direct delivery to the patient, something similar to what postdicom does?

Waiting for your comments and thanking you very much for your help.

Kind regards

1 Like

Hello,

This should definitely be possible by creating a Python plugin. Indeed, Python plugins have full access to all the Python modules, which notably includes smtplib.

Kind Regards,
Sébastien-

1 Like

That is relatively easy using the Python Plug-in that Sébastien mentioned, both using an API endpoint and just as an internal function call within the Python Plug-in. I’ve been using that to notify myself about certain things happening on the server, and if you are using Docker it is helpful to setup ENV settings so you can just change those as needed.

You probably need at least the following and possibly other python PIP modules. The logging I set up there was custom, but if you wanted to log directly to the Orthanc log you can just use: orthanc.LogWarning()

import logging
import os
import ssl
import smtplib
from email.mime.text import MIMEText

def SendNotification(subject, body):
    # If the PYTHON_EMAILS_TO='false', then it will just log to a file.
    if os.getenv('PYTHON_EMAILS_TO', "False") == "False":
        logging.info('NOTIFICATION|'+subject+'|'+body)
    else:
        msg = MIMEText(body)
        msg['Subject'] = subject
        msg['From'] = os.getenv('PYTHON_EMAILS_FROM')
        msg['To'] =   os.getenv('PYTHON_EMAILS_TO')
        msg['Cc'] = os.getenv('PYTHON_EMAILS_CC')
        recipients = msg['To']
        if msg['Cc'] != "":
            recipients = recipients + msg['Cc']
        context = ssl.create_default_context()
        server = smtplib.SMTP_SSL(os.getenv('PYTHON_EMAILS_SERVER'), os.getenv('PYTHON_EMAILS_PORT'), context)
        server.login(os.getenv('PYTHON_EMAILS_USER'), os.getenv('PYTHON_EMAILS_PASS'))
        server.sendmail(msg['From'], recipients, msg.as_string())
        server.quit()


def SendEmail(output, uri, **request):

    if request['method'] != 'POST':
        output.SendMethodNotAllowed('POST')
    response = dict()
    payload = json.loads(request['body'])
    subject = payload['subject']
    body = payload['body']
    try:
        SendNotification(subject, body)
        response['status'] = "SUCCESS"
    except Exception as e:
        response['status'] = str(e)
    output.AnswerBuffer(json.dumps(response, indent = 3), 'application/json')

orthanc.RegisterRestCallback('/sendemail', SendEmail)