Send CSV via email with Workflows

Hi everyone,

I'm trying to build a Workflow that retrieves some data from the database and sends it as a CSV via email. My workflow has 3 blocks:

  1. A PostgreSQL query retrieving the data
  2. A JS block running "Papa.unparse" over the retrieved data to transform it to the CSV format
  3. A SMTP block to send the data via email

Everything is working fine up until the unparse, but I'm not sure how I can make use of the data returned by the JS and plug it into the SMTP query so it's sent as a CSV file via email.

Here's an image of where I'm stuck at. In the SMTP query, I clicked "fx" to set the value dynamically. I then tried using both "{{code}}" and {{code.data}}" as a value, but I then get the error below when I try to run the SMTP block:

Hey @grefosco!

Does it work if you use [{data: {{btoa(code.data)}}, name: "attachment.csv", fileType: "csv" }]?

Exact same issue attempting the same thing
three blocks
data query
Papa.unparse
smtp block = [{data: {{btoa(code.data)}}, name: "attachment.csv", fileType: "csv" }]

Tried the suggestion, still the same error "Use File Input component to upload a file and ensure file is uploaded before query is fired."

I found the issue,
Attachments doesn't accept fileType:
it needs contentType: 'csv'

this will work

[{
  data: '{{btoa(code.data)}}',
  name: 'fileName.csv',
  contentType: 'csv'
}]
2 Likes

Is there a way to also send xlsx? @Kabirdas @jamesashton

Yep! You can add the SheetJS library which is particularly useful for handling xlsx files to your Workflow @Leon:

Similar to PapaParse, it has APIs for reading and writing data. The following code takes the result of a Retool DB query and returns an xlsx file as a base64 string:

const rows = query1.data;
const worksheet = xlsx.utils.json_to_sheet(rows);
const workbook = xlsx.utils.book_new();
xlsx.utils.book_append_sheet(workbook, worksheet, "Colors");
return xlsx.write(workbook, {type: "base64"});

From there you should be able to attach the file as @jamesashton mentioned:

(Note that btoa isn't necessary here as it is already a base64 string)