Example Script

The following is a small example script that illustrates use of the MIME API. It parses a MIME message file given by the variable file on the command line, printing a one line summary of each entity. See the following pages for details on the functions used here.

<script language=vortex>

<a name=printEntity entity>
<!-- Prints a summary of given $entity. -->
<local depth imap contentType filename text>
  <$depth = (mimeEntityGetDepth($entity))>
  <$imap = (mimeEntityGetImapSectionSpecification($entity))>
  <!-- Get the Content-Type, without parameters.  Thus we ask for the
    == empty-string "parameter" from ...GetHeaderParameterValues():
    -->
  <$contentType = (mimeEntityGetHeaderParameterValues($entity,
                       "Content-Type", ""))>
  <$filename = (mimeEntityGetSafeFilename($entity))>
  <$text = (mimeEntityGetText($entity))>
  <$ret = ($depth * 2)>
  <fmt "%*s" $ret "">                         <!-- indent for $depth -->
  <fmt "%s %s [%s]" $imap $contentType $filename>
  <sandr "\space" " " $text>                  <!-- newlines to space -->
  <sandr "[^ -\xFF]" "." $ret>                <!-- controls to `.' -->
  <fmt " %.30|=V...\n" $ret>
</a>

<a name=main public file>
<local reader>
  <$reader = (mimeReaderOpenFile($file))>
  <if "" eq $reader><fmt "Cannot open %s\n" $file><exit 1></if>
  <while (mimeReaderMoveToNextEntity($reader) = '1')>
    <printEntity entity=$reader>
  </while>
</a>

</script>

Given the following example MIME message:

From: "John Smith" <john.smith@acme.com>
To: <mary.jones@acme.com>
Subject: MIME Test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="boundayMixed"

Preamble of multipart/mixed message.

--boundayMixed
Content-Type: multipart/related; boundary="boundayRelated"

Preamble of multipart/related entity.

--boundayRelated
Content-Type: multipart/alternative; boundary="boundayAlternative"

Preamble of multipart/alternative entity.

--boundayAlternative
Content-Type: text/plain; charset="us-ascii"

Plain text version of main message.

--boundayAlternative
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<body>HTML version of main message, with an inline image.
<img src=3D"cid:image001.gif@01CB5A58.A51AF3E0"></body>

--boundayAlternative--

--boundayRelated
Content-Type: image/gif; name="image001.gif"
Content-Transfer-Encoding: base64
Content-ID: <image001.gif@01CB5A58.A51AF3E0>

R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==

--boundayRelated--

--boundayMixed
Content-Type: image/gif; name="attachment.gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="attachment.gif"

R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==

--boundayMixed--

Running the example script above on the example MIME message above will produce the following output:

0 multipart/mixed [part.bin] Preamble of multipart/mixed me...
  1.0 multipart/related [part-2.bin] Preamble of multipart/related ...
    1.1.0 multipart/alternative [part-3.bin] Preamble of multipart/alternat...
      1.1.1 text/plain [part.txt] Plain text version of main mes...
      1.1.2 text/html [index.html] HTML version of main message, ...
    1.2 image/gif [image001.gif] GIF89a....
  2 image/gif [attachment.gif] GIF89a....

Copyright © Thunderstone Software     Last updated: Apr 15 2024
Copyright © 2024 Thunderstone Software LLC. All rights reserved.