【问题标题】:Send Email body and attachment content to a REST (system Email) API in Mule 4将电子邮件正文和附件内容发送到 Mule 4 中的 REST(系统电子邮件)API
【发布时间】:2021-04-22 02:31:51
【问题描述】:

我正在尝试实现一个 Rest API 以使用 SMTP 连接器发送任何类型的电子邮件(带或不带附件)。我希望系统电子邮件 API 从请求正文中动态获取电子邮件内容。

在我的客户端应用程序中,我正在构建如下的 json 结构。

%dw 2.0
import toBase64 from dw::core::Binaries
output application/json
---
{
  "body": {
    "fromAddress": "abcmule01@outlook.com",
    "toAddress": Mule::p('email.to') splitBy ",",
    "subject": Mule::p('email.subject'),
    "content": payload,
    "contentType": "text/html",
    "encoding": "UTF-8",
    "contentTransferEncoding": "Base64",
    "attachments": {
        "txtAttachment": vars.textPlain,
        "csvAttachment": vars.csvPayload
    },
    "smtpHost": Mule::p('smtp.host'),
    "smtpPort": Mule::p('smtp.port'),
    "smtpUser": Mule::p('smtp.username'),
    "smtpPassword": Mule::p('smtp.password')
  },
  "header": {
    "apiName": "test-email-flow",
    "apiVersion": "1.0",
    "transactionId": "test-12345",
    "correlationId": "test-12345"
  }
}

该功能仅适用于发送文本附件但是,如果我尝试发送 CSV 内容,则该内容无法在电子邮件附件中正确显示。

只是为了测试,我正在读取一个 CSV 文件并构建 CSV 内容 (vars.csvPayload),如下所示。

应用程序/csv;字符集=UTF-8;标题=“假”

Belinda Jameson,2017,Cushing House,148,3.52
Jeff Smith,2018,Prescott House,17-D,3.20
Sandy Allen,2019,Oliver House,108,3.48

转换后的附件请求结构如下。

  "attachments": {
      "txtAttachment": "This is the email text attachment",
      "csvAttachment": [
        {
          "column_0": "Belinda Jameson",
          "column_1": "2017",
          "column_2": "Cushing House",
          "column_3": "148",
          "column_4": "3.52"
        },
        {
          "column_0": "Jeff Smith",
          "column_1": "2018",
          "column_2": "Prescott House",
          "column_3": "17-D",
          "column_4": "3.20"
        },
        {
          "column_0": "Sandy Allen",
          "column_1": "2019",
          "column_2": "Oliver House",
          "column_3": "108",
          "column_4": "3.48"
        }
      ]
    }

但是,当我将“附件”传递给系统电子邮件 API 中的电子邮件发送组件时,它没有以正确的格式显示 csv 内容。你能帮我构建附件的请求结构吗?

