Introduction
This blog explain the tricks to convert csv data to plain text for the purpuse of send csv as attachment. The Mulesoft document can be found at here1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < file:read path = "/file.json" > < email:send config-ref = "config" > < email:to-addresses > < email:to-address value = "example@domain.com" > </ email:to-address ></ email:to-addresses > < email:body > < email:content > <!--[CDATA["<h1--> Hello this is an important message"]]></ email:content > </ email:body > < email:attachments > #[{ 'csv-attachment' : payload }] </ email:attachments > </ email:send > </ file:read > |
Code Details
As shown in the follow snapshot, we need to perform two transformations:- from csv to base64 binary
- from base64 binary to plain text>
Step 1: Initial code JSON to CSV
1 2 3 4 5 6 7 8 9 10 11 12 13 | %dw 2.0 output application/csv separator="," --- [ { name: "Gary Liu", ssn: "1234" }, { name: "John Smith", ssN: "4567" } ] |
Step 2: CSV to Base64 Binary
1 2 3 4 5 | %dw 2.0 import * from dw::core::Binaries output text/plain --- toBase64(write (payload, 'application/csv' )) |
Step 3: Base64 Binary to CSV Plain Text
1 2 3 4 5 | %dw 2.0 import fromBase64 from dw::core::Binaries output text/plain --- fromBase64(payload) |