DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • Your AI-Generated Reports Have No Paper Trail
  • How Does an LLM Request and Response Cycle Work? A Full Walkthrough
  • The Java Story: The Official Documentary Is Here
  • No Observability Tool Is the “Best”

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.

By 
Rajat Vishwakarma user avatar
Rajat Vishwakarma
·
Mar. 03, 21 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
6.8K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

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

 Creating a sample flow list


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.Implementation flow chart

Step 3: Add the send component of the email

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.


Specify the key name

 

Step 5: Run the project. We will be getting an email with an attachment as a zip file .

Run the project


Step 6:  Download the file and open it using the WINRAR extractor

Download file and open using WINRAR extractor.


Project XML: 

XML
 




xxxxxxxxxx
1
54


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
3
<mule xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
4
    xmlns:compression="http://www.mulesoft.org/schema/mule/compression"
5
    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
6
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
7
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
8
http://www.mulesoft.org/schema/mule/compression http://www.mulesoft.org/schema/mule/compression/current/mule-compression.xsd
9
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
10
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd">
11
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="1cda4b9c-a806-455b-a6df-de5d16072907" >
12
        <http:listener-connection host="0.0.0.0" port="8081" ></http:listener>
13
    </http:listener-config>
14
    <email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="8d42239a-d948-4f26-89f0-b410ffddcb2b" >
15
        <email:smtp-connection host="smtp.gmail.com" port="587" user="<<yourEmailID>>" password="<<yourEmailPassword>>" >
16
            <email:properties >
17
                <email:property key="mail.smtp.starttls.enable" value="true" ></email:property>
18
            </email:properties>
19
        </email:smtp-connection>
20
    </email:smtp-config>
21
    <flow name="zip-file-pox" doc:id="45e9e355-8cd0-4cd6-8888-b927820b0778" >
22
        <http:listener doc:name="Listener" doc:id="a752c4dd-fdf6-4444-bb13-c044fe51af85" path="/readFile" config-ref="HTTP_Listener_config"></http:listener>
23
        <file:list doc:name="listAllFiles" doc:id="34fbc3e5-a405-4979-ab41-ae5df6a98595" directoryPath="<<directoryToListTheFiles>>" ></file:list>
24
        <ee:transform doc:name="setting Payload" doc:id="e8a19536-771b-4cb3-8286-e19565c6aaef">
25
            <ee:message>
26
                <ee:set-payload><![CDATA[%dw 2.0
27
output application/java
28
---
29
{(payload map {
30
    
31
    ($.attributes.name) : $.payload
32
    
33
})}]]></ee:set-payload>
34
            </ee:message>
35
        </ee:transform>
36
        <compression:archive doc:name="zippingFile" doc:id="51a8ceab-cc3c-4b78-9fef-bcdd3594574c">
37
            <compression:archiver>
38
                <compression:zip-archiver ></compression:zip>
39
            </compression:archiver>
40
        </compression:archive>
41
        <email:send doc:name="emailSend" doc:id="7e9b608f-35ff-4658-a029-e2fe4448b59d" config-ref="Email_SMTP" subject="ZIP file">
42
            <email:to-addresses >
43
                <email:to-address value="<<EmailAddressForSendingTheZIP>>" />
44
            </email:to-addresses>
45
            <email:body >
46
                <email:content ><![CDATA[#[""]]]></email:content>
47
            </email:body>
48
            <email:attachments ><![CDATA[#[{
49
    "sampleFile.zip": payload
50
}]]]></email:attachments>
51
        </email:send>
52
    </flow>
53
</mule>
54



Happy learning!

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook