6.0.0-alpha14
6/30/25
Last Modified 3/8/05 by Jan Schneider
  • Backlinks
  • Similar Pages
  • Attachments
  • History
  • Back to

How to use the MIME API

Table of Contents

Creating MIME messages

Scenario: We have a text block (such as text/calendar data) which needs to be added as a MIME part to an email message. We then need to add a couple of additional headers to this message and finally read it back as a string.

For Message Body:

<?php


require_once 'Horde/MIME/Message.php';

$message = &new MIME_Message();

$part = $new MIME_Part('text/plain', $message_text);

$message->addPart($part);

?>

For text/calendar attachment:

<?php


$part = &new MIME_Part('text/calendar', $text_calendar_data);

$message->addPart($part);

?>

For Headers:

<?php


require_once 'Horde/MIME/Headers.php';

$headers = &new MIME_Headers();

$headers->addHeader('Header 1', $Header_1_Value);

$headers->addHeader('Header 2', $Header_2_Value);

$headers->addMIMEHeaders($message);

?>

To return the message as a string:

<?php


$string = $headers->toString() . "\n\n" . $message->toString();

?>

Parsing a MIME message

Scenario: We have an existing text string which contains a valid MIME email message. We need to parse this string in order to read back the body of a specific MIME part with a certain content type.

<?php


require_once 'Horde/MIME/Structure.php';

$message = &MIME_Structure::parseTextMIMEMessage($message_text);

$part = $message->getPart($mime_id);

?>

To determine the structure of the MIME message, e.g. to find out the MIME ID we are looking for:

<?php


$map = $message->contentTypeMap();

?>

$map is an array with key being the MIME IDs and values being the Content Types of that IDs.

Using MIME Viewers

Scenario: Continuing the previous example we want to render the text/calendar part to HTML. This code currently only works inside of a Horde application because it relies on the registry and Horde's configuration files.

<?php


require HORDE_BASE . '/config/mime_drivers.php';

require_once 'Horde/MIME/Viewer.php';

$viewer = &MIME_Viewer::factory($part);

$html = $viewer->render();

?>

Reading message headers

Scenario: We want to read a certain message from a certain message on the mail server, for example to search for messages with a known header.

<?php


/* Load libraries. */

require_once 'Horde/MIME/Headers.php';

require_once 'Horde/NLS.php';

/* Custom MIME_Headers class because we need to implement _getStream(). */

class MyHeaders extends MIME_Headers {

    function &_getStream()

    {

        return imap_open('{localhost/imap}INBOX', 'username', 'password');

    }

}

/* Get message subject. */

$headers = &new MyHeaders($message_uid);

$headers->buildHeaders();

$subject = $headers->getValue('Subject');

?>