How to send email with attachments using an API (with code)
Send PDFs, images and CSVs as email attachments from Node.js, Python, PHP and cURL, plus the size limits and deliverability rules to know.
Invoices, receipts, reports and tickets often need a file attached. With an email API there's no MIME to build by hand: you base64-encode the file and add it to an attachments array.
"attachments": [
{ "filename": "invoice-1042.pdf", "content": "JVBERi0xLjQKJ..." }
]
Stack Emails accepts up to 20 attachments and 10 MB in total per email. The content type is detected from the file extension, or you can pass content_type.
Node.js
import { readFile } from "node:fs/promises";
const pdf = await readFile("invoice-1042.pdf");
await fetch("https://stackemails.com/v1/emails", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.STACKEMAILS_API_KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({
from: "Acme Billing <billing@mail.acme.com>",
to: "ada@example.com",
subject: "Invoice INV-1042",
html: "<p>Your invoice is attached.</p>",
attachments: [{ filename: "invoice-1042.pdf", content: pdf.toString("base64") }],
}),
});
Python
import base64, os, requests
with open("invoice-1042.pdf", "rb") as f:
content = base64.b64encode(f.read()).decode()
requests.post(
"https://stackemails.com/v1/emails",
headers={"Authorization": f"Bearer {os.environ['STACKEMAILS_API_KEY']}"},
json={
"from": "Acme Billing <billing@mail.acme.com>",
"to": "ada@example.com",
"subject": "Invoice INV-1042",
"html": "<p>Your invoice is attached.</p>",
"attachments": [{"filename": "invoice-1042.pdf", "content": content}],
},
timeout=15,
).raise_for_status()
PHP
$payload = [
"from" => "Acme Billing <billing@mail.acme.com>",
"to" => "ada@example.com",
"subject" => "Invoice INV-1042",
"html" => "<p>Your invoice is attached.</p>",
"attachments" => [[
"filename" => "invoice-1042.pdf",
"content" => base64_encode(file_get_contents("invoice-1042.pdf")),
]],
];
// POST $payload as JSON to https://stackemails.com/v1/emails (see the PHP guide)
Full request code: Node.js, Python, PHP.
cURL
CONTENT=$(base64 -w0 report.csv) # on macOS: base64 -i report.csv
curl -X POST https://stackemails.com/v1/emails \
-H "Authorization: Bearer $STACKEMAILS_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"from\":\"reports@mail.acme.com\",\"to\":\"ada@example.com\",\"subject\":\"Daily report\",\"text\":\"Attached.\",\"attachments\":[{\"filename\":\"report.csv\",\"content\":\"$CONTENT\"}]}"
Size limits and why they matter
Base64 makes files about 33% larger, and most receiving servers cap total message size somewhere between 10 and 35 MB (Gmail accepts up to 25 MB when receiving). Large attachments also slow delivery and can trigger spam filtering.
For anything big, send a link instead: upload the file to storage, generate a signed URL that expires, and put the link in the email.
Deliverability tips for attachments
- Stick to common, expected types: PDF, PNG, JPG, CSV, ICS. Executables, scripts and macro-enabled Office files are blocked or quarantined by most providers.
- Name files clearly (
invoice-1042.pdf, notfile.pdf). - Always include body text explaining the attachment; an empty body with a file looks like malware.
- Authenticate your domain with SPF, DKIM and DMARC.
Need a starting point? Use the free invoice and payment receipt email templates.
Enjoyed this article?
See more from Stack Emails in Google Search.