下面是发送邮件子流程的xml配置文件

        <sub-flow name="sapi-core-email-send-mail-implementation" doc:id="177de084-c73b-4be1-ab10-90479ca98d5d" >
        <ee:transform doc:name="Transform Message" doc:id="207cdb76-fc5e-47b1-bebd-1cc75f50a5d0" >
            <ee:message >
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="emailTo" ><![CDATA[%dw 2.0
output application/json
---
"deeps.test@test.net.au,deeps2@test.net.au" splitBy ","]]></ee:set-variable>
                <ee:set-variable variableName="port" ><![CDATA[%dw 2.0
output application/java
---
payload.body.smtpPort]]></ee:set-variable>
                <ee:set-variable variableName="username" ><![CDATA[%dw 2.0
output application/java
---
payload.body.smtpUser]]></ee:set-variable>
                <ee:set-variable variableName="password" ><![CDATA[%dw 2.0
output application/java
---
payload.body.smtpPassword]]></ee:set-variable>
                <ee:set-variable variableName="host" ><![CDATA[%dw 2.0
output application/java
---
payload.body.smtpHost]]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <set-payload value='#[payload.body.content replace /([\r,\t,\n,"\"])/ with ""]' doc:name="Set Payload" doc:id="98c98bac-da72-4b9f-9207-5bab35ac228c" mimeType="text/html" encoding="UTF-8"/>
        <email:send doc:name="Send" doc:id="b3318d9e-e0ca-4750-9cb5-3bd7cb9ccdca" fromAddress="#[vars.requestBody.fromAddress]" toAddresses="#[vars.requestBody.toAddress]" subject="#[vars.requestBody.subject]" ccAddresses="#[vars.requestBody.ccAddress]" bccAddresses="#[vars.requestBody.bccAddress]" replyToAddresses="#[vars.requestBody.replyToAddress]" config-ref="Email_SMTP_Sapi_Core_Email">
            <email:body contentType="#[vars.requestBody.contentType]" encoding="#[vars.requestBody.encoding]" contentTransferEncoding="#[vars.requestBody.contentTransferEncoding]" >
            </email:body>
            <email:attachments ><![CDATA[#[vars.requestBody.attachments]]]></email:attachments>
        </email:send>
        <logger level="INFO" doc:name="Logger" doc:id="7a8d82aa-a23e-4bf9-bbad-26deb046c92f" message="Email has been successfully sent"/>
    </sub-flow>

并且 csv 附件内容在电子邮件附件中如下所示

    ¬í sr java.util.ArrayListxÒ™Ça I sizexp   w   sr java.util.LinkedHashMap4ÀN\lÀû Z accessOrderxr java.util.HashMapÚÁÃ`Ñ F 
loadFactorI     thresholdxp?@     w      t column_0t Belinda Jamesont column_1t 2017t column_2t 
Cushing Houset column_3t 148t column_4t 3.52x sq ~ ?@     w      t column_0t 
Jeff Smitht column_1t 2018t column_2t Prescott Houset column_3t 17-Dt column_4t 3.20x sq ~ ?@     w      t column_0t Sandy Allent column_1t 2019t column_2t Oliver Houset column_3t 108t column_4t 3.48x x

csv有效负载

test.csv

【问题讨论】:

  • 收到的电子邮件中的附件内容是什么样的?能否分享一下 Email Send 组件的 xml 配置?
  • 感谢您的回复,我已经提供了更多详细信息。请检查。
  • 你能分享你构建附件变量的sn-p吗?
  • 我只是想读取一个csv文件(test.csv)来构造csvPayload变量。

标签: json csv mule dataweave


【解决方案1】:

为您构建一个快速示例流程.. 替换邮件 SMTP 信息并尝试以下配置。

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tls="http://www.mulesoft.org/schema/mule/tls" xmlns:email="http://www.mulesoft.org/schema/mule/email"
    xmlns:file="http://www.mulesoft.org/schema/mule/file"
    xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
http://www.mulesoft.org/schema/mule/tls http://www.mulesoft.org/schema/mule/tls/current/mule-tls.xsd">
    <file:config name="File_Config" doc:name="File Config" doc:id="6c465807-159c-4d17-94e5-8882f8baa1dc" >
        <file:connection />
    </file:config>
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="d0708665-5ac5-4bd1-80fd-cf83b7c2da9d" >
        <http:listener-connection host="0.0.0.0" port="9191" />
    </http:listener-config>
    <email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="9d7e5139-1aed-4a4e-9cc7-1b8d0297d7c2" from="from@email.com">
        <email:smtps-connection host="smtp.gmail.com"  user="yourgmailaddress"  password="yourpwd" port="465" connectionTimeout="30" readTimeout="30" writeTimeout="30">
            <tls:context>
                <tls:trust-store insecure="true" />
            </tls:context>
        </email:smtps-connection>
    </email:smtp-config>
    <flow name="test-send-emailFlow" doc:id="462b24c4-f53b-4483-b9f4-609c4190e28c" >
        <http:listener doc:name="Listener" doc:id="e764fbc7-fd3e-4121-bdb5-94eb9819184e" config-ref="HTTP_Listener_config" path="/sm"/>
        <file:read doc:name="Read" doc:id="ca92561e-1dfa-44d4-a962-59b1493c198d" config-ref="File_Config" path="path/to/file/test.csv"/>
        <ee:transform doc:name="Transform Message" doc:id="5d7da7cd-1951-4607-ac82-e2f9002716f2" >
            <ee:message >
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="attachFile" ><![CDATA[%dw 2.0
output application/java
---
{
    "message": write(payload,"application/csv")
}
]]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <email:send doc:name="Send" doc:id="c2ab4dd1-5401-4b64-abe3-9a95af371a31" config-ref="Email_SMTP" subject="CSV File">
            <email:to-addresses >
                <email:to-address value='"toemail"' />
            </email:to-addresses>
            <email:body contentType="text/plain" encoding="UTF-8">
                <email:content ><![CDATA[#["Hello"]]]></email:content>
            </email:body>
            <email:attachments ><![CDATA[#[vars.attachFile]]]></email:attachments>
        </email:send>
    </flow>
</mule>

我能够使用此配置很好地测试您的示例 csv 文件。

================================================ ========================

输入以下流程

{
    "attachments": {
        "txtAttachment": "This is the email text attachment",
        "csvAttachment": [{
                "column_0": "Belinda Jameson",
                "column_1": "2017",
                "column_2": "Cushing House",
                "column_3": "148",
                "column_4": "3.52"
            },
            {
                "column_0": "Jeff Smith",
                "column_1": "2018",
                "column_2": "Prescott House",
                "column_3": "17-D",
                "column_4": "3.20"
            },
            {
                "column_0": "Sandy Allen",
                "column_1": "2019",
                "column_2": "Oliver House",
                "column_3": "108",
                "column_4": "3.48"
            }
        ]
    }
}

Updated Flow 处理传入的 JSON

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tls="http://www.mulesoft.org/schema/mule/tls" xmlns:email="http://www.mulesoft.org/schema/mule/email"
    xmlns:file="http://www.mulesoft.org/schema/mule/file"
    xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
http://www.mulesoft.org/schema/mule/tls http://www.mulesoft.org/schema/mule/tls/current/mule-tls.xsd">
    <file:config name="File_Config" doc:name="File Config" doc:id="6c465807-159c-4d17-94e5-8882f8baa1dc" >
        <file:connection />
    </file:config>
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="d0708665-5ac5-4bd1-80fd-cf83b7c2da9d" >
        <http:listener-connection host="0.0.0.0" port="9191" />
    </http:listener-config>
    <email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="9d7e5139-1aed-4a4e-9cc7-1b8d0297d7c2" from="fromemail">
        <email:smtps-connection host="smtp.gmail.com"  user="youremailId"  password="yourpasswd" port="465" connectionTimeout="30" readTimeout="30" writeTimeout="30">
            <tls:context>
                <tls:trust-store insecure="true" />
            </tls:context>
        </email:smtps-connection>
    </email:smtp-config>
    <flow name="test-send-emailFlow" doc:id="462b24c4-f53b-4483-b9f4-609c4190e28c" >
        <http:listener doc:name="Listener" doc:id="e764fbc7-fd3e-4121-bdb5-94eb9819184e" config-ref="HTTP_Listener_config" path="/sm"/>
        <ee:transform doc:name="Transform Message" doc:id="5d7da7cd-1951-4607-ac82-e2f9002716f2" >
            <ee:message >
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="attachFile" ><![CDATA[%dw 2.0
output application/java
---
{
    "message": write((payload.attachments.csvAttachment map {
           ($ mapObject {
              ("column_$$"): $
           })
}),"application/csv", {"header":false})
}
]]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <email:send doc:name="Send" doc:id="c2ab4dd1-5401-4b64-abe3-9a95af371a31" config-ref="Email_SMTP" subject="CSV File">
            <email:to-addresses >
                <email:to-address value='"youremailaddress"' />
            </email:to-addresses>
            <email:body contentType="text/plain" encoding="UTF-8">
                <email:content ><![CDATA[#["Hello"]]]></email:content>
            </email:body>
            <email:attachments ><![CDATA[#[vars.attachFile]]]></email:attachments>
        </email:send>
    </flow>
</mule>

【讨论】:

  • 谢谢萨利姆,是的。如果我在同一流程中构建 csv 内容,我能够正确发送 csv 附件。但是我在通过 HTTP 请求调用将 CSV 内容从客户端 API 发送到 JSON 有效负载中的系统电子邮件 API 时遇到问题。由于系统电子邮件 API 应该能够根据请求动态发送任何类型的附件,因此我正在寻找一种构建请求的最佳方法,以通过 Rest API 发送任何形式的附件。请建议是否有任何最好的方法来做到这一点。
  • 当然.. 因此,如果您在代码中的 json 作为输入发送到系统 API,您需要调整 DW 以将其更改为 CSV,然后将其传递到电子邮件发送 。我这样做了,并通过发送您在问题中的“附件”json 对其进行了测试,并且我能够很好地发送电子邮件,并将 csv 附加到电子邮件中。我已在我放入的原始流程下方添加了更新的流程。
  • 非常感谢您的意见。根据您的 cmets,现在我在构建请求本身时将 csv 内容编写为 application/csv,而不是在系统电子邮件 API 上进行更改,这已经解决了这个问题。 (附上代码sn-p)
  • 以下更改已解决该问题: "attachments": { "txtAttachment": vars.textPlain, "csvAttachment": write((vars.csvPayload),"application/csv", {"header" :false}) }
  • 很高兴听到这个消息。您能否接受答案或建议进行修改,以便将来可以帮助其他人,以防他们遇到类似问题。
猜你喜欢
  • 2016-11-30
  • 2018-07-24
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多