Skip to main content

Attachments

The attachments array attaches files to your email. Each entry is an object with three fields: the MIME type of the file, the filename shown to the recipient, and content containing the file’s bytes as a base64-encoded string.
#!/usr/bin/env bash
set -u
: "${MAILCHANNELS_API_KEY:?Set MAILCHANNELS_API_KEY before running}"
: "${FROM_EMAIL:?Set FROM_EMAIL (must be on a Domain-Lockdown-authorized domain)}"
: "${TO_EMAIL:?Set TO_EMAIL}"
: "${ATTACHMENT_PATH:?Set ATTACHMENT_PATH to the file you want to attach, e.g. ./logo.png}"

# Base64-encode the file. The -w0 flag (GNU base64) emits a single line with no
# wrapping; on macOS, drop -w0 — `base64` there doesn't wrap by default.
ATTACHMENT_BASE64=$(base64 -w0 "$ATTACHMENT_PATH")
ATTACHMENT_FILENAME=$(basename "$ATTACHMENT_PATH")

curl -X POST https://api.mailchannels.net/tx/v1/send \
  -H "Content-Type: application/json" \
  -H "X-Api-Key: $MAILCHANNELS_API_KEY" \
  -d @- <<JSON
{
  "personalizations": [
    { "to": [{ "email": "$TO_EMAIL", "name": "Recipient" }] }
  ],
  "from": {
    "email": "$FROM_EMAIL",
    "name": "Your Name"
  },
  "subject": "Hello from MailChannels",
  "content": [
    {
      "type": "text/plain",
      "value": "Hello! See the attached file."
    }
  ],
  "attachments": [
    {
      "type": "image/png",
      "filename": "$ATTACHMENT_FILENAME",
      "content": "$ATTACHMENT_BASE64"
    }
  ]
}
JSON

Limits

  • Up to 1000 attachments per email.
  • 30 MB combined limit on attachments and email content. Messages that exceed this are rejected.

Forbidden attachments

The following MIME types and file extensions are blocked because they are commonly associated with malware. Sending an attachment with a forbidden type or extension returns an error. MIME types
  • application/x-msdownload
  • application/vnd.ms-htmlhelp
  • application/java-archive
  • application/x-sh
  • application/x-shellscript
File extensions .ade, .adp, .bat, .chm, .cmd, .com, .cpl, .exe, .hta, .ins, .isp, .jar, .jse, .lib, .lnk, .mde, .msc, .msp, .mst, .pif, .scr, .sct, .sh, .shb, .sys, .vb, .vbe, .vbs, .vxd, .wsc, .wsf, .wsh
Never send an attachment that you received from a user without first scanning it for viruses and malware.