How to Send an Attachment via Email API for AI Agents
Learn how to send an attachment programmatically using REST APIs, presigned URLs, and MIME payloads. Built for AI agent developers using Robotomail.
John Joubert
Founder, Robotomail

Table of contents
- Why Attachments Are Harder Than They Look for AI Agents
- File Size Limits and the Base64 Encoding Trap
- Uploading Attachments Securely with Presigned URLs
- Building MIME and Multipart Payloads for Email Attachments
- Handling Inbound Attachments and Delivery Failures
- End-to-End Agent Workflow for Sending Attachments
You've got the file, the email API call looks right, and the message still fails somewhere between your agent and the recipient. That's the part most “how to send an attachment” guides skip, because they assume a person is clicking a paperclip, not an autonomous workflow pushing bytes through SMTP limits, MIME boundaries, and provider caps.
For AI agents, attachment handling is really a transport problem. The file has to survive encoding, fit the receiving system's limit, be packaged in a format the mailbox can interpret, and still be retrievable by the recipient on the other side. If any one of those steps breaks, the agent did “send” something, but nobody can use it.
Why Attachments Are Harder Than They Look for AI Agents
The hard part isn't choosing a file picker, because agents don't have one. The hard part is deciding whether the payload should be an attachment at all, then making sure the message survives the path from your system to someone else's inbox.
That's where most automation breaks down. A file can look small enough on disk, but the transport layer treats it differently once it's wrapped for email delivery. Major inbox providers also apply their own caps, so a message that works in one environment can fail in another if the encoded payload crosses a hidden limit. SMTP2GO's attachment-size guidance is useful here because it frames the question correctly, attach only when the file is small enough to survive transport.
Practical rule: if your agent can't validate the payload before it sends, it's not an attachment workflow yet. It's a hope.
A better mental model is to treat attachments like a delivery decision, not a UI action. If the file is small and the recipient's mailbox can handle it, attach it. If not, upload it elsewhere and send a link. That split is why create your event upload page is relevant even outside event software, because the same direct-link pattern solves the “too large for email” problem cleanly.
The rest of the work is infrastructure. Your agent needs to know when to encode, when to upload, how to construct the email body, and how to confirm that the recipient can open the file. That's a different skill set from clicking through Gmail or Outlook, and it's where reliable systems start.
File Size Limits and the Base64 Encoding Trap
An attachment workflow fails fast when the payload is bigger than the mail system can carry. In practice, that means checking the file before you send it, not after the message has already been assembled. Attachment guidance from SMTP2GO points to a simple rule, keep files small enough to survive transport, and treat larger files as link candidates.
The part that catches AI agents is encoding. Base64 encoding inflates the payload, so a file that looks acceptable on disk can become too large once it is wrapped for SMTP delivery. Your pre-flight check has to estimate the encoded size, not just read the source file size, or the send request can fail even though the original file seemed safe.

That is why the practical ceiling matters more than the nominal one. If a file is close to the limit, compress it, shorten it, or move it to a link-based path before you build the email. For agent workflows, the decision needs to happen before MIME assembly and before the send call, because by then the wrong delivery method has already been chosen.
The attachment concepts in Robotomail's attachment documentation show why size checks belong in application logic. The agent should decide early whether to attach the file or send a reference to it, because waiting for a bounce or a rejected message wastes a send attempt and leaves the workflow in an uncertain state.
Operational takeaway: size checks belong before MIME assembly, before upload, and before the send request.
Uploading Attachments Securely with Presigned URLs
Raw file bytes don't belong in a message payload unless the file is trivial. For agent workflows, the safer pattern is to upload the file first, then send only a reference to it. That keeps your API calls small, avoids repeating the file across retries, and keeps credentials away from the message body.
A presigned URL flow works well because it splits responsibilities cleanly. The agent asks for a temporary upload URL, sends the binary directly to storage, then confirms that the upload finished before it starts the email send. If the upload step fails, the retry happens on the file transfer, not on the message itself, which is far easier to reason about.
For a concrete upload reference, find the upload file API shows the same basic idea in a file-centric API design. That pattern maps neatly to agent mail systems because the message can reference a stored attachment instead of carrying the whole file inline.

The sequence is straightforward, but each step matters. The agent requests the upload URL, performs the PUT or equivalent binary upload, waits for confirmation, and only then builds the send request around the uploaded file reference. If your storage layer expects a content type, set it correctly before the upload, because mismatches can turn into downstream open failures even when the transfer itself succeeded.
Robotomail's send-emails-with-attachments guide fits well here because it shows why the upload-first pattern is the practical default for autonomous senders. The key win isn't just avoiding payload bloat, it's making the attachment step repeatable under retries, queue backlogs, and partial failures.
A good agent also watches for expiration windows. If the presigned URL lapses before the upload finishes, the send should stop and re-request a fresh URL rather than trying to salvage a stale reference. That keeps the workflow predictable, which matters more than elegance when the system is handling files on its own.
Building MIME and Multipart Payloads for Email Attachments
Once the file exists, the email still has to describe it correctly. That means the agent needs to build a valid MIME structure, usually with a multipart/mixed container for the whole message and a nested multipart/alternative block when the body includes both plain text and HTML.
The key field is not just the file itself, it's the metadata around it. Each attachment needs a proper Content-Disposition header, a filename, and the correct content type so the recipient's client knows whether it's a PDF, image, or something else. If the structure is wrong, the file can appear as garbled text, vanish from the message body, or get stripped by the receiving server.

