JMAP Email/set: inline attachments with a cid end up in multipart/mixed instead of multipart/related

Issue Description

When a message is created with JMAP Email/set using textBody, htmlBody and attachments, the attachments that have disposition “inline” and a cid referenced by the HTML part are placed next to the body in multipart/mixed, not grouped with the HTML part in multipart/related. Mail clients then do not show these images in the message body. In practice this happens with screenshots pasted into the body in the Bulwark webmail. Single node, otherwise default configuration.

Expected Behavior

The HTML part and the inline parts it references by cid are grouped in multipart/related (RFC 2387):

multipart/alternative
  text/plain
  multipart/related
    text/html
    image/png  (inline, Content-ID)

wrapped in multipart/mixed when there are regular attachments as well.

Actual Behavior

multipart/mixed
  multipart/alternative
    text/plain
    text/html   (img src="cid:img1")
  image/png     (Content-Disposition: inline, Content-ID: img1)

The image is shown neither in the body by recipients’ mail clients nor in the webmail’s own Sent view.

Reproduction Steps

  1. Upload a PNG over JMAP (upload endpoint), get its blobId.
  2. Create a message with Email/set:
{
  "mailboxIds": {"DRAFTS-ID": true},
  "subject": "inline image test",
  "bodyValues": {
    "t": {"value": "see image"},
    "h": {"value": "<p>see image</p><img src=\"cid:img1\">"}
  },
  "textBody": [{"partId": "t", "type": "text/plain"}],
  "htmlBody": [{"partId": "h", "type": "text/html"}],
  "attachments": [{"blobId": "BLOB-ID", "type": "image/png", "name": "image.png",
                   "disposition": "inline", "cid": "img1"}]
}
  1. Download the created message (its blobId) and look at the MIME structure.

Relevant Log Output

No errors are logged: the message is created and delivered normally, only the MIME structure is wrong.

Stalwart Version

v1.x

Installation Method

Binary (Linux)

Database Backend

RocksDB

Blob Storage

RocksDB

Search Engine

Internal

Directory Backend

Internal

Additional Context

Client: Bulwark 1.18.0 webmail, it creates drafts with textBody, htmlBody and attachments. Reverse proxy: nginx (not involved).
From the source: the Email/set handler in the jmap crate passes all JMAP attachments to the attachments list of the mail-builder MessageBuilder, and write_body_parts in mail-builder 2.0.0 always wraps the alternative part and the attachments in multipart/mixed and never creates multipart/related (inline() adds to the same list).

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

Email/set passed every entry of attachments to the message builder as a plain attachment, and the builder always wrapped the body and attachments in multipart/mixed, so the HTML part and the images it references never ended up together. The reverse also broke: when the message was read back, the image showed up as part of the body instead of in attachments.

This is fixed now. When an htmlBody is present, an attachment with a cid, an image, audio or video type and no attachment disposition is grouped with the HTML part in multipart/related, which gives the structure you described:

multipart/mixed            (only when regular attachments remain)
  multipart/alternative
    text/plain
    multipart/related
      text/html
      image/png
  application/pdf

Email/get returns those images in attachments with their cid, same as they were created. The fix ships in next week’s Stalwart release, and after that Bulwark’s pasted screenshots should show up in the body, both for recipients and in your own Sent folder.