[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 3 require_once dirname(__FILE__) . '/Part.php'; 4 5 /** 6 * The MIME_Message:: class provides methods for creating and manipulating 7 * MIME email messages. 8 * 9 * $Horde: framework/MIME/MIME/Message.php,v 1.76.10.12 2006/01/01 21:28:24 jan Exp $ 10 * 11 * Copyright 1999-2006 Chuck Hagenbuch <chuck@horde.org> 12 * Copyright 2002-2006 Michael Slusarz <slusarz@bigworm.colorado.edu> 13 * 14 * See the enclosed file COPYING for license information (LGPL). If you 15 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 16 * 17 * @author Chuck Hagenbuch <chuck@horde.org> 18 * @author Michael Slusarz <slusarz@bigworm.colorado.edu> 19 * @since Horde 1.3 20 * @package Horde_MIME 21 */ 22 class MIME_Message extends MIME_Part { 23 24 /** 25 * Has the message been parsed via buildMessage()? 26 * 27 * @var boolean 28 */ 29 var $_build = false; 30 31 /** 32 * The server to default unqualified addresses to. 33 * 34 * @var string 35 */ 36 var $_defaultServer = null; 37 38 /** 39 * Constructor - creates a new MIME email message. 40 * 41 * @param string $defaultServer The server to default unqualified 42 * addresses to. 43 */ 44 function MIME_Message($defaultServer = null) 45 { 46 if (is_null($defaultServer)) { 47 $this->_defaultServer = $_SERVER['SERVER_NAME']; 48 } else { 49 $this->_defaultServer = $defaultServer; 50 } 51 } 52 53 /** 54 * Create a MIME_Message object from a MIME_Part object. 55 * This function can be called statically via: 56 * MIME_Message::convertMIMEPart(); 57 * 58 * @param MIME_Part &$mime_part The MIME_Part object. 59 * @param string $server The server to default unqualified 60 * addresses to. 61 * 62 * @return MIME_Message The new MIME_Message object. 63 */ 64 function &convertMIMEPart(&$mime_part, $server = null) 65 { 66 if (!$mime_part->getMIMEId()) { 67 $mime_part->setMIMEId(1); 68 } 69 70 $mime_message = &new MIME_Message($server); 71 $mime_message->addPart($mime_part); 72 $mime_message->buildMessage(); 73 74 return $mime_message; 75 } 76 77 /** 78 * Sends this message. 79 * 80 * @param string $email The address list to send to. 81 * @param mixed &$headers The MIME_Headers object holding this message's 82 * headers, or a hash with header->value mappings. 83 * @param string $driver The Mail driver to use (since Horde 3.0.4). 84 * @param array $params Any parameters necessary for the Mail driver 85 * (since Horde 3.0.4). 86 * 87 * @return mixed True on success, PEAR_Error on error. 88 */ 89 function send($email, &$headers, $driver = null, $params = array()) 90 { 91 global $conf; 92 static $mailer; 93 94 if (!isset($mailer)) { 95 require_once 'Mail.php'; 96 if (!isset($driver)) { 97 $driver = $conf['mailer']['type']; 98 $params = $conf['mailer']['params']; 99 } 100 $mailer = Mail::factory($driver, $params); 101 } 102 103 $msg = $this->toString(); 104 if (is_object($headers)) { 105 $headerArray = $this->encode($headers->toArray(), $this->getCharset()); 106 } else { 107 $headerArray = $this->encode($headers, $this->getCharset()); 108 } 109 110 /* Make sure the message has a trailing newline. */ 111 if (substr($msg, -1) != "\n") { 112 $msg .= "\n"; 113 } 114 115 $result = $mailer->send(MIME::encodeAddress($email), $headerArray, $msg); 116 117 if (is_a($result, 'PEAR_Error') && 118 $conf['mailer']['type'] == 'sendmail') { 119 // Interpret return values as defined in /usr/include/sysexits.h 120 switch ($result->getCode()) { 121 case 64: // EX_USAGE 122 $error = 'sendmail: ' . _("command line usage error") . ' (64)'; 123 break; 124 125 case 65: // EX_DATAERR 126 $error = 'sendmail: ' . _("data format error") . ' (65)'; 127 break; 128 129 case 66: // EX_NOINPUT 130 $error = 'sendmail: ' . _("cannot open input") . ' (66)'; 131 break; 132 133 case 67: // EX_NOUSER 134 $error = 'sendmail: ' . _("addressee unknown") . ' (67)'; 135 break; 136 137 case 68: // EX_NOHOST 138 $error = 'sendmail: ' . _("host name unknown") . ' (68)'; 139 break; 140 141 case 69: // EX_UNAVAILABLE 142 $error = 'sendmail: ' . _("service unavailable") . ' (69)'; 143 break; 144 145 case 70: // EX_SOFTWARE 146 $error = 'sendmail: ' . _("internal software error") . ' (70)'; 147 break; 148 149 case 71: // EX_OSERR 150 $error = 'sendmail: ' . _("system error") . ' (71)'; 151 break; 152 153 case 72: // EX_OSFILE 154 $error = 'sendmail: ' . _("critical system file missing") . ' (72)'; 155 break; 156 157 case 73: // EX_CANTCREAT 158 $error = 'sendmail: ' . _("cannot create output file") . ' (73)'; 159 break; 160 161 case 74: // EX_IOERR 162 $error = 'sendmail: ' . _("input/output error") . ' (74)'; 163 break; 164 165 case 75: // EX_TEMPFAIL 166 $error = 'sendmail: ' . _("temporary failure") . ' (75)'; 167 break; 168 169 case 76: // EX_PROTOCOL 170 $error = 'sendmail: ' . _("remote error in protocol") . ' (76)'; 171 break; 172 173 case 77: // EX_NOPERM 174 $error = 'sendmail: ' . _("permission denied") . ' (77)'; 175 break; 176 177 case 78: // EX_CONFIG 178 $error = 'sendmail: ' . _("configuration error") . ' (78)'; 179 break; 180 181 case 79: // EX_NOTFOUND 182 $error = 'sendmail: ' . _("entry not found") . ' (79)'; 183 break; 184 185 default: 186 $error = $result; 187 } 188 return PEAR::raiseError($error); 189 } 190 191 return $result; 192 } 193 194 /** 195 * Take a set of headers and make sure they are encoded properly. 196 * 197 * @param array $headers The headers to encode. 198 * @param string $charset The character set to use. 199 * 200 * @return array The array of encoded headers. 201 */ 202 function encode($headers, $charset) 203 { 204 require_once 'Horde/MIME.php'; 205 206 $addressKeys = array('To', 'Cc', 'Bcc', 'From'); 207 foreach ($headers as $key => $val) { 208 if (in_array($key, $addressKeys)) { 209 $text = MIME::encodeAddress($val, $charset, $this->_defaultServer); 210 if (is_a($text, 'PEAR_Error')) { 211 $text = $val; 212 } 213 } else { 214 $text = MIME::encode($val, $charset); 215 } 216 $headers[$key] = MIME::wrapHeaders($key, $text, $this->getEOL()); 217 } 218 219 return $headers; 220 } 221 222 /** 223 * Add the proper set of MIME headers for this message to an array. 224 * 225 * @param array $headers The headers to add the MIME headers to. 226 * 227 * @return array The full set of headers including MIME headers. 228 */ 229 function header($headers = array()) 230 { 231 /* Per RFC 2045 [4], this MUST appear in the message headers. */ 232 $headers['MIME-Version'] = '1.0'; 233 234 if ($this->_build) { 235 return parent::header($headers); 236 } else { 237 $this->buildMessage(); 238 return $this->encode($this->header($headers), $this->getCharset()); 239 } 240 } 241 242 /** 243 * Return the entire message contents, including headers, as a string. 244 * 245 * @return string The encoded, generated message. 246 */ 247 function toString() 248 { 249 if ($this->_build) { 250 return parent::toString(false); 251 } else { 252 $this->buildMessage(); 253 return $this->toString(); 254 } 255 } 256 257 /** 258 * Build message from current contents. 259 */ 260 function buildMessage() 261 { 262 if ($this->_build) { 263 return; 264 } 265 266 if (empty($this->_flags['setType'])) { 267 if (count($this->_parts) > 1) { 268 $this->setType('multipart/mixed'); 269 } else { 270 /* Copy the information from the single part to the current 271 base part. */ 272 if (($obVars = get_object_vars(reset($this->_parts)))) { 273 foreach ($obVars as $key => $val) { 274 $this->$key = $val; 275 } 276 } 277 } 278 } 279 280 /* Set the build flag now. */ 281 $this->_build = true; 282 } 283 284 /** 285 * Get a list of all MIME subparts. 286 * 287 * @return array An array of the MIME_Part subparts. 288 */ 289 function getParts() 290 { 291 if ($this->_build) { 292 return parent::getParts(); 293 } else { 294 $this->buildMessage(); 295 return $this->getParts(); 296 } 297 } 298 299 /** 300 * Return the base part of the message. This function does NOT 301 * return a reference to make sure that the whole MIME_Message 302 * object isn't accidentally modified. 303 * 304 * @return MIME_Message The base MIME_Part of the message. 305 */ 306 function getBasePart() 307 { 308 $this->buildMessage(); 309 return $this; 310 } 311 312 /** 313 * Retrieve a specific MIME part. 314 * 315 * @param string $id The MIME_Part ID string. 316 * 317 * @return MIME_Part The MIME_Part requested, or false if the part 318 * doesn't exist. 319 */ 320 function &getPart($id) 321 { 322 if ($this->_build) { 323 $part = parent::getPart($id); 324 } else { 325 $this->buildMessage(); 326 $part = $this->getPart($id); 327 } 328 if (is_a($part, 'MIME_Message')) { 329 $newpart = &new MIME_Part(); 330 $skip = array('_build', '_defaultServer'); 331 foreach (array_keys(get_object_vars($part)) as $key) { 332 /* Ignore local variables that aren't a part of the original 333 * class. */ 334 if (!in_array($key, $skip)) { 335 $newpart->$key = &$part->$key; 336 } 337 } 338 return $newpart; 339 } else { 340 return $part; 341 } 342 } 343 344 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |