6.0.0-git
2024-04-26
Last Modified 2013-09-04 by Jan Schneider

Horde_Mime

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:

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

For text/calendar attachment:

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

For Headers:

$headers = new Horde_Mime_Headers();
$headers->addHeader('Header 1', $Header_1_Value);
$headers->addHeader('Header 2', $Header_2_Value);

To return the message as a string:

$string = $part->toString(array('headers' => $headers));

Creating and Sending E-Mail with an attachment by using the Horde_Mime_Mail class

$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";
}

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.

// $message = Horde_Mime_Part object
$message = Horde_Mime_Part::parseMessage($message_text);

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

$map = $message->contentTypeMap();

$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:

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

To retrieve the body text:

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

To retrieve the HTML body text if any exists:

$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.';
}

Using MIME Viewers

TODO

Reading message headers

$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');