How can I add a CSV or excel to an email sent from Automate?

How can I send an email that contains an attachment via Foundry automate?

Here is a way that this can be done using Functions and Attachments, along with Automate.

Sending a CSV/Excel an attachment to an email using Foundry Automate

Expected Result

High Level Steps

  1. Create a CSV/Excel as an attachment to an object
  2. Create an automation which sends an email notification
  3. In that email notification, use the object’s attachment property to set the email’s attachment

Detailed Steps

1.1 Create a CSV as an attachment to an object

import {Attachments, Attachment, OntologyEditFunction, Edits, } from "@foundry/functions-api";
import { Uuid } from "@foundry/functions-utils";
import { yourObjectType } from "@foundry/ontology-api";

@Edits(yourObjectType)
@OntologyEditFunction()
public async createAndStoreCsv(): Promise<void> {
    const yourObject = Objects.create().yourObjectType(Uuid.random());
    // Create CSV content
    const csvContent = "column1,column2,column3\nvalue1,value2,value3";
    // Convert CSV content to Blob
    const csvBlob = new Blob([csvContent], { type: "text/csv;charset=utf-8" });
    // Upload the CSV as an attachment
    const attachment: Attachment = await Attachments.uploadFile("my_data.csv", csvBlob);
    yourObject.attachment = attachment
}

1.1 Or Create an Excel attachment

import {Attachments, Attachment, OntologyEditFunction, Edits, } from "@foundry/functions-api";
import { Uuid } from "@foundry/functions-utils";
import { Workbook } from "exceljs";
import { yourObjectType } from "@foundry/ontology-api";

@OntologyEditFunction()
public async createAndAttachExcelFile(): Promise<void> {
    const yourObject = Objects.create().yourObjectType(Uuid.random())
    // Create a new workbook and worksheet
    const workbook = new Workbook();
    const worksheet = workbook.addWorksheet('Sample Sheet');

    // Add some data to the worksheet
    worksheet.addRow(['Header1', 'Header2', 'Header3']);
    worksheet.addRow(['Data1', 'Data2', 'Data3']);

    // Create a Blob from the workbook
    const buffer = await workbook.xlsx.writeBuffer();
    const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });

    // Upload the Blob as an attachment
    const attachment: Attachment = await Attachments.uploadFile("sample.xlsx", blob);

    // Attach the file to the object
    yourObject.attachment = attachment;
}

2. Create an automation which sends an email notification

  1. Select the option to Send one notification for each of your object in the set (my object here is called Plat Map)

3. In that email notification, use the object’s attachment property to set the email’s attachment

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.