Receiving
Receiving attachments
Download the attachments of emails you receive.
The email.received webhook lists each attachment's id, filename, content_type and size, but not its content. Download the content with the API.
Find the attachment IDs
The webhook payload and GET /inbound/emails/{id} both list the attachments of a received email:
curl https://api.usend.et/inbound/emails/a39999a6-88e3-48b1-888b-beaabcde1b33 \
-H "Authorization: Bearer us_123456789"{
"object": "inbound_email",
"id": "a39999a6-88e3-48b1-888b-beaabcde1b33",
"...": "...",
"attachments": [
{ "id": "2a0c9ce0-3112-4728-976e-47ddcd16a318", "filename": "screenshot.png", "content_type": "image/png", "size": 182044 }
]
}Download an attachment
GET /inbound/emails/{id}/attachments/{attachment_id} returns the file itself. It needs your API key:
curl -o screenshot.png \
https://api.usend.et/inbound/emails/a39999a6-88e3-48b1-888b-beaabcde1b33/attachments/2a0c9ce0-3112-4728-976e-47ddcd16a318 \
-H "Authorization: Bearer us_123456789"Example: save every attachment
import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import { Usend } from '@usend/node';
const usend = new Usend(process.env.USEND_API_KEY);
const headers = { Authorization: `Bearer ${process.env.USEND_API_KEY}` };
export async function saveAttachments(emailId) {
const { data: email, error } = await usend.received.get(emailId);
if (error) throw new Error(error.message);
for (const att of email.attachments) {
const url = `https://api.usend.et/inbound/emails/${emailId}/attachments/${att.id}`;
const file = await fetch(url, { headers });
await writeFile(path.basename(att.filename), Buffer.from(await file.arrayBuffer()));
}
}import os
import urllib.request
from usend import Usend
usend = Usend(os.environ["USEND_API_KEY"])
def save_attachments(email_id):
email = usend.received.get(email_id)
for att in email["attachments"]:
req = urllib.request.Request(
f"https://api.usend.et/inbound/emails/{email_id}/attachments/{att['id']}",
headers={"Authorization": f"Bearer {os.environ['USEND_API_KEY']}"},
)
with urllib.request.urlopen(req) as resp, open(os.path.basename(att["filename"]), "wb") as f:
f.write(resp.read())Warning
Attachments come from strangers. Never trust filename as a path (sanitize it first), check content_type and size, and scan files before you open them.
Attachments are kept for your plan's data retention period.