> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Attachments Canonical URL: https://posthog.com/docs/workflows/transactional/attachments > **Cloud region:** This page uses US Cloud hosts. For an EU Cloud project, use `https://eu.posthog.com` instead of `https://us.posthog.com` (app host for links). ## On this page - [Attach files](#attach-files) - [Send files with the API](#send-files-with-the-api) - [Errors](#errors) Emails that you [send from code](https://posthog.com/docs/workflows/transactional/send-from-code.md#send-the-html) can carry files, such as a PDF receipt or a calendar invite. A template send can't. To send attachments, [verify your domain](https://posthog.com/docs/workflows/domain.md) first. The [test sender](https://posthog.com/docs/workflows/email/test-sender.md) doesn't send them. Ask your agentwith the [PostHog MCP](https://posthog.com/docs/workflows/mcp.md)[sending-transactional-emails](https://posthog.com/docs/workflows/skills.md) “Update our receipt email send to attach the generated PDF invoice using the PostHog SDK.” Copy ## Attach files Give the SDK each file's bytes and name. It hashes, uploads, and attaches the file. A file PostHog already has, such as your terms, isn't uploaded again. ### Node.js ```javascript await posthog.emails.send({ to: "ada@example.com", templateKey: "receipt", subject: "Your receipt for order 1042", html, attachments: [{ filename: "invoice-1042.pdf", content: pdfBytes, contentType: "application/pdf" }], idempotencyKey: "receipt-1042", }) ``` ### Python ```python posthog.emails.send( to="ada@example.com", template_key="receipt", subject="Your receipt for order 1042", html=html, attachments=[{"filename": "invoice-1042.pdf", "content": pdf_bytes, "content_type": "application/pdf"}], idempotency_key="receipt-1042", ) ``` ### Ruby ```ruby $posthog.emails.send( to: 'ada@example.com', template_key: 'receipt', subject: 'Your receipt for order 1042', html: html, attachments: [{ filename: 'invoice-1042.pdf', content: File.binread(pdf_path), content_type: 'application/pdf' }], idempotency_key: 'receipt-1042' ) ``` ### Rust ```rust posthog .emails() .send( CodeEmail::new("ada@example.com", "receipt", "Your receipt for order 1042") .html(html) // Call attachment once for each file .attachment(EmailAttachment::new("invoice-1042.pdf", pdf_bytes).content_type("application/pdf")) .idempotency_key("receipt-1042"), ) .await?; ``` ### PHP ```php PostHog::emails()->send([ 'to' => 'ada@example.com', 'template_key' => 'receipt', 'subject' => 'Your receipt for order 1042', 'html' => $html, 'attachments' => [ ['filename' => 'invoice-1042.pdf', 'content' => file_get_contents($pdfPath), 'content_type' => 'application/pdf'], ], 'idempotency_key' => 'receipt-1042', ]); ``` ### Go ```go _, err := client.Emails().Send(ctx, posthog.CodeEmail{ To: "ada@example.com", TemplateKey: "receipt", Subject: "Your receipt for order 1042", HTML: html, Attachments: []posthog.EmailAttachment{ {Filename: "invoice-1042.pdf", Content: pdfBytes, ContentType: "application/pdf"}, }, IdempotencyKey: "receipt-1042", }) ``` ### .NET ```csharp await emails.SendAsync(new SendCodeEmailOptions { To = "ada@example.com", TemplateKey = "receipt", Subject = "Your receipt for order 1042", Html = html, Attachments = [ new EmailAttachment { Filename = "invoice-1042.pdf", Content = pdfBytes, ContentType = "application/pdf" }, ], IdempotencyKey = "receipt-1042", }); ``` ### Elixir ```elixir PostHog.Emails.send( to: "ada@example.com", template_key: "receipt", subject: "Your receipt for order 1042", html: html, attachments: [%{filename: "invoice-1042.pdf", content: pdf_bytes, content_type: "application/pdf"}], idempotency_key: "receipt-1042" ) ``` ### REST ```bash # Upload the file first (see "Send files with the API" below), then send its sha256 and filename curl -X POST "https://us.posthog.com/api/messaging/send/email/" \ -H "Authorization: Bearer $POSTHOG_PROJECT_SECRET_API_KEY" \ -H "Content-Type: application/json" \ -H "Idempotency-Key: receipt-1042" \ -d '{"to": "ada@example.com", "template_key": "receipt", "subject": "Your receipt for order 1042", "html": "

