From 40300c714367930d0f83921c6aa11617fa987980 Mon Sep 17 00:00:00 2001 From: Mithun Shivashankar Date: Mon, 21 Oct 2024 10:48:05 +0200 Subject: [PATCH 1/5] Extract subject from txt --- asabiris/orchestration/sendemail.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/asabiris/orchestration/sendemail.py b/asabiris/orchestration/sendemail.py index 1c6e2fff..59222df4 100644 --- a/asabiris/orchestration/sendemail.py +++ b/asabiris/orchestration/sendemail.py @@ -186,6 +186,20 @@ def find_subject_in_md(body): return body, subject +def find_subject_in_txt(body: str) -> Tuple[str, str]: + # Check if the body starts with "Subject:" (case-insensitive) + if not body.lower().startswith("subject:"): + return body, None + + # Extract the subject from the first line, case-insensitively + subject = body.split("\n")[0].replace("Subject:", "", 1).lstrip() + + # Remove the subject line from the body + body = "\n".join(body.split("\n")[1:]) + + return body, subject + + def convert_markdown_to_full_html(html_text): """ Convert Markdown text to a full HTML document. From d548abd80bd83cf18e9e2882ab519a854cf29d98 Mon Sep 17 00:00:00 2001 From: Mithun Shivashankar Date: Tue, 22 Oct 2024 07:47:10 +0200 Subject: [PATCH 2/5] Support txt rendering --- asabiris/orchestration/sendemail.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/asabiris/orchestration/sendemail.py b/asabiris/orchestration/sendemail.py index 59222df4..b023ed0e 100644 --- a/asabiris/orchestration/sendemail.py +++ b/asabiris/orchestration/sendemail.py @@ -96,6 +96,8 @@ async def send_email( ) L.info("Email sent successfully to: {}".format(', '.join(email_to))) + import os + from typing import Dict, Tuple async def _render_template(self, template: str, params: Dict, body_template_wrapper=None) -> Tuple[str, str]: # First, determine if a default wrapper needs to be used @@ -108,9 +110,7 @@ async def _render_template(self, template: str, params: Dict, body_template_wrap ErrorCode.INVALID_PATH, tech_message="Incorrect template path '{}'. Move templates to '/Templates/Email/'.".format(template), error_i18n_key="Incorrect template path '{{incorrect_path}}'. Please move your templates to '/Templates/Email/'.", - error_dict={ - "incorrect_path": template, - } + error_dict={"incorrect_path": template} ) if body_template_wrapper is not None and not body_template_wrapper.startswith('/Templates/Wrapper/'): @@ -119,9 +119,7 @@ async def _render_template(self, template: str, params: Dict, body_template_wrap tech_message="Incorrect wrapper template path '{}'. Move wrapper templates to '/Templates/Wrapper/'.".format( body_template_wrapper), error_i18n_key="Incorrect wrapper template path '{{incorrect_path}}'. Please move your wrapper templates to '/Templates/Wrapper/'.", - error_dict={ - "incorrect_path": body_template_wrapper, - } + error_dict={"incorrect_path": body_template_wrapper} ) # Proceed with rendering the template @@ -144,14 +142,23 @@ async def _render_template(self, template: str, params: Dict, body_template_wrap return html_body, subject + elif ext == '.txt': + # Extract the subject from the text template + plain_text_body, subject = find_subject_in_txt(jinja_output) + + # Apply the wrapper if it exists and is not empty + if body_template_wrapper not in [None, '']: + plain_text_param = {"content": plain_text_body} + plain_text_body = await self.JinjaService.format(body_template_wrapper, plain_text_param) + + return plain_text_body, subject + else: raise ASABIrisError( ErrorCode.INVALID_FORMAT, tech_message="Unsupported conversion format '{}' for template '{}'".format(ext, template), error_i18n_key="The format '{{invalid_format}}' is not supported", - error_dict={ - "invalid_format": ext, - } + error_dict={"invalid_format": ext} ) def _generate_error_message(self, specific_error: str) -> Tuple[str, str]: From c7215efbb8c03d498002d0b245f4eb4ff97a16fb Mon Sep 17 00:00:00 2001 From: Mithun Shivashankar Date: Tue, 22 Oct 2024 09:48:15 +0200 Subject: [PATCH 3/5] Add sample template --- library/Templates/Email/message.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 library/Templates/Email/message.txt diff --git a/library/Templates/Email/message.txt b/library/Templates/Email/message.txt new file mode 100644 index 00000000..f96636ff --- /dev/null +++ b/library/Templates/Email/message.txt @@ -0,0 +1,7 @@ +SUBJECT: Hello from ASAB Iris +Hello, + +I'm very simple message by {{name}}! + +Your, +ASAB Iris From 657fc7f8e2a73080efb6aa4a5e6f49da650e349a Mon Sep 17 00:00:00 2001 From: Mithun Shivashankar Date: Tue, 22 Oct 2024 09:50:20 +0200 Subject: [PATCH 4/5] Add to test suite --- qa.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/qa.md b/qa.md index bc91776d..2728e0d4 100644 --- a/qa.md +++ b/qa.md @@ -89,6 +89,23 @@ } ``` +## TSM001F: Send an email using Markdown template from a template that uses now from template global. + +`PUT /send_mail` + +``` +{ + "to": ["foo@example.com"], + "body": { + "template": "/Templates/Email/mesage.txt", + "params":{ + "name":"Iris", + "tenant":"Default", + } + } +} +``` + ## TSM002: Send an email using HTML template From 62e635bf39b3537b1df6bc84835d03c45b799b01 Mon Sep 17 00:00:00 2001 From: Mithun Shivashankar Date: Tue, 22 Oct 2024 09:54:37 +0200 Subject: [PATCH 5/5] Add to test suite for kafka --- qa.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/qa.md b/qa.md index 2728e0d4..b171e055 100644 --- a/qa.md +++ b/qa.md @@ -1484,6 +1484,8 @@ EXPECTED RESPONSE: {"type":"email", "to": ["Shivashankar "], "from": "info@teskalabs.com", "body":{"template":"/Templates/Export.md", "params":{"name": "I am testing a template", "error": "None" }}} +{"type":"email", "to": ["Shivashankar "], "from": "info@teskalabs.com", "body":{"template":"/Templates/Email/message.txt", "params":{"name": "I am testing a template", "error": "None" }}} + 'WITHOUT FROM' {"type":"email", "to": ["Shivashankar "], "body":{"template":"/Templates/Export.md", "params":{"name": "I am testing a template", "error": "None" }}}