Search code examples
azureemailazure-logic-apps

Azure logic app - keep JSON structure when using Send Email v2


I have an azure logic app for error handling that reads from a service bus topic. The idea is straightforward, I want to send an email to an appropriate team notifying them of a failure and attaching the JSON data that failed.

My issue is that the JSON formatting gets completely lost when sent through the outlook step "Send an Email v2"

I've tried creating a step that replaces the \r\n characters in my JSON string with html code for newlines, composing the variable using json function, and creating my own HTML using compose and putting that in the body of the email.

enter image description here

All I'm looking to do is get the email to keep the same formatting that appeared in the output of the compose step, but it always comes back all jumbled up and more difficult to read in outlook.

enter image description here

Is what I am looking for possible to do? Here is my logic app code.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "@{triggerBody()?['contentData']}  ",
                "runAfter": {},
                "type": "Compose"
            },
            "Send_an_email_(V2)": {
                "inputs": {
                    "body": {
                        "Body": "<p>Hello,<br><br>An error was detected from! Please review the below details and investigate the error.<br><br>Here is the error message<br>@{triggerBody()?['label']}<br><br>Here is the data that was passed<br>@{outputs('Compose')}<br><br>Please reach out to the IT department with a helpdesk ticket if further action is required.</p>",
                        "Importance": "Normal",
                        "Subject": "Error detected from our API",
                        "To": "[email protected]"
                    },
                    "host": {
                        "connection": {
                            "referenceName": "office365"
                        }
                    },
                    "method": "post",
                    "path": "/v2/Mail"
                },
                "runAfter": {
                    "Compose": [
                        "SUCCEEDED"
                    ]
                },
                "type": "ApiConnection"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "triggers": {
            "When_messages_are_available_in_a_topic": {
                "inputs": {
                    "parameters": {
                        "isSessionsEnabled": false,
                        "subscriptionName": "sbs-api",
                        "topicName": "sbt-error-handling"
                    },
                    "serviceProviderConfiguration": {
                        "connectionName": "serviceBus",
                        "operationId": "receiveTopicMessages",
                        "serviceProviderId": "/serviceProviders/serviceBus"
                    }
                },
                "splitOn": "@triggerOutputs()?['body']",
                "type": "ServiceProvider"
            }
        }
    },
    "kind": "Stateful"
}

Thank you for the help/suggestions!


Solution

  • I was able to figure it out! My mistake was looking at this from the perspective of azure logic apps, and not of the HTML rendering. The send email v2 method uses HTML rendering by default for sending emails.

    In order to get the output to render properly, I had to surround it with the

     tag. That let the browser (azure) know that this was preformatted text and no need to correct it.

    It looks like this and rendered it exactly how i was expecting it to

    <pre>@{outputs('Compose')}</pre>