1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
|
<?php
/**
* HTML MIME Mail composer class
*
* @copyright 2002-2003 Richard Heyes
* @copyright 2003-2005 The PHP Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* o The names of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Tobias Ratschiller <tobias@dnet.it> and
* @author Sascha Schumann <sascha@schumann.cx>
* @author Richard Heyes <richard@phpguru.org>
* @author Tomas V.V.Cox <cox@idecnet.com> (port to PEAR)
*
* @author Jeroen Roos
*
* @package Zoph
*/
namespace mail;
/**
* Mime mail composer class. Can handle: text and html bodies, embedded html
* images and attachments.
* @author Tobias Ratschiller <tobias@dnet.it> and
* @author Sascha Schumann <sascha@schumann.cx>
* @author Richard Heyes <richard@phpguru.org>
* @author Tomas V.V.Cox <cox@idecnet.com> (port to PEAR)
*
* @author Jeroen Roos
*
* @package Zoph
*/
class mime {
/** @var string Contains the plain text part of the email */
private $txtbody;
/** @var string Contains the html part of the email */
private $htmlbody;
/** @var array list of the attached images */
private $html_images = array();
/** @var array list of the attachments */
private $parts = array();
/** @var array Build parameters */
private $build_params = array();
/** @var array Headers for the mail */
private $headers = array();
/**
* Constructor function
*/
public function __construct() {
$this->build_params = array(
'text_encoding' => '7bit',
'html_encoding' => 'quoted-printable',
'7bit_wrap' => 998,
'html_charset' => 'utf-8',
'text_charset' => 'utf-8',
'head_charset' => 'utf-8'
);
}
/**
* Accessor function to set the body text. Body text is used if
* it's not an html mail being sent or else is used to fill the
* text/plain part that emails clients who don't support
* html should show.
*
* @param string Either a string or the file name with the contents
* @param bool If true the first param should be treated as a file name,
* else as a string (default)
* @param bool If true the text or file is appended to the existing body,
* else the old body is overwritten
* @return bool true on success
*/
public function setTXTBody($data, $isfile = false, $append = false) {
if (!$isfile) {
if (!$append) {
$this->txtbody = $data;
} else {
$this->txtbody .= $data;
}
} else {
$cont = $this->file2str($data);
if (!$append) {
$this->txtbody = $cont;
} else {
$this->txtbody .= $cont;
}
}
return true;
}
/**
* Adds a html part to the mail
*
* @param string Either a string or the file name with the contents
* @param bool If true the first param should be treated as a file name,
* else as a string (default)
* @return bool true on succes
*/
public function setHTMLBody($data, $isfile = false) {
if (!$isfile) {
$this->htmlbody = $data;
} else {
$cont = $this->file2str($data);
$this->htmlbody = $cont;
}
return true;
}
/**
* Adds an image to the list of embedded images. The source is a string containing the image.
*
* @paramstring The image data.
* @param string The file name
* @param string The content type
* @return bool true
*/
public function addHTMLImageFromString($filedata, $filename,
$c_type='application/octet-stream') {
$filename = basename($filename);
$this->html_images[] = array(
'body' => $filedata,
'name' => $filename,
'c_type' => $c_type,
'cid' => md5(uniqid(time()))
);
return true;
}
/**
* Adds an image to the list of embedded images. The source is a file on disk.
*
* @param string The file to be used as attachment
* @param string The content type
* @param string encoding.
*/
public function addHTMLImageFromFile($file,
$c_type='application/octet-stream') {
$filedata = $this->file2str($file);
return $this->addHTMLImageFromString($filedata, $file, $c_type);
}
/**
* Adds a file to the list of attachments. The source is a string containing the
* contents of the file.
*
* @param string The file data to use as attachment
* @param string The content type
* @param string The filename of the attachment.
* @param string encoding.
* @throws mail\exception
*/
public function addAttachmentFromString($filedata, $filename,
$c_type = 'application/octet-stream', $encoding = 'base64') {
if (empty($filename)) {
throw new exception("The supplied filename for the attachment can\'t be empty");
}
$filename = basename($filename);
$this->parts[] = array(
'body' => $filedata,
'name' => $filename,
'c_type' => $c_type,
'encoding' => $encoding
);
}
/**
* Adds a file to the list of attachments. The source is a file on disk.
*
* @param string The file to be used as attachment
* @param string The content type
* @param string encoding.
*/
public function addAttachmentFromFile($file,
$c_type = 'application/octet-stream', $encoding = 'base64') {
$filedata=$this->file2str($file);
$this->addAttachmentFromString($filedata, $file, $c_type, $encoding);
}
/**
* Get the contents of the given file name as string
*
* @param string path of file to process
* @return string contents of $file_name
* @throws mail\exception
*/
private function file2str($file_name) {
if (!is_readable($file_name)) {
throw new exception('File is not readable ' . $file_name);
}
if (!$fd = fopen($file_name, 'rb')) {
throw new exception('Could not open ' . $file_name);
}
$filesize = filesize($file_name);
if ($filesize == 0){
$cont = "";
}else{
$cont = fread($fd, $filesize);
}
fclose($fd);
return $cont;
}
/**
* Adds a text subpart to the mail\mimePart object and
* returns it during the build process.
*
* @param mixed The object to add the part to, or null if a new object is to be created.
* @param string The text to add.
* @return mail\mimePart The text mail\mimePart object
*/
private function addTextPart($obj, $text) {
$params['content_type'] = 'text/plain';
$params['encoding'] = $this->build_params['text_encoding'];
$params['charset'] = $this->build_params['text_charset'];
if (is_object($obj)) {
return $obj->addSubpart($text, $params);
} else {
return new mimePart($text, $params);
}
}
/**
* Adds a html subpart to the mail\mimePart object and
* returns it during the build process.
*
* @param mixed The object to add the part to, or null if a new object is to be created.
* @return mail\mimePart The html mail\mimePart object
*/
private function addHtmlPart($obj) {
$params['content_type'] = 'text/html';
$params['encoding'] = $this->build_params['html_encoding'];
$params['charset'] = $this->build_params['html_charset'];
if (is_object($obj)) {
return $obj->addSubpart($this->htmlbody, $params);
} else {
return new mimePart($this->htmlbody, $params);
}
}
/**
* Creates a new mail\mimePart object, using multipart/mixed as
* the initial content-type and returns it during the
* build process.
*
* @return mail\mimePart The multipart/mixed mail\mimePart object
*/
private function addMixedPart() {
$params['content_type'] = 'multipart/mixed';
return new mimePart('', $params);
}
/**
* Adds a multipart/alternative part to a mail\mimePart
* object (or creates one), and returns it during
* the build process.
*
* @param mixed The object to add the part to, or
* null if a new object is to be created.
* @return mail\mimePart The multipart/mixed mail\mimePart object
*/
private function addAlternativePart($obj) {
$params['content_type'] = 'multipart/alternative';
if (is_object($obj)) {
return $obj->addSubpart('', $params);
} else {
return new mimePart('', $params);
}
}
/**
* Adds a multipart/related part to a mail\mimePart
* object (or creates one), and returns it during
* the build process.
*
* @param mixed The object to add the part to, or
* null if a new object is to be created
* @return mail\mimePart The multipart/mixed mail\mimePart object
*/
private function addRelatedPart($obj) {
$params['content_type'] = 'multipart/related';
if (is_object($obj)) {
return $obj->addSubpart('', $params);
} else {
return new mimePart('', $params);
}
}
/**
* Adds an html image subpart to a mail\mimePart object
* and returns it during the build process.
*
* @param mail\mimePart The mail\mimePart to add the image to
* @param array The image information
* @return mail\mimePart The image mail\mimePart object
*/
private function addHtmlImagePart(mimePart $obj, $value) {
$params['content_type'] = $value['c_type'];
$params['encoding'] = 'base64';
$params['disposition'] = 'inline';
$params['dfilename'] = $value['name'];
$params['cid'] = $value['cid'];
$obj->addSubpart($value['body'], $params);
}
/**
* Adds an attachment subpart to a mail\mimePart object
* and returns it during the build process.
*
* @param mail\mimePart The mail\mimePart to add the image to
* @param array The attachment information
* @return mail\mimePart The image mail\mimePart object
*/
private function addAttachmentPart(mimePart $obj, $value) {
$params['content_type'] = $value['c_type'];
$params['encoding'] = $value['encoding'];
$params['disposition'] = 'attachment';
$params['dfilename'] = $value['name'];
$obj->addSubpart($value['body'], $params);
}
/**
* Builds the multipart message from the list ($this->parts) and
* returns the mime content.
*
* @param array Build parameters that change the way the email
* is built. Should be associative. Can contain:
* text_encoding - What encoding to use for plain text
* Default is 7bit
* html_encoding - What encoding to use for html
* Default is quoted-printable
* 7bit_wrap - Number of characters before text is
* wrapped in 7bit encoding
* Default is 998
* html_charset - The character set to use for html.
* Default is iso-8859-1
* text_charset - The character set to use for text.
* Default is iso-8859-1
* head_charset - The character set to use for headers.
* Default is iso-8859-1
* @return string The mime content
*/
public function get($build_params = null) {
if (isset($build_params)) {
foreach ($build_params as $key => $value) {
$this->build_params[$key] = $value;
}
}
if (!empty($this->html_images) && isset($this->htmlbody)) {
foreach ($this->html_images as $value) {
$regex = '#(\s)((?i)src|background|href(?-i))\s*=\s*(["\']?)' .
preg_quote($value['name'], '#') . '\3#';
$rep = '\1\2=\3cid:' . $value['cid'] .'\3';
$this->htmlbody = preg_replace($regex, $rep,
$this->htmlbody
);
}
}
$null = null;
$attachments = !empty($this->parts) ? true : false;
$html_images = !empty($this->html_images) ? true : false;
$html = !empty($this->htmlbody) ? true : false;
$text = (!$html && !empty($this->txtbody)) ? true : false;
switch (true) {
case $text && !$attachments:
$message = $this->addTextPart($null, $this->txtbody);
break;
case !$text && !$html && $attachments:
$message = $this->addMixedPart();
foreach ($this->parts as $part) {
$this->addAttachmentPart($message, $part);
}
break;
case $text && $attachments:
$message = $this->addMixedPart();
$this->addTextPart($message, $this->txtbody);
foreach ($this->parts as $part) {
$this->addAttachmentPart($message, $part);
}
break;
case $html && !$attachments && !$html_images:
if (isset($this->txtbody)) {
$message = $this->addAlternativePart($null);
$this->addTextPart($message, $this->txtbody);
$this->addHtmlPart($message);
} else {
$message =$this->addHtmlPart($null);
}
break;
case $html && !$attachments && $html_images:
if (isset($this->txtbody)) {
$message =$this->addAlternativePart($null);
$this->addTextPart($message, $this->txtbody);
$related = $this->addRelatedPart($message);
} else {
$message = $this->addRelatedPart($null);
$related = $message;
}
$this->addHtmlPart($related);
foreach ($this->html_images as $img) {
$this->addHtmlImagePart($related, $img);
}
break;
case $html && $attachments && !$html_images:
$message = $this->addMixedPart();
if (isset($this->txtbody)) {
$alt = $this->addAlternativePart($message);
$this->addTextPart($alt, $this->txtbody);
$this->addHtmlPart($alt);
} else {
$this->addHtmlPart($message);
}
foreach ($this->parts as $part) {
$this->addAttachmentPart($message, $part);
}
break;
case $html && $attachments && $html_images:
$message = $this->addMixedPart();
if (isset($this->txtbody)) {
$alt = $this->addAlternativePart($message);
$this->addTextPart($alt, $this->txtbody);
$rel = $this->addRelatedPart($alt);
} else {
$rel = $this->addRelatedPart($message);
}
$this->addHtmlPart($rel);
foreach ($this->html_images as $img) {
$this->addHtmlImagePart($rel, $img);
}
foreach ($this->parts as $part) {
$this->addAttachmentPart($message, $part);
}
break;
}
if (isset($message)) {
$output = $message->encode();
$this->headers = array_merge($this->headers,
$output['headers']);
return $output['body'];
} else {
return false;
}
}
/**
* Returns an array with the headers needed to prepend to the email
* (MIME-Version and Content-Type). Format of argument is:
* $array['header-name'] = 'header-value';
*
* @param array Assoc array with any extra headers. Optional.
* @return array Assoc array with the mime headers
*/
public function headers(array $xtra_headers = null) {
// Content-Type header should already be present,
// So just add mime version header
$headers['MIME-Version'] = '1.0';
if (isset($xtra_headers)) {
$headers = array_merge($headers, $xtra_headers);
}
$this->headers = array_merge($headers, $this->headers);
return $this->encodeHeaders($this->headers);
}
/**
* Get the text version of the headers
* (useful if you want to use the PHP mail() function)
*
* @param array headers Assoc array with any extra headers. Optional.
* @return string Plain text headers
*/
public function txtHeaders(array $xtra_headers = null) {
$headers = $this->headers($xtra_headers);
$ret = '';
foreach ($headers as $key => $val) {
$ret .= "$key: $val" . PHP_EOL;
}
return $ret;
}
/**
* Sets the Subject header
*
* @param string $subject String to set the subject to
*/
public function setSubject($subject) {
$this->headers['Subject'] = $subject;
}
/**
* Set an email to the From (the sender) header
*
* @param string $email The email address to add
*/
public function setFrom($email) {
$this->headers['From'] = $email;
}
/**
* Add an email to the Cc (carbon copy) header
* (multiple calls to this method are allowed)
*
* @param string The email address to add
*/
public function addCc($email) {
if (isset($this->headers['Cc'])) {
$this->headers['Cc'] .= ", $email";
} else {
$this->headers['Cc'] = $email;
}
}
/**
* Add an email to the Bcc (blind carbon copy) header
* (multiple calls to this method are allowed)
*
* @param string The email address to add
*/
public function addBcc($email) {
if (isset($this->headers['Bcc'])) {
$this->headers['Bcc'] .= ", $email";
} else {
$this->headers['Bcc'] = $email;
}
}
/**
* Encodes a header as per RFC2047
*
* @param string The header data to encode
* @return string Encoded data
*/
private function encodeHeaders($input) {
foreach ($input as $hdr_name => $hdr_value) {
preg_match_all('/(\w*[\x80-\xFF]+\w*)/', $hdr_value, $matches);
foreach ($matches[1] as $value) {
$replacement = preg_replace('/([\x80-\xFF])/e',
'"=" .
strtoupper(dechex(ord("\1")))',
$value);
$hdr_value = str_replace($value, '=?' .
$this->build_params['head_charset'] .
'?Q?' . $replacement . '?=',
$hdr_value);
}
$input[$hdr_name] = $hdr_value;
}
return $input;
}
} // End of class
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
?>
|