Auto download csv (when a button is pressed) from table and send it via email as attachment?

Hi whats the best approach to auto download csv (when a button is pressed) from a data table and send it via email as attachment?

thank you

1 Like

Hey @agaitan026!

It sounds as though this would be best done in two steps. You can create a custom button on your table and attach it to a JS query that triggers the query you'd use to send your email and also downloads the table's data:

The emailSender query itself depends on which integration you're using to send emails. With the SMTP integration, for instance, you can specify the attachment as follows:

This first uses the built-in PapaParse library to construct a csv string from your table data, then converts it to base64 using btoa since that is the format the integration expects.

Let us know if that helps or if you're using a different email sender!

1 Like

[{ data: { data: {{btoa(Papa.unparse(table1.data))}}, name: "table_data.csv", contentType: "csv" }]

getting this

my bad [{ data: {{btoa(Papa.unparse(table1.data))}}, name: "table_data.csv", contentType: "csv" }]

works perfect! thank you @Kabirdas

hi @Kabirdas any chance to send attachment as XLSX excel? instead of csv?

thank you

Yep! It takes a bit more than just using PapaParse though - there's a great post here that goes over how to create XLSX files in Retool. Instead of downloading the file at the end, you'll want to pass it to your email sender by either returning the result and then referencing the value of the js query/transformer or passing the result as additionalScope.

1 Like

Thank you