Skip to content

Commit

Permalink
Merge pull request #80 from TeskaLabs/Enhancement/Email-supports-txt
Browse files Browse the repository at this point in the history
Email Supports TXT
  • Loading branch information
mithunbharadwaj authored Nov 21, 2024
2 parents 84ecc7c + 62e635b commit 7f020f4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
39 changes: 30 additions & 9 deletions asabiris/orchestration/sendemail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/'):
Expand All @@ -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
Expand All @@ -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]:
Expand Down Expand Up @@ -186,6 +193,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.
Expand Down
7 changes: 7 additions & 0 deletions library/Templates/Email/message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SUBJECT: Hello from ASAB Iris
Hello,

I'm very simple message by {{name}}!

Your,
ASAB Iris
19 changes: 19 additions & 0 deletions qa.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@
}
```

## TSM001F: Send an email using Markdown template from a template that uses now from template global.

`PUT /send_mail`

```
{
"to": ["[email protected]"],
"body": {
"template": "/Templates/Email/mesage.txt",
"params":{
"name":"Iris",
"tenant":"Default",
}
}
}
```


## TSM002: Send an email using HTML template

Expand Down Expand Up @@ -1467,6 +1484,8 @@ EXPECTED RESPONSE:
{"type":"email", "to": ["Shivashankar <[email protected]>"], "from": "[email protected]", "body":{"template":"/Templates/Export.md", "params":{"name": "I am testing a template", "error": "None" }}}
{"type":"email", "to": ["Shivashankar <[email protected]>"], "from": "[email protected]", "body":{"template":"/Templates/Email/message.txt", "params":{"name": "I am testing a template", "error": "None" }}}
'WITHOUT FROM'
{"type":"email", "to": ["Shivashankar <[email protected]>"], "body":{"template":"/Templates/Export.md", "params":{"name": "I am testing a template", "error": "None" }}}
Expand Down

0 comments on commit 7f020f4

Please sign in to comment.