mailDecoding_functions.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:4k
- <?php
- /**
- * Filename.......: decode.php
- * Project........: Mail decoding function
- * Last Modified..: $Date: 2002/12/23 22:26:39 $
- * CVS Revision...: $Revision: 1.1 $
- * Copyright......: 2001, 2002 Richard Heyes
- */
- /**
- * Some globals
- */
- $GLOBALS['decodeMail_output'] = new stdClass;
- $GLOBALS['decodeMail_inRelated'] = false;
- /**
- * Decodes a mail and returns the various parts.
- *
- * @param string The raw mail text
- */
- function decodeMail($input)
- {
- $output = &$GLOBALS['decodeMail_output'];
- $decode_params['input'] = $input;
- $decode_params['include_bodies'] = true;
- $decode_params['decode_bodies'] = true;
- $decode_params['decode_headers'] = true;
- $structure = Mail_mimeDecode::decode($decode_params);
- decodeMail_traverseStructure($structure);
- // Sizes
- $output->textSize = strlen(@$output->text);
- $output->htmlSize = strlen(@$output->html);
- // Headers
- $output->headers = $structure->headers;
- // From name/email
- if (!empty($output->headers['from'])) {
- $from = Mail_RFC822::parseAddressList($output->headers['from'], null, null, false);
- $output->fromEmail = sprintf('%s@%s', $from[0]->mailbox, $from[0]->host);
- $output->fromName = preg_replace('/^"(.*)"$/', '1', $from[0]->personal);
- } else {
- $output->fromEmail = '';
- $output->fromName = '';
- }
-
- // Return address
- if (!empty($output->headers['reply-to'])) {
- $output->returnAddress = $output->headers['reply-to'];
- } elseif (!empty($output->headers['sender'])) {
- $output->returnAddress = $output->headers['sender'];
- } elseif (!empty($output->headers['from'])) {
- $output->returnAddress = $output->headers['from'];
- } elseif (!empty($output->headers['return-path'])) {
- $output->returnAddress = $output->headers['return-path'];
- }
-
- // Recipient addresses
- foreach (array('to', 'cc') as $header) {
- if (!empty($output->headers[$header])) {
- $to = Mail_RFC822::parseAddressList($output->headers[$header], null, null, false);
- for ($i=0; $i<count($to); $i++) {
- if ($to[$i]) {
- $recipients[] = sprintf('%s@%s', $to[$i]->mailbox, $to[$i]->host);
- }
- }
- }
- }
- $output->recipientAddresses = $recipients;
- return $output;
- }
- /**
- * Traverses the decoded structure and adds the various
- * parts to the global var.
- */
- function decodeMail_traverseStructure($structure)
- {
- $output = &$GLOBALS['decodeMail_output'];
- $ctype = strtolower(@$structure->ctype_primary) . '/' . strtolower(@$structure->ctype_secondary);
- if (!$ctype) {
- $ctype = 'text/plain';
- }
- switch ($ctype) {
- case 'text/html':
- case 'text/plain':
- if (!empty($structure->disposition) AND $structure->disposition == 'attachment') {
- decodeMail_addAttachment($structure);
- } else {
- $var = $ctype == 'text/html' ? 'html' : 'text';
- @$output->$var .= $structure->body;
- }
- break;
- case 'multipart/report':
- case 'multipart/signed':
- case 'multipart/mixed':
- case 'multipart/alternative':
- case 'multipart/related':
- case 'multipart/digest':
- case 'multipart/parallel':
- case 'message/rfc822':
- case 'message/disposition-notification':
- if (!empty($structure->parts)) {
- if ($ctype == 'multipart/related') {
- $GLOBALS['decodeMail_inRelated'] = true;
- }
- for ($i=0; $i<count($structure->parts); $i++) {
- decodeMail_traverseStructure($structure->parts[$i]);
- }
- $GLOBALS['decodeMail_inRelated'] = false;
- }
- break;
-
- default:
- decodeMail_addAttachment($structure);
- }
- }
- /**
- * Private function used by the traverse function (above)
- * to add an attachment to the output object.
- */
- function decodeMail_addAttachment($structure)
- {
- $output = &$GLOBALS['decodeMail_output'];
- // Determine filename and extension
- if (!empty($structure->d_parameters['filename'])) {
- $filename = $structure->d_parameters['filename'];
- if (preg_match('/^.+.(w{3,4})$/', $filename, $matches)) {
- $extension = $matches[1];
- } else {
- $extension = '';
- }
- } else {
- $filename = '';
- $extension = '';
- }
- // Attachment or embedded?
- $var = $GLOBALS['decodeMail_inRelated'] ? 'embedded' : 'attachments';
- $output->{$var}[] = array('data' => @$structure->body,
- 'size' => @strlen($structure->body),
- 'filename' => $filename,
- 'extension' => $extension);
- }
- ?>