Attachments

Sending and receiving files with your agents.

What are Attachments?

An Attachment is a file that is associated with a Message. This can be anything from a PDF invoice to a CSV report or an image(though we don’t recommend sending images in the first email sent. We go more into this in the deliverability section). Your agents can both send Attachments in outgoing Messages and process Attachments from incoming Messages.

Sending Attachments

To send a file, include it in the attachments array when sending a Message. Each object in the array represents one file and must provide either content or url.

  • content: The Base64 encoded content of your file.
  • url: A URL that AgentMail can download without custom authentication headers or cookies. Redirects and pre-signed URLs are supported, and the final response must be a successful 2xx response.
  • filename (optional): The name of the file (e.g., invoice.pdf).
  • content_type (optional): The MIME type of the file (e.g., application/pdf).

Attachment size limits

InputLimitWhat counts toward the limit
Inline content6 MB per requestThe entire request, including the message body, metadata, and all attachments.
Remote urlAround 30 MB of attachments per messageThe combined size of the files AgentMail downloads from all attachment URLs.

If an inline request exceeds 6 MB, the API returns 413 Request Entity Too Large. For larger files, use URL-backed attachments and keep the combined attachment size around 30 MB per message.

To detect the inline limit proactively, measure the complete serialized request and leave room for the message body and metadata. Use url for larger attachments.

1import base64
2
3# A simple text file for this example
4
5file_content = "This is the content of our report."
6
7# You must Base64 encode the file content before sending
8
9encoded_content = base64.b64encode(file_content.encode()).decode()
10
11sent_message = client.inboxes.messages.send(
12inbox_id="reports@agentmail.to",
13to=["supervisor@example.com"],
14subject="Q4 Financial Report",
15text="Please see the attached report.",
16attachments=[{
17"content": encoded_content,
18"filename": "Q4-report.txt",
19"content_type": "text/plain"
20}]
21)

Retrieving Attachments

To retrieve an Attachment, you first need its attachment_id. You can get this ID from the attachments array on a Message object. Once you have the ID, you can download the file.

The API response for getting an attachment is the raw file itself, which you can then save to disk or process in memory.

From a Specific Message

If you know the Message an Attachment belongs to, you can retrieve it directly.

1inbox_id = "inbox_123"
2message_id = "<def456@agentmail.to>"
3attachment_id = "attach_789" # From the message object
4
5file_data = client.inboxes.messages.get_attachment(
6inbox_id=inbox_id,
7message_id=message_id,
8attachment_id=attachment_id
9)
10
11# Now you can save the file
12
13with open("downloaded_file.pdf", "wb") as f:
14f.write(file_data)

From a Specific Thread

Similarly, you can retrieve an Attachment if you know the Thread it’s in, which can be more convenient for multi-message conversations.

1inbox_id = "inbox_123"
2thread_id = "thread_abc"
3attachment_id = "attach_789" # From a message within the thread
4
5file_data = client.inboxes.threads.get_attachment(
6inbox_id=inbox_id,
7thread_id=thread_id,
8attachment_id=attachment_id
9)

Copy for Cursor / Claude

Copy one of the blocks below into Cursor or Claude for complete Attachments usage in one shot.

1"""
2AgentMail Attachments — copy into Cursor/Claude.
3
4Send: attachments=[{ content: base64_string, filename?, content_type? }] in messages.send/reply.
5Get attachment_id from message.attachments or thread.attachments.
6
7- messages.get_attachment(inbox_id, message_id, attachment_id) → bytes
8- threads.get_attachment(inbox_id, thread_id, attachment_id) → bytes
9"""
10import base64
11from agentmail import AgentMail
12
13client = AgentMail(api_key="YOUR_API_KEY")
14
15encoded = base64.b64encode(b"file content").decode()
16client.inboxes.messages.send(
17 "agent@agentmail.to",
18 to="user@example.com",
19 subject="Report",
20 text="See attachment",
21 attachments=[{"content": encoded, "filename": "report.txt", "content_type": "text/plain"}],
22)
23
24data = client.inboxes.messages.get_attachment("inbox@am.to", "<abc123@agentmail.to>", "att_456")
25with open("downloaded.pdf", "wb") as f:
26 f.write(data)