Json 将电子邮件正文和附件内容发送到Mule 4中的REST(系统电子邮件)API

Json 将电子邮件正文和附件内容发送到Mule 4中的REST(系统电子邮件)API,json,csv,mule,dataweave,Json,Csv,Mule,Dataweave,我正在尝试实现一个RESTAPI来使用SMTP连接器发送任何类型的电子邮件(带附件或不带附件)。我希望系统电子邮件API能够从请求主体动态获取电子邮件内容 在我的客户端应用程序中,我正在构建json结构,如下所示 %dw 2.0 import toBase64 from dw::core::Binaries output application/json --- { "body": { "fromAddress": "abcmule0

我正在尝试实现一个RESTAPI来使用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),如下所示

%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;字符集=UTF-8;header=“false”

转换后,附件的请求结构如下所示

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

test.csv

为您构建了一个快速的样本流。。替换邮件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>
更新的流处理传入的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>

为您构建了一个快速的样本流。。替换邮件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>
更新的流处理传入的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>



收到的电子邮件中的附件内容是什么样的?您还可以共享电子邮件发送组件的xml配置吗?感谢您的回复,我已经提供了其他详细信息。请检查。您能否共享构建附件变量的代码段?我只是尝试读取一个csv文件(test.csv)来构建csvPayload变量。收到的电子邮件中的附件内容是什么样的?您还可以共享电子邮件发送组件的xml配置吗?感谢您的回复,我已经提供了其他详细信息。请检查。您能否共享构建附件变量的代码段?我只是尝试读取一个csv文件(test.csv)来构建csvPayload变量。谢谢Salim,是的。如果我在同一流程中构建csv内容,我能够正确发送csv附件。但在JSON负载中通过HTTP请求调用将CSV内容从客户端API发送到系统电子邮件API时,我面临着这个问题。由于系统电子邮件API应该能够根据请求动态发送任何类型的附件,因此我正在寻找一种最佳方法来构造请求,以便通过Rest API发送任何形式的附件。请建议是否有最好的方法来做到这一点。当然。。因此,如果代码中的json作为输入发送到系统API,则需要调整DW以将其更改为CSV,然后将其传递到电子邮件发送。我这样做了,并通过发送“附件”json测试了它,您在您的问题中有,我能够发送电子邮件与csv附加到相同的罚款。我已经在原来的流程下添加了更新的流程。非常感谢您的输入。根据您的评论,现在我正在将csv内容编写为application/csv,同时构建请求本身,而不是在系统电子邮件API中进行更改,这已经解决了问题。(附加代码片段)下面的更改解决了问题:“附件”:{“txtatchment”:vars.textplane,“csvatachment”:write((vars.csvPayload),“application/csv”,{“header”:false})很高兴听到这个消息。请您接受答案或建议进行编辑,以便答案在将来帮助其他人,以防他们遇到类似问题。谢谢Salim,是的。如果我在同一流程中构建csv内容,我能够正确发送csv附件。但在JSON负载中通过HTTP请求调用将CSV内容从客户端API发送到系统电子邮件API时,我面临着这个问题。由于系统电子邮件API应该能够根据请求动态发送任何类型的附件,因此我正在寻找一种最佳方法来构造请求,以便通过Rest API发送任何形式的附件。请建议是否有最好的方法来做到这一点。当然。。因此,如果代码中的json作为输入发送到系统API,则需要调整DW以将其更改为CSV,然后将其传递到电子邮件发送。我这样做了,并通过发送“附件”json测试了它,您在您的问题中有,我能够发送电子邮件与csv附加到相同的罚款。我已经在原来的流程下添加了更新的流程。非常感谢您的输入。根据您的评论,现在我正在将csv内容编写为application/csv,同时构建请求本身,而不是在系统电子邮件API中进行更改,这已经解决了问题。(附加代码片段)下面的更改解决了问题:“附件”:{“txtatchment”:vars.textplane,“csvatachment”:write((vars.csvPayload),“application/csv”,{“header”:false})很高兴听到这个消息。请您接受答案或建议进行编辑,以便答案能够在将来帮助其他人,以防他们遇到类似问题。