\documentclass{article}
\usepackage{ulem}
\usepackage{graphicx}
\usepackage{hyperref}
\pagestyle{headings}
\begin{document}
\part{Horde\_Mime}
\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:

<pre><code class="language-php">
\$part = new Horde\_Mime\_Part();
\$part->setType('text/plain');
\$part->setContents(\$message\_text);
\$part->setCharset(\$charset); /* Defaults to PHP charset. */
</code></pre>
For text/calendar attachment:

<pre><code class="language-php">
\$part = new Horde\_Mime\_Part();
\$part->setType('text/calendar');
\$part->setContents(\$text\_calendar\_data);
\$part->setCharset(\$charset); /* Defaults to PHP charset. */
</code></pre>
For Headers:

<pre><code class="language-php">
\$headers = new Horde\_Mime\_Headers();
\$headers->addHeader('Header 1', \$Header\_1\_Value);
\$headers->addHeader('Header 2', \$Header\_2\_Value);
</code></pre>
To return the message as a string:

<pre><code class="language-php">
\$string = \$part->toString(array('headers' => \$headers));
</code></pre>
\section{Creating and Sending E-Mail with an attachment by using the Horde\_Mime\_Mail class}
<pre><code class="language-php">
\$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\textbackslash\{\}n";
\} catch (Horde\_Mime\_Exception \$e) \{
    print "E-Mail not sent\textbackslash\{\}n";
\}
</code></pre>
\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.

<pre><code class="language-php">
// \$message = Horde\_Mime\_Part object
\$message = Horde\_Mime\_Part::parseMessage(\$message\_text);
</code></pre>
To determine the structure of the MIME message, e.g. to find out the MIME ID we are looking for:

<pre><code class="language-php">
\$map = \$message->contentTypeMap();
</code></pre>
\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:

<pre><code class="language-php">
\$part = \$message->getPart(\$mime\_id);
</code></pre>
To retrieve the body text:

<pre><code class="language-php">
\$body\_id = \$message->findBody();
if (\$body\_id) \{
    \$part = \$message->getPart(\$body\_id);
    \$body = \$part->getContents();
\} else \{
    \$body = 'Could not render body of message.';
\}
</code></pre>
To retrieve the HTML body text if any exists:

<pre><code class="language-php">
\$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.';
\}
</code></pre>
\section{Using MIME Viewers}
TODO

\section{Reading message headers}
<pre><code class="language-php">
\$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');
</code></pre>
\end{document}
