\documentclass{article}
\usepackage{ulem}
\pagestyle{headings}
\begin{document}
\part{Horde\_Mime}
\tableofcontents

Horde\_MimeCreating MIME messagesCreating and Sending E-Mail with an attachment by using the Horde\_Mime\_Mail classParsing a MIME messageUsing MIME ViewersReading message headers

\section{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.

This example assumes you are in the Horde environment (i.e. autoloading).  If not, you will need to manually handle loading the various Horde libraries used.

For Message Body:\newline
\begin{verbatim}

$part = new Horde_Mime_Part();
$part->setType('text/plain');
$part->setContents($message_text);
$part->setCharset($charset); /* Defaults to PHP charset. */
\end{verbatim}

For text/calendar attachment:\newline
\begin{verbatim}

$part = new Horde_Mime_Part();
$part->setType('text/calendar');
$part->setContents($text_calendar_data);
$part->setCharset($charset); /* Defaults to PHP charset. */
\end{verbatim}

For Headers:\newline
\begin{verbatim}

$headers = new Horde_Mime_Headers();
$headers->addHeader('Header 1', $Header_1_Value);
$headers->addHeader('Header 2', $Header_2_Value);
\end{verbatim}

To return the message as a string:\newline
\begin{verbatim}

$string = $part->toString(array('headers' => $headers));
\end{verbatim}

\section{Creating and Sending E-Mail with an attachment by using the Horde\_Mime\_Mail class}
\begin{verbatim}

$mail = new Horde_Mime_Mail();

// Set the header date
$mail->addHeader('Date', date('r'));

// Set the from address
$mail->addHeader('From', 'sender@example.com');

// Set the subject of the mail
$mail->addHeader('Subject', 'Horde_Mime_Mail example');

// Set the text message body
$mail->setBody('Example MIME message with an attachment');

// Add the file as an attachment, set the file name and what kind of file it is.
$mail->addAttachment('/tmp/some_file.zip', 'some_file.zip', 'application/x-zip-compressed');

// Add recipients
$mail->addRecipients('recipient@example.com');

// Get the mail driver
$mail_config = array(
    'type' => $conf['mailer']['type'],
    'params' => $conf['mailer']['params']
);
if (($mail_config['type'] == 'smtp') &&
    $mail_config['params']['auth'] &&
    empty($mail_config['params']['username']) &&
    Horde_Auth::getAuth()) {
        $mail_config['params']['username'] = Horde_Auth::getAuth();
        $mail_config['params']['password'] = Horde_Auth::getCredential('password');
    }
}

// Send the mail
try {
    $mail->send($mail_config);
    print "E-Mail sent\n";
} catch (Horde_Mime_Exception $e) {
    print "E-Mail not sent\n";
}
\end{verbatim}

\section{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.

\begin{verbatim}

// $message = Horde_Mime_Part object
$message = Horde_Mime_Part::parseMessage($message_text);
\end{verbatim}

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

\begin{verbatim}

$map = $message->contentTypeMap();
\end{verbatim}

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

To retrieve a certain MIME part of the message:

\begin{verbatim}

$part = $message->getPart($mime_id);
\end{verbatim}

To retrieve the body text:

\begin{verbatim}

$body_id = $message->findBody();
if ($body_id) {
    $part = $message->getPart($body_id);
    $body = $part->getContents();
} else {
    $body = 'Could not render body of message.';
}
\end{verbatim}

To retrieve the HTML body text if any exists:

\begin{verbatim}

$body_id = $message->findBody('html');
if (is_null($body_id)) {
    $body_id = $contents->findBody();
}
if ($body_id) {
    $part = $message->getPart($body_id);
    $body = $part->getContents();
} else {
    $body = 'Could not render body of message.';
}
\end{verbatim}

\section{Using MIME Viewers}
TODO

\section{Reading message headers}
\begin{verbatim}

$headerText = 'Return-Path: <john@example.com>
Message-ID: <20080228160832.604925ntqxdo4o00@example.com>
Date: Thu, 28 Feb 2008 16:08:32 +0100
From: John Doe <john@example.com>
To: jane@example.com
Subject: Some Subject';

/* Get message subject. */
$headers = Horde_Mime_Headers::parseHeaders($headerText);
$subject = $headers->getValue('Subject');
\end{verbatim}

\end{document}
