From 80f1fc276f8f4469e880dd80e5950ea9162dfd72 Mon Sep 17 00:00:00 2001 From: mike cullerton Date: Thu, 11 Mar 2021 11:19:02 -0500 Subject: [PATCH] Modify api.tools.send_email to accommodate the changes to the api endpoint in api.yml We now accept subject, address, body (the email message), and possible data. We now use email_service to send the message, and wrap it with the CR Connect html formatting. **** We import DEFAULT_SENDER from config.default. Will this be a problem on testing and production? **** --- crc/api/tools.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/crc/api/tools.py b/crc/api/tools.py index d3325540..faccd040 100644 --- a/crc/api/tools.py +++ b/crc/api/tools.py @@ -10,8 +10,10 @@ from crc.api.common import ApiError from crc.scripts.complete_template import CompleteTemplate from crc.scripts.script import Script import crc.scripts -from crc.services.mails import send_test_email from crc.services.workflow_processor import WorkflowProcessor +from crc.services.email_service import EmailService +from crc.services.workflow_processor import WorkflowProcessor, CustomBpmnScriptEngine +from config.default import DEFAULT_SENDER def render_markdown(data, template): @@ -64,11 +66,14 @@ def list_scripts(): return script_meta -def send_email(address): +def send_email(subject, address, body, data=None): """Just sends a quick test email to assure the system is working.""" - if not address: - address = "dan@sartography.com" - return send_test_email(address, [address]) + if address and body: + body = body.decode('UTF-8') + return send_test_email(subject, address, body, json.loads(data)) + else: + raise ApiError(code='missing_parameter', + message='You must provide an email address and a message.') def evaluate_python_expression(body): @@ -83,3 +88,14 @@ def evaluate_python_expression(body): return {"result": result} except Exception as e: raise ApiError("expression_error", str(e)) + + +def send_test_email(subject, address, message, data=None): + rendered, wrapped = EmailService().get_rendered_content(message, data) + EmailService.add_email( + subject=subject, + sender=DEFAULT_SENDER, + recipients=[address], + content=rendered, + content_html=wrapped) +