[cvs] [Wiki] created: Doc/Dev/MimeH4
Michael Slusarz
slusarz at horde.org
Wed Feb 3 01:51:28 UTC 2010
slusarz Tue, 02 Feb 2010 20:51:28 -0500
Created page: http://wiki.horde.org/Doc/Dev/MimeH4
+ Horde_Mime
[[toc]]
++ 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:
<code type="php">
$part = new Horde_Mime_Part();
$part->setType('text/plain');
$part->setContents($message_text);
$part->setCharset($charset); /* Defaults to PHP charset. */
</code>
For text/calendar attachment:
<code type="php">
$part = new Horde_Mime_Part();
$part->setType('text/calendar');
$part->setContents($text_calendar_data);
$part->setCharset($charset); /* Defaults to PHP charset. */
</code>
For Headers:
<code type="php">
$headers = new Horde_Mime_Headers();
$headers->addHeader('Header 1', $Header_1_Value);
$headers->addHeader('Header 2', $Header_2_Value);
</code>
To return the message as a string:
<code type="php">
$string = $part->toString(array('headers' => $headers));
</code>
++ Creating and Sending E-Mail with an attachment by using the
Horde_Mime_Mail class
<code type="php">
$mail = new Horde_Mime_Mail();
// Set the header date
$mail->addHeader('Date', date('r'));
// Set the from address
$mail->addHeader('From', 'sender at 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 at 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";
}
</code>
++ 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.
<code type="php">
// $message = Horde_Mime_Part object
$message = Horde_Mime_Part::parseMessage($message_text);
</code>
To determine the structure of the MIME message, e.g. to find out the
MIME ID we are looking for:
<code type="php">
$map = $message->contentTypeMap();
</code>
{{$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:
<code type="php">
$part = $message->getPart($mime_id);
</code>
To retrieve the body text:
<code type="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>
To retrieve the HTML body text if any exists:
<code type="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>
++ Using MIME Viewers
TODO
++ Reading message headers
<code type="php">
$headerText = 'Return-Path: <john at example.com>
Message-ID: <20080228160832.604925ntqxdo4o00 at example.com>
Date: Thu, 28 Feb 2008 16:08:32 +0100
From: John Doe <john at example.com>
To: jane at example.com
Subject: Some Subject';
/* Get message subject. */
$headers = Horde_Mime_Headers::parseHeaders($headerText);
$subject = $headers->getValue('Subject');
</code>
More information about the cvs
mailing list