Attachments travel inside the JSON as base64. Mailcandor decodes them and builds the email.
To attach a file
PDF=$(base64 < invoice-1042.pdf | tr -d '\n')
curl https://api.mailcandor.com/v1/emails \
-H "Authorization: Bearer $MAILCANDOR_KEY" \
-H "Content-Type: application/json" \
-d '{
"from": "Acme <billing@acme.com>",
"to": "sam@example.com",
"subject": "Invoice 1042",
"html": "<p>Your invoice is attached.</p>",
"attachments": [
{ "filename": "invoice-1042.pdf", "content": "'"$PDF"'" }
]
}'
Attachment fields
| Field | Required | Notes |
|---|---|---|
filename |
Yes | Up to 255 characters, no slashes. Recipients see it. |
content |
Yes | The file as base64. |
content_type |
No | Guessed from the extension if left out: pdf, png, jpg, gif, csv, txt, ics and zip are known; anything else is sent as application/octet-stream. |
content_id |
No | Set it to show the file inline in the html. |
To show an image inline
Give the image a content_id and refer to it with cid: in your html:
{
"from": "Acme <hello@acme.com>",
"to": "sam@example.com",
"subject": "Welcome",
"html": "<p><img src=\"cid:logo\" alt=\"Acme\" width=\"120\"></p><p>Welcome aboard.</p>",
"attachments": [
{ "filename": "logo.png", "content": "iVBORw0KGgo...", "content_type": "image/png", "content_id": "logo" }
]
}
A short id like logo is fine; Mailcandor turns it into a valid Content-ID and updates the cid: reference for you.
Inline images make emails bigger. For logos used in every email, a normal https:// image URL is usually better.
In Node.js
import { readFile } from 'node:fs/promises';
const pdf = await readFile('invoice-1042.pdf');
await fetch('https://api.mailcandor.com/v1/emails', {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.MAILCANDOR_KEY}`, 'Content-Type': 'application/json' },
body: JSON.stringify({
from: 'Acme <billing@acme.com>',
to: 'sam@example.com',
subject: 'Invoice 1042',
text: 'Your invoice is attached.',
attachments: [{ filename: 'invoice-1042.pdf', content: pdf.toString('base64') }],
}),
});
Limits
- 40 MB in total per email, as base64. Base64 is about 4/3 the size of the file, so the raw files can add up to roughly 30 MB.
- No attachments in batch sends.
- No fetching by URL. Only
contentis accepted. - Over SMTP, the whole message can be up to 40 MB, and attachments are kept as sent. See Send with SMTP.
Check before you send
Files with risky names can push an email toward spam. Run the content check with the file names:
curl https://api.mailcandor.com/v1/emails/check \
-H "Authorization: Bearer $MAILCANDOR_KEY" -H "Content-Type: application/json" \
-d '{ "subject": "Invoice 1042", "html": "<p>Attached.</p>", "attachments": [{ "filename": "invoice-1042.pdf" }] }'
The dashboard has the same check under Check an email, with an Attachment file names field.
If it does not work
All of these return 422 invalid_attachment with field pointing at the attachment:
- “Each attachment needs a filename without slashes, up to 255 characters.”
- “Add the file for … as base64 in content.” The content is missing or empty.
- “The content of … is not valid base64.” Remove line breaks and
data:...;base64,prefixes. - “Attachments add up to more than 40 MB (as base64). Link to large files instead.”
- “Send the file as base64 in content. Fetching files by URL is not supported yet.”
- The inline image shows as an attachment instead: check the
cid:in the html matchescontent_idexactly.