Search code examples
htmljsonsmtpdataweavemule4

JSON array to Table structure formatting while sending email via Mule 4


I have a requirement to send the error report as a tabular format, which needs to be sent as a email body. I am constructing error report as a JSON array and I would need to do the HTML transformation to structure that as table format.

Below is the JSON array and the expected result as email body.

[ { "templateID": "72", "houseNumber": "SR0000V-test", "dataMigrationSourceId": 2102866, "errorDescription": "Process aborted:CatalogItem not found in Target system!" }, { "templateID": "", "houseNumber": "SR0000V-test", "dataMigrationSourceId": 2102866, "errorDescription": "Process aborted: TemplateID is not present!" } ]

Expected Email Body: enter image description here

If not exactly same format, would need the result something similar to the table structure or more readable format. Can some one please help me construct this format in Mule 4

Thanks


Solution

  • Html Table (Payload)

    You can customize your css inside @(). I just kept a sample which can be useful to you

    %dw 2.0
    output application/xml writeDeclaration=false
    ---
    {
        table @(style: "width: 50%; border: 1px solid grey; font-family: Monospace" ): {
            tr @(bgcolor: "#6c7ae0",style: "color: white !important; font-size:14px; "): {
                th @() : "templateId",
                th @() : "houseNumber",
                th @() : "dataMigrationSourceId",
                th @() : "errorDescription"
            },
            (payload map (item, index) -> {
                tr @(align:"center", style: "color: #666666; font-size:12px; font-weight: 500; width:10%"): {
                    td @(): item.templateID,
                    td @(): item.houseNumber,
                    td @(): item.dataMigrationSourceId,
                    td @(): item.errorDescription
                }
            })
        }
    }
    

    Parser Template

    I further used Parser template Connector to write the html content because the above table we will be sending as html table embedded in mail.

    <html>
      <head></head>
      <body>
        <p>Hello XYZ,</p>
        <p>Please find the error report for today's run. </p>
        <br />
      #[payload]
    <br /><br />
    <p>This is an automated mail, please do not reply.</p>
    <br />
    <p>Thanks and Regards,</p>
    </body>
    </html>
    

    Here payload denotes the html table we created above.

    Finally in the mail (Send) connector you can refer this html table as payload in the Content of Body.

    Note - Make sure you change the ContentType to text/html in the Body of mail Send connector