What the payload needs
A developer-friendly payload usually tracks these pieces together:
- Message boundary: separates the top-level parts of the email.
- Text part: plain text for clients that don't render HTML.
- HTML part: the richer version of the body.
- Attachment part: the file content, plus a filename and disposition.
- Inline assets: only when something like a logo should render inside the body rather than appear as a downloadable file.
If the filename includes non-ASCII characters, encoding matters too. Mail clients differ in how they interpret names, so the attachment can arrive with a mangled label even when the file content itself is fine. That's one of those bugs that looks cosmetic until a support team can't identify which file the recipient downloaded.
Robotomail can abstract most of this, but the useful thing for an agent developer is understanding what the API is protecting you from. When you need low-level control, especially for a message with several files or embedded images, the structure above is what keeps the payload valid.
Handling Inbound Attachments and Delivery Failures
Sending a file is only half the job. Agents also need to process incoming attachments and react cleanly when outbound delivery fails. That means the workflow has to include webhook handling, validation, logging, and a suppression path when a recipient system won't accept the message.
Inbound attachment handling is easiest when the webhook payload gives you metadata first, then a secure way to fetch the file. That lets the agent validate type and size before any downstream processing starts. For support-oriented workflows, AgentStack's guide for support teams is a useful companion because threaded conversations and attachments tend to fail together in real inbox operations.
Practical rule: if a failure can be predicted from the payload, don't retry it blindly.
Delivery failures need a different response depending on the cause. A temporary transport issue can justify a retry, but a size rejection, unsupported format, or recipient server refusal usually needs user notification and a stop to repeated attempts. Otherwise the agent burns cycles repeating a send that can't succeed.
The cleanest operational pattern is to track three things together, the file metadata, the send status, and the failure reason. That gives the system enough context to decide whether to retry, switch to a link, or mark the recipient as temporarily blocked for this attachment path. If the same destination keeps rejecting similar payloads, the agent should stop treating that as a transient problem.
One more detail matters in practice. When the inbound or outbound path includes webhooks, sign them and verify them before acting on the data. That keeps attachment automation from turning into an open relay for bad payloads, and it makes your logs much more useful when something goes wrong.
End-to-End Agent Workflow for Sending Attachments
A quarterly report request is a good test case because it looks simple and exposes all the edge cases. The agent receives the PDF, checks the file size, decides whether the attachment should go inline or through a link, then uploads it if needed before building the email payload.
The useful part is the order. First comes validation, then upload, then message construction, then the send call. If the upload succeeds but the send fails, the agent shouldn't re-upload the file unless the reference expired. It should reuse the stored file and fix the email step instead.
A practical flow
- Validate the file first. Check the file type, size, and whether the recipient path can handle it.
- Choose the delivery mode. Attach it if the payload is small enough, or switch to a secure link if it isn't.
- Upload before sending. Use a presigned URL flow when the attachment needs to live outside the message body.
- Build the message payload. Add the attachment reference or MIME part, depending on the send path.
- Send and confirm. Wait for delivery confirmation through the mail platform's status channel.
- Handle failure deliberately. Retry only when the error is transient, otherwise notify the requester and stop looping.
That structure keeps the attachment logic reusable. The same module can serve a report export, a contract PDF, or an inbound reply workflow, because the agent makes the same core decisions every time. It also gives you a clean place to add observability, which is where most production attachment systems get saved during the first real incident.
Robotomail fits naturally into that pattern because it gives AI agents a mailbox-oriented workflow instead of forcing them to bolt email behavior onto generic infrastructure. It's designed for API-driven send and receive, with attachment handling that matches the way autonomous systems work.
If you're wiring attachment delivery into an agent workflow, build it around size checks, secure uploads, and delivery confirmation from the start. Visit Robotomail to see how an agent-native mailbox and attachment workflow can fit into your stack without the manual email plumbing.
Give your AI agent a real email address
One API call creates a mailbox with full send and receive. Webhooks for inbound, automatic threading, deliverability handled. 30-day money-back guarantee.
Related posts

10 Email Automation Best Practices for 2026
Master our top 10 email automation best practices for AI agents. Learn about DKIM, webhooks, threading, and more for secure, reliable agent-native email.
Read post
10 Best Email Automation Tools for Developers in 2026
Explore the top 10 email automation tools for developers. Compare APIs, deliverability, pricing, and AI agent use cases for Robotomail, SendGrid, and more.
Read post
Email Archiving: A Developer's Guide for AI Agents
A developer-focused guide to email archiving for AI agent platforms. Learn about regulations, architecture patterns, and API-driven implementation strategies.
Read post