This example demonstrates how to send messages and drafts with inline attachments using the content_id field in the Nylas Python SDK.
- How to create inline attachments with
content_idfor HTML emails - How the SDK properly handles
content_idfor large attachments (>3MB) - The difference between inline attachments and regular attachments
- How to reference inline attachments in HTML email bodies using
cid:syntax
When an attachment includes a content_id field, the SDK will use this as the field name in multipart form data instead of the generic file{index} pattern. This is crucial for inline attachments that need to be referenced in the email body.
The example shows how to:
- Set the
content_idfield in the attachment - Reference the attachment in HTML using
src="cid:your-content-id" - Set appropriate inline properties (
is_inline: True,content_disposition: "inline")
For attachments larger than 3MB, the SDK automatically switches from JSON to multipart form data. With this fix, the content_id is now properly respected in the form field names.
-
Set your Nylas API key:
export NYLAS_API_KEY='your-api-key-here'
-
Update the grant ID and email addresses in the script
-
Run the example:
python inline_attachment_example.py
- Content ID Format: Use a unique identifier for each inline attachment (e.g.,
"image1@example.com","logo","banner-image") - HTML Reference: Reference inline attachments in HTML using
src="cid:your-content-id" - Backward Compatibility: Attachments without
content_idstill work as before usingfile{index}naming - File Size Threshold: The 3MB threshold determines whether JSON or form data is used for the request
Form data fields:
- message: (JSON payload)
- file0: (inline image - content_id ignored)
- file1: (regular attachment)
Form data fields:
- message: (JSON payload)
- my-inline-image: (inline image - uses content_id)
- file1: (regular attachment - fallback to file{index})
This ensures that email clients can properly display inline images by matching the content_id in the HTML cid: reference with the multipart form field name.