html - 通过 Mule 4 发送电子邮件时将 JSON 数组转换为表结构格式

标签 html json smtp dataweave mule4

我需要以表格格式发送错误报告,该报告需要作为电子邮件正文发送。 我正在将错误报告构建为 JSON 数组,并且需要进行 HTML 转换以将其构造为表格格式。

下面是 JSON 数组和作为电子邮件正文的预期结果。

[ { "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!" } ]

预期的电子邮件正文: enter image description here

如果格式不完全相同,则需要结果类似于表结构或更易读的格式。 有人可以帮我在 Mule 4 中构建这种格式

谢谢

最佳答案

Html 表(有效负载)

你可以在@()中自定义你的CSS。我只是保留了一个对您有用的示例

%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
            }
        })
    }
}

解析器模板

我进一步使用解析器模板连接器来编写html内容,因为上面的表格我们将作为嵌入邮件中的html表格发送。

<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>

这里的有效负载表示我们上面创建的 html 表。

最后,在邮件(发送)连接器中,您可以将此 html 表作为正文内容中的有效负载引用。

注意 - 确保在邮件正文发送连接器中将 ContentType 更改为 text/html

关于html - 通过 Mule 4 发送电子邮件时将 JSON 数组转换为表结构格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66632026/

相关文章:

单击提交按钮时 JavaScript 代码输出两次

linux - 在 Docker 容器中通过 curl (smtp) 发送电子邮件

email - 本地 SMTP 服务器

html - 如何使用 HTML 区分重叠的文本段?

jquery - 使用jquery选择点击的Td右侧的所有兄弟tds

javascript - 将 JSON HTML 字符串转换为 HTML

javascript - gzip 内容的 XMLHttpRequest 进度事件问题(FireFox 除外)

php - 如何将 csv 文件附加到 php 电子邮件(从 magento root 运行的代码)

javascript - 使用 jquery 将选定的第 n 个子元素包装在 div 中

python - 在 Python 中,让 json 不转义字符串