首页 > 解决方案 > 将电子邮件正文和附件内容发送到 Mule 4 中的 REST(系统电子邮件)API

问题描述

我正在尝试实现一个 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有效载荷 在此处输入图像描述

测试.csv 在此处输入图像描述

标签: jsoncsvmuledataweave

解决方案


为您构建了一个快速示例流程.. 替换邮件 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"
            }
        ]
    }
}

更新的 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>

推荐阅读