On this page:
11.1 Message Decoding
mime-analyze
message
entity
disposition
11.2 Exceptions
mime-error
unexpected-termination
missing-multipart-boundary-parameter
malformed-multipart-entity
empty-mechanism
empty-type
empty-subtype
empty-disposition-type
11.3 MIME Unit
mime@
11.4 MIME Signature
mime^

11 MIME: Decoding Internet Data

The net/mime library provides utilities for parsing and creating MIME encodings as described in RFC 2045 through RFC 2049.
The library was written by Francisco Solsona.

11.1 Message Decoding

procedure

(mime-analyze message-in [part?])  message?

  message-in : (or/c bytes? input-port)
  part? : any/c = #f
Parses message-in and returns the parsed result as a message instance.

If part? is #f, then message-in should start with the header for a full message; otherwise, message-in should start with the header for a part within a message.

struct

(struct message (version entity fields)
  #:extra-constructor-name make-message)
  version : real?
  entity : entity
  fields : (listof string?)
A decoded MIME message. The version is 1.0 by default. The entity field represents the message data. The fields field contains one string for each field in the message header.

struct

(struct entity (type
    subtype
    charset
    encoding
    disposition
    params
    id
    description
    other
    fields
    parts
    body)
  #:extra-constructor-name make-entity)
  type : symbol?
  subtype : symbol?
  charset : symbol?
  encoding : symbol?
  disposition : disposition?
  params : (listof (cons/c symbol? string?))
  id : string?
  description : string?
  other : (listof string?)
  fields : (listof string?)
  parts : (listof message?)
  body : (or/c (output-port? . -> . void?) null?)
Represents the content of a message or a sub-part. The mime-analyze function chooses default values for fields when they are not specified in input.

Standard values for the type field include 'text, 'image, 'audio, 'video, 'application, 'message, and 'multipart.

Standard values for the subtype field depend on the type field, and include the following, but any subtype is allowed as a downcased version of the specification from the header.

'text

 

'plain

 

[RFC1521, NSB]

 

 

'richtext

 

[RFC1521, NSB]

 

 

'tab-separated-values

 

[Lindner]

'multipart

 

'mixed

 

[RFC1521, NSB]

 

 

'alternative

 

[RFC1521, NSB]

 

 

'digest

 

[RFC1521, NSB]

 

 

'parallel

 

[RFC1521, NSB]

 

 

'appledouble

 

[MacMime, Faltstrom]

 

 

'header-set

 

[Crocker]

'message

 

'rfc822

 

[RFC1521, NSB]

 

 

'partial

 

[RFC1521, NSB]

 

 

'external-body

 

[RFC1521, NSB]

 

 

'news

 

[RFC 1036, Spencer]

'application

 

'octet-stream

 

[RFC1521, NSB]

 

 

'postscript

 

[RFC1521, NSB]

 

 

'oda

 

[RFC1521, NSB]

 

 

'atomicmail

 

[atomicmail, NSB]

 

 

'andrew-inset

 

[andrew-inset, NSB]

 

 

'slate

 

[slate, Crowley]

 

 

'wita

 

[Wang Info Transfer, Campbell]

 

 

'dec-dx

 

[Digital Doc Trans, Campbell]

 

 

'dca-rft

 

[IBM Doc Content Arch, Campbell]

 

 

'activemessage

 

[Shapiro]

 

 

'rtf

 

[Lindner]

 

 

'applefile

 

[MacMime, Faltstrom]

 

 

'mac-binhex40

 

[MacMime, Faltstrom]

 

 

'news-message-id

 

[RFC1036, Spencer]

 

 

'news-transmission

 

[RFC1036, Spencer]

 

 

'wordperfect5.1

 

[Lindner]

 

 

'pdf

 

[Lindner]

 

 

'zip

 

[Lindner]

 

 

'macwriteii

 

[Lindner]

 

 

'msword

 

[Lindner]

 

 

'remote-printing

 

[RFC1486,MTR]

'image

 

'jpeg

 

[RFC1521, NSB]

 

 

'gif

 

[RFC1521, NSB]

 

 

'ief

 

[RFC1314]

 

 

'tiff

 

[MTR]

'audio

 

'basic

 

[RFC1521, NSB]

'video

 

'mpeg

 

[RFC1521, NSB]

 

 

'quicktime

 

[Lindner]

Standard values for the charset field include 'us-ascii, which is the default.

Standard values for the encoding field are '7bit, '8bit, 'binary, 'quoted-printable, and 'base64. The default is '7bit.

The params field contains a list of parameters from other MIME headers.

The id field is taken from the "Content-Id" header field.

The description field is taken from the "Content-description" header field.

The other field contains additional (non-standard) field headers whose field names start with "Content-".

The fields field contains additional field headers whose field names do not start with "Content-".

The parts contains sub-parts from multipart MIME messages. This list is non-empty only when type is 'multipart or 'message.

The body field represents the body as a function that consumes an output out and writes the decoded message to the port. If type is 'multipart or 'message., then body is '(). All of the standard values of encoding are supported. The procedure only works once (since the encoded body is pulled from a stream).

struct

(struct disposition (type
    filename
    creation
    modification
    read
    size
    params)
  #:extra-constructor-name make-disposition)
  type : symbol?
  filename : (or/c string? false/c)
  creation : (or/c string? false/c)
  modification : (or/c string? false/c)
  read : (or/c string? false/c)
  size : (or/c exact-nonnegative-integer? false/c)
  params : (listof (cons/c symbol? string?))
Represents a "Content-Disposition" header as defined in RFC 2183.

Standard values for the type field include 'inline and 'attachment.

The filename field is drawn from the "filename" parameter of the "Content-Disposition" header, if included in the message.

The creation, modification, and read fields represent file timestamps as drawn from the "creation-date", "modification-date", and "read-date" attributes of the "Content-Disposition" header, if included in the message.

The size field is drawn from the "size" parameter of the "Content-Disposition" header, if included in the message.

The params field stores any additional attribute bindings of the "Content-Disposition" header, if included in the message.

11.2 Exceptions

struct

(struct mime-error exn:fail ()
  #:extra-constructor-name make-mime-error)
The supertype of all MIME exceptions. Only the subtype missing-multipart-boundary-parameter is ever actually raised.

struct

(struct unexpected-termination mime-error (msg)
  #:extra-constructor-name make-unexpected-termination)
  msg : string?
Originally raised when an end-of-file is reached while parsing the headers of a MIME entity, but currently a mere warning is logged.

Raised when a multipart type is specified, but no "Boundary" parameter is given.

struct

(struct malformed-multipart-entity mime-error (msg)
  #:extra-constructor-name make-malformed-multipart-entity)
  msg : string?
Never actually raised.

struct

(struct empty-mechanism mime-error ()
  #:extra-constructor-name make-empty-mechanism)
Never actually raised.

struct

(struct empty-type mime-error ()
  #:extra-constructor-name make-empty-type)
Never actually raised.

struct

(struct empty-subtype mime-error ()
  #:extra-constructor-name make-empty-subtype)
Never actually raised.

struct

(struct empty-disposition-type mime-error ()
  #:extra-constructor-name make-empty-disposition-type)
Never actually raised.

11.3 MIME Unit

mime@ and mime^ are deprecated. They exist for backward-compatibility and will likely be removed in the future. New code should use the net/mime module.

 (require net/mime-unit)

value

mime@ : unit?

Imports nothing, exports mime^.

11.4 MIME Signature

 (require net/mime-sig)

signature

mime^ : signature

Includes everything exported by the net/mime module.