Attachment Handling
Relevant source files
The following files were used as context for generating this wiki page:
Attachment Handling in Mailiner encompasses the identification, display, downloading, and composition of email attachments. The system distinguishes between standard file attachments and inline images, managing them through a lifecycle of parsing from incoming IMAP streams to serializing for SMTP delivery.
The architecture relies on a specialized MIME parser to detect attachments within BODYSTRUCTURE and a streaming download system that handles transfer-encoding (TE) decoding directly into browser Blob objects. During composition, the system enforces strict size and count constraints to ensure draft documents remain within deliverable limits.
Attachment Identification and Parsing
Mailiner identifies attachments by analyzing the BodyPart structure returned by the IMAP server. The MessageParser uses heuristics to determine if a part is an attachment or part of the message body.
Identification Heuristics
A part is classified as an attachment if:
- Its
Content-Dispositionis explicitly set toATTACHMENT. - It is not a
multiparttype. - It is a
texttype and contains a disposition filename. - It is an image part (unless it has a
Content-IDand is not marked asATTACHMENT).
Sources: crates/mailiner-mime/src/heuristics.rs:11-28, crates/mailiner-mime/src/parser/mod.rs:141-160
Parser Workflow
The following diagram illustrates how the MessageParser processes message parts into a flat list of MessagePart objects.
The parser calculates the expected decoded size for Base64 encoded parts by dividing the original size by 1.37.
Sources: crates/mailiner-mime/src/parser/mod.rs:136-140, crates/mailiner-mime/src/heuristics.rs:11-28
Attachment Download System
The download system manages the transition from raw, transfer-encoded chunks to a local file in the browser. It uses a streaming approach to minimize memory usage, avoiding the need to hold the entire file in a contiguous buffer.
Streaming Architecture
The StreamingBlobDownload struct accumulates decoded octets as discrete parts. On wasm32 targets, these are stored in a JavaScript Array of Uint8Array objects before being assembled into a Blob.
| Component | Responsibility |
|---|---|
StreamingTransferDecoder | Incremental decoding of Base64 or Quoted-Printable data. |
StreamingBlobDownload | Manages download state, progress, and file assembly. |
DownloadStatus | Tracks Idle, InProgress (with byte counts), Finished, or Error. |
Sources: crates/mailiner-app/src/download.rs:13-25, crates/mailiner-app/src/download.rs:88-115
Download Limits
To maintain application stability, the system enforces a hard cap of 100 MiB for attachment downloads (decoded size).
Sources: crates/mailiner-app/src/download.rs:7-8
Composition and Export
When composing a message, attachments are managed within the DraftDocument. The system supports both FileAttachment and InlineImage types.
Constraints and Validation
Before a draft can be exported for sending, it must pass validation against specific capacity limits (caps).
| Constraint | Limit |
|---|---|
| Max Single File | 25 MiB |
| Max Single Inline | 25 MiB |
| Max Total Draft | 40 MiB |
| Max File Count | 20 |
| Max Inline Count | 30 |
Sources: crates/mailiner-composer/src/model/draft.rs:107-118, crates/mailiner-composer/src/model/draft.rs:244-297
MIME Tree Construction
The export module builds a nested MIME structure based on the content of the draft:
- Text Body: Wrapped in
multipart/alternative(Plain + HTML). - Inline Images: If present, the text body and images are wrapped in
multipart/related. - Attachments: If present, the previous structure and file attachments are wrapped in
multipart/mixed.
Sources: crates/mailiner-composer/src/export.rs:213-258
Filename Encoding
The system uses RFC 2231 to encode filenames in the Content-Disposition header. This ensures that filenames with non-ASCII characters (e.g., "café.pdf") are handled correctly by the recipient's mail client by providing both a standard filename= fallback and an encoded filename*= parameter.
Sources: crates/mailiner-mime/src/writer/mod.rs:135-165
Summary
Attachment Handling integrates parsing, streaming downloads, and complex MIME generation. By utilizing streaming decoders and enforcing strict validation caps during composition, Mailiner ensures efficient resource usage in the browser environment while maintaining compatibility with RFC standards for email transmission.