Attachments

Emails that you send from code can carry files, such as a PDF receipt or a calendar invite. A template send can't. To send attachments, verify your domain first. The test sender doesn't send them.

“Update our receipt email send to attach the generated PDF invoice using the PostHog SDK.”

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.

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",
})
  • Limits: 10 files, and 40 MB per email after Base64 encoding, which is about 29 MB of files. See 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 <img src="cid:logo">.
  • 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.
Terminal
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 example is a complete script.

Errors

CodeCauseFix
too_many_attachmentsMore than 10 files.Link to the rest from the email.
message_too_largeOver 40 MB after encoding.Send fewer or smaller files, or link to them.
attachment_not_uploadedPostHog has no file with this hash. The upload didn't finish or expired.Ask where to upload again, upload, and resend.
blocked_file_typeMailbox providers block this type.Send another type, or link to it.
unsafe_filenamefilename has a path or a line break.Send only the name, such as invoice.pdf.
not_mime_typecontent_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_idThe content ID has <, >, or a line break.Send only the ID, such as logo.
upload_quota_exceededYour project hit today's upload limit. Resets at midnight UTC.Reuse files PostHog already has, or link to new ones.
field_not_acceptedYour 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.

Still have questions?

Was this page useful?