# Sending Multipart Attachments

## Overview

Some third-party systems only support receiving attachments using **`multipart/form-data`** requests.

Because multipart requests require the payload body, boundaries, and headers to be constructed together, this is implemented using a **Unifi Multipart Helper** service that assembles the multipart body as a temporary attachment, which can then be streamed as the outbound payload.

For more information on general attachment sending, see the [**Sending Attachments**](https://docs.sharelogic.com/unifi/configure/sending-attachments#streaming-attachments) documentation.

{% hint style="info" %}
The **Unifi Multipart Helper** is provided separately. Please contact us via **support** and we will share it with you.
{% endhint %}

***

## Sending Multipart Attachments

The multipart helper is used from the **Stage to Request** script to construct and stream a multipart request containing one or more attachments.

In this stage, Unifi:

* Collects eligible bonded attachments using `AttachmentSender`
* Builds a multipart body containing those attachments
* Stores the multipart body as a temporary attachment
* Streams that temporary attachment as the outbound request payload

### Stage to Request Script

```javascript
// ------------------------------------------------------------------
// Build and stream a multipart/form-data request containing attachments
// ------------------------------------------------------------------
var mp = new UnifiMultipartHelper();

// Use the current transaction as the host record for temporary
// attachments - these will be cleaned up later by an Event Action
mp.setHostRecord(transaction);

// Add each bonded attachment to the multipart body
var sender = new x_snd_eb.AttachmentSender(transaction, bond);
while (sender.next()) {
  mp.addAttachment('file', sender.attachment_id);
}

// Call the service to create the multipart body as a temporary attachment
var data_attachment_id = mp.createBody();

// Prepare request to stream the temporary attachment
headers['Content-Type'] = mp.getContentType();
payload = 'sys_attachment:' + data_attachment_id;
```

**How this works**

* Each call to `mp.addAttachment()` adds an attachment to the multipart body
* `mp.createBody()` assembles the multipart payload and writes it as a temporary attachment
* Setting the payload to `sys_attachment:<id>` instructs Unifi to **stream** the multipart body
* The `Content-Type` header includes the correct multipart boundary

***

## Cleaning Up Temporary Attachments

The multipart body is stored as a temporary attachment and must be removed once the transaction completes.

This is handled using an **Event Action**.

### Event Action: Remove Temporary Attachments

**Description**\
Automatically remove the temporary attachments created for streaming multipart/form-data content.

**Run Conditions**

* **Event:** `transaction.complete`

**Action Script**

```javascript
// Remove temporary attachments from the current transaction
var mp = new UnifiMultipartHelper();
mp.removeTemporaryAttachments(current);
```
