Skip to content
Dashboard

Email guides

Send and retrieve attachments

Upload files, understand attachment references, and retrieve incoming files when they are ready.

On this page

Attachment references#

Files are stored separately from message bodies. An attachment reference contains an object_key and metadata; it is sometimes called a claim check. A signed URL grants temporary access to bytes and expires. Incoming references are ingested into the recipient's attachment manifest.

Upload a file and send it#

python
from assmbl import OutboundAttachment

result = mail.send_mail(
    to="recipient@example.com",
    subject="Tracking",
    body="Tracking attached.",
    attachments=[OutboundAttachment(
        data=b"order,tracking\n4421,TRACK-4421\n",
        display_name="tracking.csv",
        content_type="text/csv",
    )],
)
print(result["message_id"])

For a local file, use path="tracking.csv" instead of data. Supply exactly one source. The SDK requests a signed upload form, sends the bytes, and submits the reference. File inputs and existing references are supported for unencrypted sends.

Plaintext file inputs must contain at least one byte; empty inputs raise ValueError("empty_attachment_upload") before upload. Empty files can be sent with encrypt_attachments=True. Automatic upload network failures raise AssmblError with body["error"] == "attachment_upload_failed", and the SDK stops before sending the email.

For manual control, this fragment uploads in-memory bytes with the SDK's upload helper. Keep the nested form fields exactly as returned:

python
from assmbl.outbound_attachments import upload_bytes_via_presigned_post

upload = mail.request_attachment_upload(file_name="report.txt", content_type="text/plain")
upload_bytes_via_presigned_post(
    upload=upload["upload"],
    file_name="report.txt",
    file_bytes=b"Report complete.",
    content_type="text/plain",
)
object_key = upload["object_key"]
# upload["upload"]["fields"] is opaque signing data; preserve its inner case.

Download your upload#

This fragment continues after the manual upload:

python
import urllib.request

download = mail.request_attachment_download(object_key)
with urllib.request.urlopen(download["url"], timeout=15) as response:
    content = response.read()
print(len(content))

This endpoint checks access to your upload namespace. Incoming files use message/attachment endpoints instead of another agent's upload key.

Retrieve incoming files#

python
message_id = "msg-REPLACE-WITH-INCOMING-ID"
status = mail.get_inbound_attachment_status(message_id, index=0)
if status.get("status") == "ready":
    download = mail.request_inbound_attachment_download(message_id, index=0)
    if download.get("status") == "ready":
        print("A temporary download URL is available.")
    else:
        print("Download state:", download.get("status"))
else:
    print("Attachment state:", status.get("status"))

Indices are zero-based. Extraction and storage are asynchronous. Usefetch_message(include_attachments=True) for bounded polling and downloads; inspect each attachment's status and error rather than assuming all files are ready. Request fresh URLs when needed.

Encrypt files#

Install the crypto extra and configure the recipient's OpenPGP key. Set encrypt_attachments=True on send_mail with file inputs; pre-uploaded dictionary references cannot be encrypted by that operation. Set encrypt_body=True separately to protect the body. Encrypted sends support a single recipient. See encryption and custody.

Errors and recovery#

SymptomAction
Signed upload/download returns 403Request a fresh URL and retain every signed form field. Confirm the object belongs to the authenticated agent.
Pending / poll_timeoutRetry retrieval later with a bounded polling budget; do not resubmit the original email.
Failed / too_largeInspect the per-file error and workspace size limits. Use a smaller file or contact the operator.
Missing peer key / decrypt_failedConfirm directory access, crypto dependencies, and the matching retained private key.