Create a Zip File and Send It as an Email Attachment in Mule 4
In this article, see a tutorial on how to create a zip file and send it as an email attachment in Mule 4.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
We will be using the Compression Module of Mule 4 for creating a zip file and email component to send as attachments.
Prerequisites:
- Compression Module (Install the compression Module from Mulesoft)
- Gmail Account (Third-party Application enabled)
Step 1: Creating a sample flow to list all the files in the directory
Step 2: Add the Compression Module Archive component. This component creates the zip of all the files read by the LIST component.
We will be adding the transform
message to create a map of the key-value pair of Filename and the Payload of the file.
Step 3: Add the send component of the email
Step 4: We need to use the below config for the send component:
Note: If we don't add the empty quotes "" in the content tab, an additional file will be sent to overcome that, so we use "". And also, we need to specify the key name as "fileName.zip" so that once we download the file, we should able to open a zip file.
Step 5: Run the project. We will be getting an email with an attachment as a zip file .
Step 6: Download the file and open it using the WINRAR extractor
Project XML:
xxxxxxxxxx
<mule xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:compression="http://www.mulesoft.org/schema/mule/compression"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:file="http://www.mulesoft.org/schema/mule/file" 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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.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/compression http://www.mulesoft.org/schema/mule/compression/current/mule-compression.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/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="1cda4b9c-a806-455b-a6df-de5d16072907" >
<http:listener-connection host="0.0.0.0" port="8081" ></http:listener>
</http:listener-config>
<email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="8d42239a-d948-4f26-89f0-b410ffddcb2b" >
<email:smtp-connection host="smtp.gmail.com" port="587" user="<<yourEmailID>>" password="<<yourEmailPassword>>" >
<email:properties >
<email:property key="mail.smtp.starttls.enable" value="true" ></email:property>
</email:properties>
</email:smtp-connection>
</email:smtp-config>
<flow name="zip-file-pox" doc:id="45e9e355-8cd0-4cd6-8888-b927820b0778" >
<http:listener doc:name="Listener" doc:id="a752c4dd-fdf6-4444-bb13-c044fe51af85" path="/readFile" config-ref="HTTP_Listener_config"></http:listener>
<file:list doc:name="listAllFiles" doc:id="34fbc3e5-a405-4979-ab41-ae5df6a98595" directoryPath="<<directoryToListTheFiles>>" ></file:list>
<ee:transform doc:name="setting Payload" doc:id="e8a19536-771b-4cb3-8286-e19565c6aaef">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/java
---
{(payload map {
($.attributes.name) : $.payload
})}]]></ee:set-payload>
</ee:message>
</ee:transform>
<compression:archive doc:name="zippingFile" doc:id="51a8ceab-cc3c-4b78-9fef-bcdd3594574c">
<compression:archiver>
<compression:zip-archiver ></compression:zip>
</compression:archiver>
</compression:archive>
<email:send doc:name="emailSend" doc:id="7e9b608f-35ff-4658-a029-e2fe4448b59d" config-ref="Email_SMTP" subject="ZIP file">
<email:to-addresses >
<email:to-address value="<<EmailAddressForSendingTheZIP>>" />
</email:to-addresses>
<email:body >
<email:content ><![CDATA[#[""]]]></email:content>
</email:body>
<email:attachments ><![CDATA[#[{
"sampleFile.zip": payload
}]]]></email:attachments>
</email:send>
</flow>
</mule>
Happy learning!
Opinions expressed by DZone contributors are their own.
Comments