Your invoice is attached.

", "attachments": [{"sha256": "d00ca3c2296eace098658a8576dafec8a959f5da536a51c8f9d060c00e427252", "filename": "invoice-1042.pdf", "content_type": "application/pdf"}]}' ``` - **Limits:** 10 files, and 40 MB per email after Base64 encoding, which is about 29 MB of files. See [API and SDK limits](https://posthog.com/docs/workflows/reference/limits.md#api-and-sdk-limits) for every limit on a send. - **Inline images:** give the file a content ID, such as `logo`, and use it in the HTML as ``. - **Calendar invites:** send `text/calendar; method=REQUEST` as the content type, so mail apps show the file as an invite. - **Blocked files:** PostHog refuses types that mailbox providers block, such as `.exe`. - **Viruses:** if the email provider finds a virus, the email fails with the reason `provider_rejected`. ## Send files with the API Files go to PostHog before the email: 1. **Hash each file:** its SHA-256, as 64 lowercase hex characters, and its size in bytes. 2. **Ask where to upload:** `POST /api/messaging/attachments/uploads/` with the `files` for this email. PostHog answers `present` for a file it already has, and `upload` with a URL for the rest. 3. **Upload:** `PUT` each `upload` file's bytes to its URL, with the headers exactly as given, within 15 minutes. Don't send your PostHog key. 4. **Send:** list each file's `sha256` and `filename` in `attachments`. ```bash curl -X POST "https://us.posthog.com/api/messaging/attachments/uploads/" \ -H "Authorization: Bearer $POSTHOG_PROJECT_SECRET_API_KEY" \ -H "Content-Type: application/json" \ -d '{"files": [{"sha256": "d00ca3c2296eace098658a8576dafec8a959f5da536a51c8f9d060c00e427252", "size": 48213}]}' ``` Uploads go to Amazon S3. If your network limits outgoing traffic, allow hosts that end in `.s3.us-east-1.amazonaws.com` (US) or `.s3.eu-central-1.amazonaws.com` (EU). The SDKs upload the same way. The [receipt with a PDF](https://posthog.com/docs/workflows/transactional/examples.md#receipt-with-a-pdf) example is a complete script. ## Errors | Code | Cause | Fix | | --- | --- | --- | | `too_many_attachments` | More than 10 files. | Link to the rest from the email. | | `message_too_large` | Over 40 MB after encoding. | Send fewer or smaller files, or link to them. | | `attachment_not_uploaded` | PostHog has no file with this hash. The upload didn't finish or expired. | Ask where to upload again, upload, and resend. | | `blocked_file_type` | Mailbox providers block this type. | Send another type, or link to it. | | `unsafe_filename` | `filename` has a path or a line break. | Send only the name, such as `invoice.pdf`. | | `not_mime_type` | `content_type` isn't a MIME type, or it has a `name` parameter. | Send a type such as `application/pdf`, or leave it out. | | `unsafe_content_id` | The content ID has `<`, `>`, or a line break. | Send only the ID, such as `logo`. | | `upload_quota_exceeded` | Your project hit today's upload limit. Resets at midnight UTC. | Reuse files PostHog already has, or link to new ones. | | `field_not_accepted` | Your project can't send files yet. | Send without files. | Other problems with a file, such as a hash that isn't 64 lowercase hex characters, return `invalid_request`, and `attr` names the field. Each of these errors returns `400`, except `upload_quota_exceeded`, which returns `429`. PostHog sends nothing when a check fails.