| [ Index ] |
PHP Cross Reference of bbPress |
[Summary view] [Print] [Text view]
1 <?php 2 // Last sync [WP11537] 3 4 /*~ class.phpmailer.php 5 .---------------------------------------------------------------------------. 6 | Software: PHPMailer - PHP email class | 7 | Version: 2.0.4 | 8 | Contact: via sourceforge.net support pages (also www.codeworxtech.com) | 9 | Info: http://phpmailer.sourceforge.net | 10 | Support: http://sourceforge.net/projects/phpmailer/ | 11 | ------------------------------------------------------------------------- | 12 | Author: Andy Prevost (project admininistrator) | 13 | Author: Brent R. Matzelle (original founder) | 14 | Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved. | 15 | Copyright (c) 2001-2003, Brent R. Matzelle | 16 | ------------------------------------------------------------------------- | 17 | License: Distributed under the Lesser General Public License (LGPL) | 18 | http://www.gnu.org/copyleft/lesser.html | 19 | This program is distributed in the hope that it will be useful - WITHOUT | 20 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 21 | FITNESS FOR A PARTICULAR PURPOSE. | 22 | ------------------------------------------------------------------------- | 23 | We offer a number of paid services (www.codeworxtech.com): | 24 | - Web Hosting on highly optimized fast and secure servers | 25 | - Technology Consulting | 26 | - Oursourcing (highly qualified programmers and graphic designers) | 27 '---------------------------------------------------------------------------' 28 */ 29 /** 30 * PHPMailer - PHP email transport class 31 * @package PHPMailer 32 * @author Andy Prevost 33 * @copyright 2004 - 2009 Andy Prevost 34 */ 35 36 class PHPMailer { 37 38 ///////////////////////////////////////////////// 39 // PROPERTIES, PUBLIC 40 ///////////////////////////////////////////////// 41 42 /** 43 * Email priority (1 = High, 3 = Normal, 5 = low). 44 * @var int 45 */ 46 var $Priority = 3; 47 48 /** 49 * Sets the CharSet of the message. 50 * @var string 51 */ 52 var $CharSet = 'iso-8859-1'; 53 54 /** 55 * Sets the Content-type of the message. 56 * @var string 57 */ 58 var $ContentType = 'text/plain'; 59 60 /** 61 * Sets the Encoding of the message. Options for this are "8bit", 62 * "7bit", "binary", "base64", and "quoted-printable". 63 * @var string 64 */ 65 var $Encoding = '8bit'; 66 67 /** 68 * Holds the most recent mailer error message. 69 * @var string 70 */ 71 var $ErrorInfo = ''; 72 73 /** 74 * Sets the From email address for the message. 75 * @var string 76 */ 77 var $From = 'root@localhost'; 78 79 /** 80 * Sets the From name of the message. 81 * @var string 82 */ 83 var $FromName = 'Root User'; 84 85 /** 86 * Sets the Sender email (Return-Path) of the message. If not empty, 87 * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode. 88 * @var string 89 */ 90 var $Sender = ''; 91 92 /** 93 * Sets the Subject of the message. 94 * @var string 95 */ 96 var $Subject = ''; 97 98 /** 99 * Sets the Body of the message. This can be either an HTML or text body. 100 * If HTML then run IsHTML(true). 101 * @var string 102 */ 103 var $Body = ''; 104 105 /** 106 * Sets the text-only body of the message. This automatically sets the 107 * email to multipart/alternative. This body can be read by mail 108 * clients that do not have HTML email capability such as mutt. Clients 109 * that can read HTML will view the normal Body. 110 * @var string 111 */ 112 var $AltBody = ''; 113 114 /** 115 * Sets word wrapping on the body of the message to a given number of 116 * characters. 117 * @var int 118 */ 119 var $WordWrap = 0; 120 121 /** 122 * Method to send mail: ("mail", "sendmail", or "smtp"). 123 * @var string 124 */ 125 var $Mailer = 'mail'; 126 127 /** 128 * Sets the path of the sendmail program. 129 * @var string 130 */ 131 var $Sendmail = '/usr/sbin/sendmail'; 132 133 /** 134 * Path to PHPMailer plugins. This is now only useful if the SMTP class 135 * is in a different directory than the PHP include path. 136 * @var string 137 */ 138 var $PluginDir = ''; 139 140 /** 141 * Holds PHPMailer version. 142 * @var string 143 */ 144 var $Version = "2.0.4"; 145 146 /** 147 * Sets the email address that a reading confirmation will be sent. 148 * @var string 149 */ 150 var $ConfirmReadingTo = ''; 151 152 /** 153 * Sets the hostname to use in Message-Id and Received headers 154 * and as default HELO string. If empty, the value returned 155 * by SERVER_NAME is used or 'localhost.localdomain'. 156 * @var string 157 */ 158 var $Hostname = ''; 159 160 /** 161 * Sets the message ID to be used in the Message-Id header. 162 * If empty, a unique id will be generated. 163 * @var string 164 */ 165 var $MessageID = ''; 166 167 ///////////////////////////////////////////////// 168 // PROPERTIES FOR SMTP 169 ///////////////////////////////////////////////// 170 171 /** 172 * Sets the SMTP hosts. All hosts must be separated by a 173 * semicolon. You can also specify a different port 174 * for each host by using this format: [hostname:port] 175 * (e.g. "smtp1.example.com:25;smtp2.example.com"). 176 * Hosts will be tried in order. 177 * @var string 178 */ 179 var $Host = 'localhost'; 180 181 /** 182 * Sets the default SMTP server port. 183 * @var int 184 */ 185 var $Port = 25; 186 187 /** 188 * Sets the SMTP HELO of the message (Default is $Hostname). 189 * @var string 190 */ 191 var $Helo = ''; 192 193 /** 194 * Sets connection prefix. 195 * Options are "", "ssl" or "tls" 196 * @var string 197 */ 198 var $SMTPSecure = ""; 199 200 /** 201 * Sets SMTP authentication. Utilizes the Username and Password variables. 202 * @var bool 203 */ 204 var $SMTPAuth = false; 205 206 /** 207 * Sets SMTP username. 208 * @var string 209 */ 210 var $Username = ''; 211 212 /** 213 * Sets SMTP password. 214 * @var string 215 */ 216 var $Password = ''; 217 218 /** 219 * Sets the SMTP server timeout in seconds. This function will not 220 * work with the win32 version. 221 * @var int 222 */ 223 var $Timeout = 10; 224 225 /** 226 * Sets SMTP class debugging on or off. 227 * @var bool 228 */ 229 var $SMTPDebug = false; 230 231 /** 232 * Prevents the SMTP connection from being closed after each mail 233 * sending. If this is set to true then to close the connection 234 * requires an explicit call to SmtpClose(). 235 * @var bool 236 */ 237 var $SMTPKeepAlive = false; 238 239 /** 240 * Provides the ability to have the TO field process individual 241 * emails, instead of sending to entire TO addresses 242 * @var bool 243 */ 244 var $SingleTo = false; 245 246 ///////////////////////////////////////////////// 247 // PROPERTIES, PRIVATE 248 ///////////////////////////////////////////////// 249 250 var $smtp = NULL; 251 var $to = array(); 252 var $cc = array(); 253 var $bcc = array(); 254 var $ReplyTo = array(); 255 var $attachment = array(); 256 var $CustomHeader = array(); 257 var $message_type = ''; 258 var $boundary = array(); 259 var $language = array(); 260 var $error_count = 0; 261 var $LE = "\n"; 262 var $sign_cert_file = ""; 263 var $sign_key_file = ""; 264 var $sign_key_pass = ""; 265 266 ///////////////////////////////////////////////// 267 // METHODS, VARIABLES 268 ///////////////////////////////////////////////// 269 270 /** 271 * Sets message type to HTML. 272 * @param bool $bool 273 * @return void 274 */ 275 function IsHTML($bool) { 276 if($bool == true) { 277 $this->ContentType = 'text/html'; 278 } else { 279 $this->ContentType = 'text/plain'; 280 } 281 } 282 283 /** 284 * Sets Mailer to send message using SMTP. 285 * @return void 286 */ 287 function IsSMTP() { 288 $this->Mailer = 'smtp'; 289 } 290 291 /** 292 * Sets Mailer to send message using PHP mail() function. 293 * @return void 294 */ 295 function IsMail() { 296 $this->Mailer = 'mail'; 297 } 298 299 /** 300 * Sets Mailer to send message using the $Sendmail program. 301 * @return void 302 */ 303 function IsSendmail() { 304 $this->Mailer = 'sendmail'; 305 } 306 307 /** 308 * Sets Mailer to send message using the qmail MTA. 309 * @return void 310 */ 311 function IsQmail() { 312 $this->Sendmail = '/var/qmail/bin/sendmail'; 313 $this->Mailer = 'sendmail'; 314 } 315 316 ///////////////////////////////////////////////// 317 // METHODS, RECIPIENTS 318 ///////////////////////////////////////////////// 319 320 /** 321 * Adds a "To" address. 322 * @param string $address 323 * @param string $name 324 * @return void 325 */ 326 function AddAddress($address, $name = '') { 327 $cur = count($this->to); 328 $this->to[$cur][0] = trim($address); 329 $this->to[$cur][1] = $name; 330 } 331 332 /** 333 * Adds a "Cc" address. Note: this function works 334 * with the SMTP mailer on win32, not with the "mail" 335 * mailer. 336 * @param string $address 337 * @param string $name 338 * @return void 339 */ 340 function AddCC($address, $name = '') { 341 $cur = count($this->cc); 342 $this->cc[$cur][0] = trim($address); 343 $this->cc[$cur][1] = $name; 344 } 345 346 /** 347 * Adds a "Bcc" address. Note: this function works 348 * with the SMTP mailer on win32, not with the "mail" 349 * mailer. 350 * @param string $address 351 * @param string $name 352 * @return void 353 */ 354 function AddBCC($address, $name = '') { 355 $cur = count($this->bcc); 356 $this->bcc[$cur][0] = trim($address); 357 $this->bcc[$cur][1] = $name; 358 } 359 360 /** 361 * Adds a "Reply-To" address. 362 * @param string $address 363 * @param string $name 364 * @return void 365 */ 366 function AddReplyTo($address, $name = '') { 367 $cur = count($this->ReplyTo); 368 $this->ReplyTo[$cur][0] = trim($address); 369 $this->ReplyTo[$cur][1] = $name; 370 } 371 372 ///////////////////////////////////////////////// 373 // METHODS, MAIL SENDING 374 ///////////////////////////////////////////////// 375 376 /** 377 * Creates message and assigns Mailer. If the message is 378 * not sent successfully then it returns false. Use the ErrorInfo 379 * variable to view description of the error. 380 * @return bool 381 */ 382 function Send() { 383 $header = ''; 384 $body = ''; 385 $result = true; 386 387 if((count($this->to) + count($this->cc) + count($this->bcc)) < 1) { 388 $this->SetError($this->Lang('provide_address')); 389 return false; 390 } 391 392 /* Set whether the message is multipart/alternative */ 393 if(!empty($this->AltBody)) { 394 $this->ContentType = 'multipart/alternative'; 395 } 396 397 $this->error_count = 0; // reset errors 398 $this->SetMessageType(); 399 $header .= $this->CreateHeader(); 400 $body = $this->CreateBody(); 401 402 if($body == '') { 403 return false; 404 } 405 406 /* Choose the mailer */ 407 switch($this->Mailer) { 408 case 'sendmail': 409 $result = $this->SendmailSend($header, $body); 410 break; 411 case 'smtp': 412 $result = $this->SmtpSend($header, $body); 413 break; 414 case 'mail': 415 $result = $this->MailSend($header, $body); 416 break; 417 default: 418 $result = $this->MailSend($header, $body); 419 break; 420 //$this->SetError($this->Mailer . $this->Lang('mailer_not_supported')); 421 //$result = false; 422 //break; 423 } 424 425 return $result; 426 } 427 428 /** 429 * Sends mail using the $Sendmail program. 430 * @access private 431 * @return bool 432 */ 433 function SendmailSend($header, $body) { 434 if ($this->Sender != '') { 435 $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender)); 436 } else { 437 $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail)); 438 } 439 440 if(!@$mail = popen($sendmail, 'w')) { 441 $this->SetError($this->Lang('execute') . $this->Sendmail); 442 return false; 443 } 444 445 fputs($mail, $header); 446 fputs($mail, $body); 447 448 $result = pclose($mail); 449 if (version_compare(phpversion(), '4.2.3') == -1) { 450 $result = $result >> 8 & 0xFF; 451 } 452 if($result != 0) { 453 $this->SetError($this->Lang('execute') . $this->Sendmail); 454 return false; 455 } 456 return true; 457 } 458 459 /** 460 * Sends mail using the PHP mail() function. 461 * @access private 462 * @return bool 463 */ 464 function MailSend($header, $body) { 465 466 $to = ''; 467 for($i = 0; $i < count($this->to); $i++) { 468 if($i != 0) { $to .= ', '; } 469 $to .= $this->AddrFormat($this->to[$i]); 470 } 471 472 $toArr = split(',', $to); 473 474 $params = sprintf("-oi -f %s", $this->Sender); 475 if ($this->Sender != '' && strlen(ini_get('safe_mode')) < 1) { 476 $old_from = ini_get('sendmail_from'); 477 ini_set('sendmail_from', $this->Sender); 478 if ($this->SingleTo === true && count($toArr) > 1) { 479 foreach ($toArr as $key => $val) { 480 $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params); 481 } 482 } else { 483 $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params); 484 } 485 } else { 486 if ($this->SingleTo === true && count($toArr) > 1) { 487 foreach ($toArr as $key => $val) { 488 $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params); 489 } 490 } else { 491 $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header); 492 } 493 } 494 495 if (isset($old_from)) { 496 ini_set('sendmail_from', $old_from); 497 } 498 499 if(!$rt) { 500 $this->SetError($this->Lang('instantiate')); 501 return false; 502 } 503 504 return true; 505 } 506 507 /** 508 * Sends mail via SMTP using PhpSMTP (Author: 509 * Chris Ryan). Returns bool. Returns false if there is a 510 * bad MAIL FROM, RCPT, or DATA input. 511 * @access private 512 * @return bool 513 */ 514 function SmtpSend($header, $body) { 515 include_once ('class.mailer-smtp.php'); 516 $error = ''; 517 $bad_rcpt = array(); 518 519 if(!$this->SmtpConnect()) { 520 return false; 521 } 522 523 $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender; 524 if(!$this->smtp->Mail($smtp_from)) { 525 $error = $this->Lang('from_failed') . $smtp_from; 526 $this->SetError($error); 527 $this->smtp->Reset(); 528 return false; 529 } 530 531 /* Attempt to send attach all recipients */ 532 for($i = 0; $i < count($this->to); $i++) { 533 if(!$this->smtp->Recipient($this->to[$i][0])) { 534 $bad_rcpt[] = $this->to[$i][0]; 535 } 536 } 537 for($i = 0; $i < count($this->cc); $i++) { 538 if(!$this->smtp->Recipient($this->cc[$i][0])) { 539 $bad_rcpt[] = $this->cc[$i][0]; 540 } 541 } 542 for($i = 0; $i < count($this->bcc); $i++) { 543 if(!$this->smtp->Recipient($this->bcc[$i][0])) { 544 $bad_rcpt[] = $this->bcc[$i][0]; 545 } 546 } 547 548 if(count($bad_rcpt) > 0) { // Create error message 549 for($i = 0; $i < count($bad_rcpt); $i++) { 550 if($i != 0) { 551 $error .= ', '; 552 } 553 $error .= $bad_rcpt[$i]; 554 } 555 $error = $this->Lang('recipients_failed') . $error; 556 $this->SetError($error); 557 $this->smtp->Reset(); 558 return false; 559 } 560 561 if(!$this->smtp->Data($header . $body)) { 562 $this->SetError($this->Lang('data_not_accepted')); 563 $this->smtp->Reset(); 564 return false; 565 } 566 if($this->SMTPKeepAlive == true) { 567 $this->smtp->Reset(); 568 } else { 569 $this->SmtpClose(); 570 } 571 572 return true; 573 } 574 575 /** 576 * Initiates a connection to an SMTP server. Returns false if the 577 * operation failed. 578 * @access private 579 * @return bool 580 */ 581 function SmtpConnect() { 582 if($this->smtp == NULL) { 583 $this->smtp = new SMTP(); 584 } 585 586 $this->smtp->do_debug = $this->SMTPDebug; 587 $hosts = explode(';', $this->Host); 588 $index = 0; 589 $connection = ($this->smtp->Connected()); 590 591 /* Retry while there is no connection */ 592 while($index < count($hosts) && $connection == false) { 593 $hostinfo = array(); 594 if(eregi('^(.+):([0-9]+)$', $hosts[$index], $hostinfo)) { 595 $host = $hostinfo[1]; 596 $port = $hostinfo[2]; 597 } else { 598 $host = $hosts[$index]; 599 $port = $this->Port; 600 } 601 602 if($this->smtp->Connect(((!empty($this->SMTPSecure))?$this->SMTPSecure.'://':'').$host, $port, $this->Timeout)) { 603 if ($this->Helo != '') { 604 $this->smtp->Hello($this->Helo); 605 } else { 606 $this->smtp->Hello($this->ServerHostname()); 607 } 608 609 $connection = true; 610 if($this->SMTPAuth) { 611 if(!$this->smtp->Authenticate($this->Username, $this->Password)) { 612 $this->SetError($this->Lang('authenticate')); 613 $this->smtp->Reset(); 614 $connection = false; 615 } 616 } 617 } 618 $index++; 619 } 620 if(!$connection) { 621 $this->SetError($this->Lang('connect_host')); 622 } 623 624 return $connection; 625 } 626 627 /** 628 * Closes the active SMTP session if one exists. 629 * @return void 630 */ 631 function SmtpClose() { 632 if($this->smtp != NULL) { 633 if($this->smtp->Connected()) { 634 $this->smtp->Quit(); 635 $this->smtp->Close(); 636 } 637 } 638 } 639 640 /** 641 * Sets the language for all class error messages. Returns false 642 * if it cannot load the language file. The default language type 643 * is English. 644 * @param string $lang_type Type of language (e.g. Portuguese: "br") 645 * @param string $lang_path Path to the language file directory 646 * @access public 647 * @return bool 648 */ 649 function SetLanguage($lang_type, $lang_path = 'language/') { 650 if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php')) { 651 include($lang_path.'phpmailer.lang-'.$lang_type.'.php'); 652 } elseif (file_exists($lang_path.'phpmailer.lang-en.php')) { 653 include($lang_path.'phpmailer.lang-en.php'); 654 } else { 655 $PHPMAILER_LANG = array(); 656 $PHPMAILER_LANG["provide_address"] = 'You must provide at least one ' . 657 $PHPMAILER_LANG["mailer_not_supported"] = ' mailer is not supported.'; 658 $PHPMAILER_LANG["execute"] = 'Could not execute: '; 659 $PHPMAILER_LANG["instantiate"] = 'Could not instantiate mail function.'; 660 $PHPMAILER_LANG["authenticate"] = 'SMTP Error: Could not authenticate.'; 661 $PHPMAILER_LANG["from_failed"] = 'The following From address failed: '; 662 $PHPMAILER_LANG["recipients_failed"] = 'SMTP Error: The following ' . 663 $PHPMAILER_LANG["data_not_accepted"] = 'SMTP Error: Data not accepted.'; 664 $PHPMAILER_LANG["connect_host"] = 'SMTP Error: Could not connect to SMTP host.'; 665 $PHPMAILER_LANG["file_access"] = 'Could not access file: '; 666 $PHPMAILER_LANG["file_open"] = 'File Error: Could not open file: '; 667 $PHPMAILER_LANG["encoding"] = 'Unknown encoding: '; 668 $PHPMAILER_LANG["signing"] = 'Signing Error: '; 669 } 670 $this->language = $PHPMAILER_LANG; 671 672 return true; 673 } 674 675 ///////////////////////////////////////////////// 676 // METHODS, MESSAGE CREATION 677 ///////////////////////////////////////////////// 678 679 /** 680 * Creates recipient headers. 681 * @access private 682 * @return string 683 */ 684 function AddrAppend($type, $addr) { 685 $addr_str = $type . ': '; 686 $addr_str .= $this->AddrFormat($addr[0]); 687 if(count($addr) > 1) { 688 for($i = 1; $i < count($addr); $i++) { 689 $addr_str .= ', ' . $this->AddrFormat($addr[$i]); 690 } 691 } 692 $addr_str .= $this->LE; 693 694 return $addr_str; 695 } 696 697 /** 698 * Formats an address correctly. 699 * @access private 700 * @return string 701 */ 702 function AddrFormat($addr) { 703 if(empty($addr[1])) { 704 $formatted = $this->SecureHeader($addr[0]); 705 } else { 706 $formatted = $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">"; 707 } 708 709 return $formatted; 710 } 711 712 /** 713 * Wraps message for use with mailers that do not 714 * automatically perform wrapping and for quoted-printable. 715 * Original written by philippe. 716 * @access private 717 * @return string 718 */ 719 function WrapText($message, $length, $qp_mode = false) { 720 $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE; 721 // If utf-8 encoding is used, we will need to make sure we don't 722 // split multibyte characters when we wrap 723 $is_utf8 = (strtolower($this->CharSet) == "utf-8"); 724 725 $message = $this->FixEOL($message); 726 if (substr($message, -1) == $this->LE) { 727 $message = substr($message, 0, -1); 728 } 729 730 $line = explode($this->LE, $message); 731 $message = ''; 732 for ($i=0 ;$i < count($line); $i++) { 733 $line_part = explode(' ', $line[$i]); 734 $buf = ''; 735 for ($e = 0; $e<count($line_part); $e++) { 736 $word = $line_part[$e]; 737 if ($qp_mode and (strlen($word) > $length)) { 738 $space_left = $length - strlen($buf) - 1; 739 if ($e != 0) { 740 if ($space_left > 20) { 741 $len = $space_left; 742 if ($is_utf8) { 743 $len = $this->UTF8CharBoundary($word, $len); 744 } elseif (substr($word, $len - 1, 1) == "=") { 745 $len--; 746 } elseif (substr($word, $len - 2, 1) == "=") { 747 $len -= 2; 748 } 749 $part = substr($word, 0, $len); 750 $word = substr($word, $len); 751 $buf .= ' ' . $part; 752 $message .= $buf . sprintf("=%s", $this->LE); 753 } else { 754 $message .= $buf . $soft_break; 755 } 756 $buf = ''; 757 } 758 while (strlen($word) > 0) { 759 $len = $length; 760 if ($is_utf8) { 761 $len = $this->UTF8CharBoundary($word, $len); 762 } elseif (substr($word, $len - 1, 1) == "=") { 763 $len--; 764 } elseif (substr($word, $len - 2, 1) == "=") { 765 $len -= 2; 766 } 767 $part = substr($word, 0, $len); 768 $word = substr($word, $len); 769 770 if (strlen($word) > 0) { 771 $message .= $part . sprintf("=%s", $this->LE); 772 } else { 773 $buf = $part; 774 } 775 } 776 } else { 777 $buf_o = $buf; 778 $buf .= ($e == 0) ? $word : (' ' . $word); 779 780 if (strlen($buf) > $length and $buf_o != '') { 781 $message .= $buf_o . $soft_break; 782 $buf = $word; 783 } 784 } 785 } 786 $message .= $buf . $this->LE; 787 } 788 789 return $message; 790 } 791 792 /** 793 * Finds last character boundary prior to maxLength in a utf-8 794 * quoted (printable) encoded string. 795 * Original written by Colin Brown. 796 * @access private 797 * @param string $encodedText utf-8 QP text 798 * @param int $maxLength find last character boundary prior to this length 799 * @return int 800 */ 801 function UTF8CharBoundary($encodedText, $maxLength) { 802 $foundSplitPos = false; 803 $lookBack = 3; 804 while (!$foundSplitPos) { 805 $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack); 806 $encodedCharPos = strpos($lastChunk, "="); 807 if ($encodedCharPos !== false) { 808 // Found start of encoded character byte within $lookBack block. 809 // Check the encoded byte value (the 2 chars after the '=') 810 $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2); 811 $dec = hexdec($hex); 812 if ($dec < 128) { // Single byte character. 813 // If the encoded char was found at pos 0, it will fit 814 // otherwise reduce maxLength to start of the encoded char 815 $maxLength = ($encodedCharPos == 0) ? $maxLength : 816 $maxLength - ($lookBack - $encodedCharPos); 817 $foundSplitPos = true; 818 } elseif ($dec >= 192) { // First byte of a multi byte character 819 // Reduce maxLength to split at start of character 820 $maxLength = $maxLength - ($lookBack - $encodedCharPos); 821 $foundSplitPos = true; 822 } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back 823 $lookBack += 3; 824 } 825 } else { 826 // No encoded character found 827 $foundSplitPos = true; 828 } 829 } 830 return $maxLength; 831 } 832 833 /** 834 * Set the body wrapping. 835 * @access private 836 * @return void 837 */ 838 function SetWordWrap() { 839 if($this->WordWrap < 1) { 840 return; 841 } 842 843 switch($this->message_type) { 844 case 'alt': 845 /* fall through */ 846 case 'alt_attachments': 847 $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap); 848 break; 849 default: 850 $this->Body = $this->WrapText($this->Body, $this->WordWrap); 851 break; 852 } 853 } 854 855 /** 856 * Assembles message header. 857 * @access private 858 * @return string 859 */ 860 function CreateHeader() { 861 $result = ''; 862 863 /* Set the boundaries */ 864 $uniq_id = md5(uniqid(time())); 865 $this->boundary[1] = 'b1_' . $uniq_id; 866 $this->boundary[2] = 'b2_' . $uniq_id; 867 868 $result .= $this->HeaderLine('Date', $this->RFCDate()); 869 if($this->Sender == '') { 870 $result .= $this->HeaderLine('Return-Path', trim($this->From)); 871 } else { 872 $result .= $this->HeaderLine('Return-Path', trim($this->Sender)); 873 } 874 875 /* To be created automatically by mail() */ 876 if($this->Mailer != 'mail') { 877 if(count($this->to) > 0) { 878 $result .= $this->AddrAppend('To', $this->to); 879 } elseif (count($this->cc) == 0) { 880 $result .= $this->HeaderLine('To', 'undisclosed-recipients:;'); 881 } 882 } 883 884 $from = array(); 885 $from[0][0] = trim($this->From); 886 $from[0][1] = $this->FromName; 887 $result .= $this->AddrAppend('From', $from); 888 889 /* sendmail and mail() extract Cc from the header before sending */ 890 if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->cc) > 0)) { 891 $result .= $this->AddrAppend('Cc', $this->cc); 892 } 893 894 /* sendmail and mail() extract Bcc from the header before sending */ 895 if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) { 896 $result .= $this->AddrAppend('Bcc', $this->bcc); 897 } 898 899 if(count($this->ReplyTo) > 0) { 900 $result .= $this->AddrAppend('Reply-To', $this->ReplyTo); 901 } 902 903 /* mail() sets the subject itself */ 904 if($this->Mailer != 'mail') { 905 $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject))); 906 } 907 908 if($this->MessageID != '') { 909 $result .= $this->HeaderLine('Message-ID',$this->MessageID); 910 } else { 911 $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE); 912 } 913 $result .= $this->HeaderLine('X-Priority', $this->Priority); 914 $result .= $this->HeaderLine('X-Mailer', 'PHPMailer (phpmailer.sourceforge.net) [version ' . $this->Version . ']'); 915 916 if($this->ConfirmReadingTo != '') { 917 $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>'); 918 } 919 920 // Add custom headers 921 for($index = 0; $index < count($this->CustomHeader); $index++) { 922 $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1]))); 923 } 924 if (!$this->sign_key_file) { 925 $result .= $this->HeaderLine('MIME-Version', '1.0'); 926 $result .= $this->GetMailMIME(); 927 } 928 929 return $result; 930 } 931 932 /** 933 * Returns the message MIME. 934 * @access private 935 * @return string 936 */ 937 function GetMailMIME() { 938 $result = ''; 939 switch($this->message_type) { 940 case 'plain': 941 $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding); 942 $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet); 943 break; 944 case 'attachments': 945 /* fall through */ 946 case 'alt_attachments': 947 if($this->InlineImageExists()){ 948 $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE); 949 } else { 950 $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;'); 951 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"'); 952 } 953 break; 954 case 'alt': 955 $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;'); 956 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"'); 957 break; 958 } 959 960 if($this->Mailer != 'mail') { 961 $result .= $this->LE.$this->LE; 962 } 963 964 return $result; 965 } 966 967 /** 968 * Assembles the message body. Returns an empty string on failure. 969 * @access private 970 * @return string 971 */ 972 function CreateBody() { 973 $result = ''; 974 if ($this->sign_key_file) { 975 $result .= $this->GetMailMIME(); 976 } 977 978 $this->SetWordWrap(); 979 980 switch($this->message_type) { 981 case 'alt': 982 $result .= $this->GetBoundary($this->boundary[1], '', 'text/plain', ''); 983 $result .= $this->EncodeString($this->AltBody, $this->Encoding); 984 $result .= $this->LE.$this->LE; 985 $result .= $this->GetBoundary($this->boundary[1], '', 'text/html', ''); 986 $result .= $this->EncodeString($this->Body, $this->Encoding); 987 $result .= $this->LE.$this->LE; 988 $result .= $this->EndBoundary($this->boundary[1]); 989 break; 990 case 'plain': 991 $result .= $this->EncodeString($this->Body, $this->Encoding); 992 break; 993 case 'attachments': 994 $result .= $this->GetBoundary($this->boundary[1], '', '', ''); 995 $result .= $this->EncodeString($this->Body, $this->Encoding); 996 $result .= $this->LE; 997 $result .= $this->AttachAll(); 998 break; 999 case 'alt_attachments': 1000 $result .= sprintf("--%s%s", $this->boundary[1], $this->LE); 1001 $result .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE); 1002 $result .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body 1003 $result .= $this->EncodeString($this->AltBody, $this->Encoding); 1004 $result .= $this->LE.$this->LE; 1005 $result .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body 1006 $result .= $this->EncodeString($this->Body, $this->Encoding); 1007 $result .= $this->LE.$this->LE; 1008 $result .= $this->EndBoundary($this->boundary[2]); 1009 $result .= $this->AttachAll(); 1010 break; 1011 } 1012 1013 if($this->IsError()) { 1014 $result = ''; 1015 } else if ($this->sign_key_file) { 1016 $file = tempnam("", "mail"); 1017 $fp = fopen($file, "w"); 1018 fwrite($fp, $result); 1019 fclose($fp); 1020 $signed = tempnam("", "signed"); 1021 1022 if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), null)) { 1023 $fp = fopen($signed, "r"); 1024 $result = fread($fp, filesize($this->sign_key_file)); 1025 $result = ''; 1026 while(!feof($fp)){ 1027 $result = $result . fread($fp, 1024); 1028 } 1029 fclose($fp); 1030 } else { 1031 $this->SetError($this->Lang("signing").openssl_error_string()); 1032 $result = ''; 1033 } 1034 1035 unlink($file); 1036 unlink($signed); 1037 } 1038 1039 return $result; 1040 } 1041 1042 /** 1043 * Returns the start of a message boundary. 1044 * @access private 1045 */ 1046 function GetBoundary($boundary, $charSet, $contentType, $encoding) { 1047 $result = ''; 1048 if($charSet == '') { 1049 $charSet = $this->CharSet; 1050 } 1051 if($contentType == '') { 1052 $contentType = $this->ContentType; 1053 } 1054 if($encoding == '') { 1055 $encoding = $this->Encoding; 1056 } 1057 $result .= $this->TextLine('--' . $boundary); 1058 $result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet); 1059 $result .= $this->LE; 1060 $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding); 1061 $result .= $this->LE; 1062 1063 return $result; 1064 } 1065 1066 /** 1067 * Returns the end of a message boundary. 1068 * @access private 1069 */ 1070 function EndBoundary($boundary) { 1071 return $this->LE . '--' . $boundary . '--' . $this->LE; 1072 } 1073 1074 /** 1075 * Sets the message type. 1076 * @access private 1077 * @return void 1078 */ 1079 function SetMessageType() { 1080 if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) { 1081 $this->message_type = 'plain'; 1082 } else { 1083 if(count($this->attachment) > 0) { 1084 $this->message_type = 'attachments'; 1085 } 1086 if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) { 1087 $this->message_type = 'alt'; 1088 } 1089 if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) { 1090 $this->message_type = 'alt_attachments'; 1091 } 1092 } 1093 } 1094 1095 /* Returns a formatted header line. 1096 * @access private 1097 * @return string 1098 */ 1099 function HeaderLine($name, $value) { 1100 return $name . ': ' . $value . $this->LE; 1101 } 1102 1103 /** 1104 * Returns a formatted mail line. 1105 * @access private 1106 * @return string 1107 */ 1108 function TextLine($value) { 1109 return $value . $this->LE; 1110 } 1111 1112 ///////////////////////////////////////////////// 1113 // CLASS METHODS, ATTACHMENTS 1114 ///////////////////////////////////////////////// 1115 1116 /** 1117 * Adds an attachment from a path on the filesystem. 1118 * Returns false if the file could not be found 1119 * or accessed. 1120 * @param string $path Path to the attachment. 1121 * @param string $name Overrides the attachment name. 1122 * @param string $encoding File encoding (see $Encoding). 1123 * @param string $type File extension (MIME) type. 1124 * @return bool 1125 */ 1126 function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') { 1127 if(!@is_file($path)) { 1128 $this->SetError($this->Lang('file_access') . $path); 1129 return false; 1130 } 1131 1132 $filename = basename($path); 1133 if($name == '') { 1134 $name = $filename; 1135 } 1136 1137 $cur = count($this->attachment); 1138 $this->attachment[$cur][0] = $path; 1139 $this->attachment[$cur][1] = $filename; 1140 $this->attachment[$cur][2] = $name; 1141 $this->attachment[$cur][3] = $encoding; 1142 $this->attachment[$cur][4] = $type; 1143 $this->attachment[$cur][5] = false; // isStringAttachment 1144 $this->attachment[$cur][6] = 'attachment'; 1145 $this->attachment[$cur][7] = 0; 1146 1147 return true; 1148 } 1149 1150 /** 1151 * Attaches all fs, string, and binary attachments to the message. 1152 * Returns an empty string on failure. 1153 * @access private 1154 * @return string 1155 */ 1156 function AttachAll() { 1157 /* Return text of body */ 1158 $mime = array(); 1159 1160 /* Add all attachments */ 1161 for($i = 0; $i < count($this->attachment); $i++) { 1162 /* Check for string attachment */ 1163 $bString = $this->attachment[$i][5]; 1164 if ($bString) { 1165 $string = $this->attachment[$i][0]; 1166 } else { 1167 $path = $this->attachment[$i][0]; 1168 } 1169 1170 $filename = $this->attachment[$i][1]; 1171 $name = $this->attachment[$i][2]; 1172 $encoding = $this->attachment[$i][3]; 1173 $type = $this->attachment[$i][4]; 1174 $disposition = $this->attachment[$i][6]; 1175 $cid = $this->attachment[$i][7]; 1176 1177 $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE); 1178 $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE); 1179 $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE); 1180 1181 if($disposition == 'inline') { 1182 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE); 1183 } 1184 1185 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE); 1186 1187 /* Encode as string attachment */ 1188 if($bString) { 1189 $mime[] = $this->EncodeString($string, $encoding); 1190 if($this->IsError()) { 1191 return ''; 1192 } 1193 $mime[] = $this->LE.$this->LE; 1194 } else { 1195 $mime[] = $this->EncodeFile($path, $encoding); 1196 if($this->IsError()) { 1197 return ''; 1198 } 1199 $mime[] = $this->LE.$this->LE; 1200 } 1201 } 1202 1203 $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE); 1204 1205 return join('', $mime); 1206 } 1207 1208 /** 1209 * Encodes attachment in requested format. Returns an 1210 * empty string on failure. 1211 * @access private 1212 * @return string 1213 */ 1214 function EncodeFile ($path, $encoding = 'base64') { 1215 if(!@$fd = fopen($path, 'rb')) { 1216 $this->SetError($this->Lang('file_open') . $path); 1217 return ''; 1218 } 1219 $magic_quotes = get_magic_quotes_runtime(); 1220 set_magic_quotes_runtime(0); 1221 $file_buffer = fread($fd, filesize($path)); 1222 $file_buffer = $this->EncodeString($file_buffer, $encoding); 1223 fclose($fd); 1224 set_magic_quotes_runtime($magic_quotes); 1225 1226 return $file_buffer; 1227 } 1228 1229 /** 1230 * Encodes string to requested format. Returns an 1231 * empty string on failure. 1232 * @access private 1233 * @return string 1234 */ 1235 function EncodeString ($str, $encoding = 'base64') { 1236 $encoded = ''; 1237 switch(strtolower($encoding)) { 1238 case 'base64': 1239 /* chunk_split is found in PHP >= 3.0.6 */ 1240 $encoded = chunk_split(base64_encode($str), 76, $this->LE); 1241 break; 1242 case '7bit': 1243 case '8bit': 1244 $encoded = $this->FixEOL($str); 1245 if (substr($encoded, -(strlen($this->LE))) != $this->LE) 1246 $encoded .= $this->LE; 1247 break; 1248 case 'binary': 1249 $encoded = $str; 1250 break; 1251 case 'quoted-printable': 1252 $encoded = $this->EncodeQP($str); 1253 break; 1254 default: 1255 $this->SetError($this->Lang('encoding') . $encoding); 1256 break; 1257 } 1258 return $encoded; 1259 } 1260 1261 /** 1262 * Encode a header string to best of Q, B, quoted or none. 1263 * @access private 1264 * @return string 1265 */ 1266 function EncodeHeader ($str, $position = 'text') { 1267 $x = 0; 1268 1269 switch (strtolower($position)) { 1270 case 'phrase': 1271 if (!preg_match('/[\200-\377]/', $str)) { 1272 /* Can't use addslashes as we don't know what value has magic_quotes_sybase. */ 1273 $encoded = addcslashes($str, "\0..\37\177\\\""); 1274 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) { 1275 return ($encoded); 1276 } else { 1277 return ("\"$encoded\""); 1278 } 1279 } 1280 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches); 1281 break; 1282 case 'comment': 1283 $x = preg_match_all('/[()"]/', $str, $matches); 1284 /* Fall-through */ 1285 case 'text': 1286 default: 1287 $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches); 1288 break; 1289 } 1290 1291 if ($x == 0) { 1292 return ($str); 1293 } 1294 1295 $maxlen = 75 - 7 - strlen($this->CharSet); 1296 /* Try to select the encoding which should produce the shortest output */ 1297 if (strlen($str)/3 < $x) { 1298 $encoding = 'B'; 1299 if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) { 1300 // Use a custom function which correctly encodes and wraps long 1301 // multibyte strings without breaking lines within a character 1302 $encoded = $this->Base64EncodeWrapMB($str); 1303 } else { 1304 $encoded = base64_encode($str); 1305 $maxlen -= $maxlen % 4; 1306 $encoded = trim(chunk_split($encoded, $maxlen, "\n")); 1307 } 1308 } else { 1309 $encoding = 'Q'; 1310 $encoded = $this->EncodeQ($str, $position); 1311 $encoded = $this->WrapText($encoded, $maxlen, true); 1312 $encoded = str_replace('='.$this->LE, "\n", trim($encoded)); 1313 } 1314 1315 $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded); 1316 $encoded = trim(str_replace("\n", $this->LE, $encoded)); 1317 1318 return $encoded; 1319 } 1320 1321 /** 1322 * Checks if a string contains multibyte characters. 1323 * @access private 1324 * @param string $str multi-byte text to wrap encode 1325 * @return bool 1326 */ 1327 function HasMultiBytes($str) { 1328 if (function_exists('mb_strlen')) { 1329 return (strlen($str) > mb_strlen($str, $this->CharSet)); 1330 } else { // Assume no multibytes (we can't handle without mbstring functions anyway) 1331 return False; 1332 } 1333 } 1334 1335 /** 1336 * Correctly encodes and wraps long multibyte strings for mail headers 1337 * without breaking lines within a character. 1338 * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php 1339 * @access private 1340 * @param string $str multi-byte text to wrap encode 1341 * @return string 1342 */ 1343 function Base64EncodeWrapMB($str) { 1344 $start = "=?".$this->CharSet."?B?"; 1345 $end = "?="; 1346 $encoded = ""; 1347 1348 $mb_length = mb_strlen($str, $this->CharSet); 1349 // Each line must have length <= 75, including $start and $end 1350 $length = 75 - strlen($start) - strlen($end); 1351 // Average multi-byte ratio 1352 $ratio = $mb_length / strlen($str); 1353 // Base64 has a 4:3 ratio 1354 $offset = $avgLength = floor($length * $ratio * .75); 1355 1356 for ($i = 0; $i < $mb_length; $i += $offset) { 1357 $lookBack = 0; 1358 1359 do { 1360 $offset = $avgLength - $lookBack; 1361 $chunk = mb_substr($str, $i, $offset, $this->CharSet); 1362 $chunk = base64_encode($chunk); 1363 $lookBack++; 1364 } 1365 while (strlen($chunk) > $length); 1366 1367 $encoded .= $chunk . $this->LE; 1368 } 1369 1370 // Chomp the last linefeed 1371 $encoded = substr($encoded, 0, -strlen($this->LE)); 1372 return $encoded; 1373 } 1374 1375 /** 1376 * Encode string to quoted-printable. 1377 * @access private 1378 * @return string 1379 */ 1380 function EncodeQP( $input = '', $line_max = 76, $space_conv = false ) { 1381 $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); 1382 $lines = preg_split('/(?:\r\n|\r|\n)/', $input); 1383 $eol = "\r\n"; 1384 $escape = '='; 1385 $output = ''; 1386 while( list(, $line) = each($lines) ) { 1387 $linlen = strlen($line); 1388 $newline = ''; 1389 for($i = 0; $i < $linlen; $i++) { 1390 $c = substr( $line, $i, 1 ); 1391 $dec = ord( $c ); 1392 if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E 1393 $c = '=2E'; 1394 } 1395 if ( $dec == 32 ) { 1396 if ( $i == ( $linlen - 1 ) ) { // convert space at eol only 1397 $c = '=20'; 1398 } else if ( $space_conv ) { 1399 $c = '=20'; 1400 } 1401 } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required 1402 $h2 = floor($dec/16); 1403 $h1 = floor($dec%16); 1404 $c = $escape.$hex[$h2].$hex[$h1]; 1405 } 1406 if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted 1407 $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay 1408 $newline = ''; 1409 // check if newline first character will be point or not 1410 if ( $dec == 46 ) { 1411 $c = '=2E'; 1412 } 1413 } 1414 $newline .= $c; 1415 } // end of for 1416 $output .= $newline.$eol; 1417 } // end of while 1418 return $output; 1419 } 1420 1421 /** 1422 * Callback for converting to "=XX". 1423 * @access private 1424 * @return string 1425 */ 1426 function EncodeQ_callback ($matches) { 1427 return sprintf('=%02X', ord($matches[1])); 1428 } 1429 1430 /** 1431 * Encode string to q encoding. 1432 * @access private 1433 * @return string 1434 */ 1435 function EncodeQ ($str, $position = 'text') { 1436 /* There should not be any EOL in the string */ 1437 $encoded = preg_replace("/[\r\n]/", '', $str); 1438 1439 switch (strtolower($position)) { 1440 case 'phrase': 1441 $encoded = preg_replace_callback("/([^A-Za-z0-9!*+\/ -])/", 1442 array('PHPMailer', 'EncodeQ_callback'), $encoded); 1443 break; 1444 case 'comment': 1445 $encoded = preg_replace_callback("/([\(\)\"])/", 1446 array('PHPMailer', 'EncodeQ_callback'), $encoded); 1447 break; 1448 case 'text': 1449 default: 1450 /* Replace every high ascii, control =, ? and _ characters */ 1451 $encoded = preg_replace_callback('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/', 1452 array('PHPMailer', 'EncodeQ_callback'), $encoded); 1453 break; 1454 } 1455 1456 /* Replace every spaces to _ (more readable than =20) */ 1457 $encoded = str_replace(' ', '_', $encoded); 1458 1459 return $encoded; 1460 } 1461 1462 /** 1463 * Adds a string or binary attachment (non-filesystem) to the list. 1464 * This method can be used to attach ascii or binary data, 1465 * such as a BLOB record from a database. 1466 * @param string $string String attachment data. 1467 * @param string $filename Name of the attachment. 1468 * @param string $encoding File encoding (see $Encoding). 1469 * @param string $type File extension (MIME) type. 1470 * @return void 1471 */ 1472 function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') { 1473 /* Append to $attachment array */ 1474 $cur = count($this->attachment); 1475 $this->attachment[$cur][0] = $string; 1476 $this->attachment[$cur][1] = $filename; 1477 $this->attachment[$cur][2] = $filename; 1478 $this->attachment[$cur][3] = $encoding; 1479 $this->attachment[$cur][4] = $type; 1480 $this->attachment[$cur][5] = true; // isString 1481 $this->attachment[$cur][6] = 'attachment'; 1482 $this->attachment[$cur][7] = 0; 1483 } 1484 1485 /** 1486 * Adds an embedded attachment. This can include images, sounds, and 1487 * just about any other document. Make sure to set the $type to an 1488 * image type. For JPEG images use "image/jpeg" and for GIF images 1489 * use "image/gif". 1490 * @param string $path Path to the attachment. 1491 * @param string $cid Content ID of the attachment. Use this to identify 1492 * the Id for accessing the image in an HTML form. 1493 * @param string $name Overrides the attachment name. 1494 * @param string $encoding File encoding (see $Encoding). 1495 * @param string $type File extension (MIME) type. 1496 * @return bool 1497 */ 1498 function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') { 1499 1500 if(!@is_file($path)) { 1501 $this->SetError($this->Lang('file_access') . $path); 1502 return false; 1503 } 1504 1505 $filename = basename($path); 1506 if($name == '') { 1507 $name = $filename; 1508 } 1509 1510 /* Append to $attachment array */ 1511 $cur = count($this->attachment); 1512 $this->attachment[$cur][0] = $path; 1513 $this->attachment[$cur][1] = $filename; 1514 $this->attachment[$cur][2] = $name; 1515 $this->attachment[$cur][3] = $encoding; 1516 $this->attachment[$cur][4] = $type; 1517 $this->attachment[$cur][5] = false; 1518 $this->attachment[$cur][6] = 'inline'; 1519 $this->attachment[$cur][7] = $cid; 1520 1521 return true; 1522 } 1523 1524 /** 1525 * Returns true if an inline attachment is present. 1526 * @access private 1527 * @return bool 1528 */ 1529 function InlineImageExists() { 1530 $result = false; 1531 for($i = 0; $i < count($this->attachment); $i++) { 1532 if($this->attachment[$i][6] == 'inline') { 1533 $result = true; 1534 break; 1535 } 1536 } 1537 1538 return $result; 1539 } 1540 1541 ///////////////////////////////////////////////// 1542 // CLASS METHODS, MESSAGE RESET 1543 ///////////////////////////////////////////////// 1544 1545 /** 1546 * Clears all recipients assigned in the TO array. Returns void. 1547 * @return void 1548 */ 1549 function ClearAddresses() { 1550 $this->to = array(); 1551 } 1552 1553 /** 1554 * Clears all recipients assigned in the CC array. Returns void. 1555 * @return void 1556 */ 1557 function ClearCCs() { 1558 $this->cc = array(); 1559 } 1560 1561 /** 1562 * Clears all recipients assigned in the BCC array. Returns void. 1563 * @return void 1564 */ 1565 function ClearBCCs() { 1566 $this->bcc = array(); 1567 } 1568 1569 /** 1570 * Clears all recipients assigned in the ReplyTo array. Returns void. 1571 * @return void 1572 */ 1573 function ClearReplyTos() { 1574 $this->ReplyTo = array(); 1575 } 1576 1577 /** 1578 * Clears all recipients assigned in the TO, CC and BCC 1579 * array. Returns void. 1580 * @return void 1581 */ 1582 function ClearAllRecipients() { 1583 $this->to = array(); 1584 $this->cc = array(); 1585 $this->bcc = array(); 1586 } 1587 1588 /** 1589 * Clears all previously set filesystem, string, and binary 1590 * attachments. Returns void. 1591 * @return void 1592 */ 1593 function ClearAttachments() { 1594 $this->attachment = array(); 1595 } 1596 1597 /** 1598 * Clears all custom headers. Returns void. 1599 * @return void 1600 */ 1601 function ClearCustomHeaders() { 1602 $this->CustomHeader = array(); 1603 } 1604 1605 ///////////////////////////////////////////////// 1606 // CLASS METHODS, MISCELLANEOUS 1607 ///////////////////////////////////////////////// 1608 1609 /** 1610 * Adds the error message to the error container. 1611 * Returns void. 1612 * @access private 1613 * @return void 1614 */ 1615 function SetError($msg) { 1616 $this->error_count++; 1617 $this->ErrorInfo = $msg; 1618 } 1619 1620 /** 1621 * Returns the proper RFC 822 formatted date. 1622 * @access private 1623 * @return string 1624 */ 1625 function RFCDate() { 1626 $tz = date('Z'); 1627 $tzs = ($tz < 0) ? '-' : '+'; 1628 $tz = abs($tz); 1629 $tz = (int)($tz/3600)*100 + ($tz%3600)/60; 1630 $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz); 1631 1632 return $result; 1633 } 1634 1635 /** 1636 * Returns the appropriate server variable. Should work with both 1637 * PHP 4.1.0+ as well as older versions. Returns an empty string 1638 * if nothing is found. 1639 * @access private 1640 * @return mixed 1641 */ 1642 function ServerVar($varName) { 1643 global $HTTP_SERVER_VARS; 1644 global $HTTP_ENV_VARS; 1645 1646 if(!isset($_SERVER)) { 1647 $_SERVER = $HTTP_SERVER_VARS; 1648 if(!isset($_SERVER['REMOTE_ADDR'])) { 1649 $_SERVER = $HTTP_ENV_VARS; // must be Apache 1650 } 1651 } 1652 1653 if(isset($_SERVER[$varName])) { 1654 return $_SERVER[$varName]; 1655 } else { 1656 return ''; 1657 } 1658 } 1659 1660 /** 1661 * Returns the server hostname or 'localhost.localdomain' if unknown. 1662 * @access private 1663 * @return string 1664 */ 1665 function ServerHostname() { 1666 if ($this->Hostname != '') { 1667 $result = $this->Hostname; 1668 } elseif ($this->ServerVar('SERVER_NAME') != '') { 1669 $result = $this->ServerVar('SERVER_NAME'); 1670 } else { 1671 $result = 'localhost.localdomain'; 1672 } 1673 1674 return $result; 1675 } 1676 1677 /** 1678 * Returns a message in the appropriate language. 1679 * @access private 1680 * @return string 1681 */ 1682 function Lang($key) { 1683 if(count($this->language) < 1) { 1684 $this->SetLanguage('en'); // set the default language 1685 } 1686 1687 if(isset($this->language[$key])) { 1688 return $this->language[$key]; 1689 } else { 1690 return 'Language string failed to load: ' . $key; 1691 } 1692 } 1693 1694 /** 1695 * Returns true if an error occurred. 1696 * @return bool 1697 */ 1698 function IsError() { 1699 return ($this->error_count > 0); 1700 } 1701 1702 /** 1703 * Changes every end of line from CR or LF to CRLF. 1704 * @access private 1705 * @return string 1706 */ 1707 function FixEOL($str) { 1708 $str = str_replace("\r\n", "\n", $str); 1709 $str = str_replace("\r", "\n", $str); 1710 $str = str_replace("\n", $this->LE, $str); 1711 return $str; 1712 } 1713 1714 /** 1715 * Adds a custom header. 1716 * @return void 1717 */ 1718 function AddCustomHeader($custom_header) { 1719 $this->CustomHeader[] = explode(':', $custom_header, 2); 1720 } 1721 1722 /** 1723 * Evaluates the message and returns modifications for inline images and backgrounds 1724 * @access public 1725 * @return $message 1726 */ 1727 function MsgHTML($message,$basedir='') { 1728 preg_match_all("/(src|background)=\"(.*)\"/Ui", $message, $images); 1729 if(isset($images[2])) { 1730 foreach($images[2] as $i => $url) { 1731 // do not change urls for absolute images (thanks to corvuscorax) 1732 if (!preg_match('/^[A-z][A-z]*:\/\//',$url)) { 1733 $filename = basename($url); 1734 $directory = dirname($url); 1735 ($directory == '.')?$directory='':''; 1736 $cid = 'cid:' . md5($filename); 1737 $fileParts = split("\.", $filename); 1738 $ext = $fileParts[1]; 1739 $mimeType = $this->_mime_types($ext); 1740 if ( strlen($basedir) > 1 && substr($basedir,-1) != '/') { $basedir .= '/'; } 1741 if ( strlen($directory) > 1 && substr($directory,-1) != '/') { $directory .= '/'; } 1742 if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64',$mimeType) ) { 1743 $message = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, '/')."\"/Ui", $images[1][$i]."=\"".$cid."\"", $message); 1744 } 1745 } 1746 } 1747 } 1748 $this->IsHTML(true); 1749 $this->Body = $message; 1750 $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message))); 1751 if ( !empty($textMsg) && empty($this->AltBody) ) { 1752 $this->AltBody = html_entity_decode($textMsg); 1753 } 1754 if ( empty($this->AltBody) ) { 1755 $this->AltBody = 'To view this email message, open the email in with HTML compatibility!' . "\n\n"; 1756 } 1757 } 1758 1759 /** 1760 * Gets the mime type of the embedded or inline image 1761 * @access private 1762 * @return mime type of ext 1763 */ 1764 function _mime_types($ext = '') { 1765 $mimes = array( 1766 'ai' => 'application/postscript', 1767 'aif' => 'audio/x-aiff', 1768 'aifc' => 'audio/x-aiff', 1769 'aiff' => 'audio/x-aiff', 1770 'avi' => 'video/x-msvideo', 1771 'bin' => 'application/macbinary', 1772 'bmp' => 'image/bmp', 1773 'class' => 'application/octet-stream', 1774 'cpt' => 'application/mac-compactpro', 1775 'css' => 'text/css', 1776 'dcr' => 'application/x-director', 1777 'dir' => 'application/x-director', 1778 'dll' => 'application/octet-stream', 1779 'dms' => 'application/octet-stream', 1780 'doc' => 'application/msword', 1781 'dvi' => 'application/x-dvi', 1782 'dxr' => 'application/x-director', 1783 'eml' => 'message/rfc822', 1784 'eps' => 'application/postscript', 1785 'exe' => 'application/octet-stream', 1786 'gif' => 'image/gif', 1787 'gtar' => 'application/x-gtar', 1788 'htm' => 'text/html', 1789 'html' => 'text/html', 1790 'jpe' => 'image/jpeg', 1791 'jpeg' => 'image/jpeg', 1792 'jpg' => 'image/jpeg', 1793 'hqx' => 'application/mac-binhex40', 1794 'js' => 'application/x-javascript', 1795 'lha' => 'application/octet-stream', 1796 'log' => 'text/plain', 1797 'lzh' => 'application/octet-stream', 1798 'mid' => 'audio/midi', 1799 'midi' => 'audio/midi', 1800 'mif' => 'application/vnd.mif', 1801 'mov' => 'video/quicktime', 1802 'movie' => 'video/x-sgi-movie', 1803 'mp2' => 'audio/mpeg', 1804 'mp3' => 'audio/mpeg', 1805 'mpe' => 'video/mpeg', 1806 'mpeg' => 'video/mpeg', 1807 'mpg' => 'video/mpeg', 1808 'mpga' => 'audio/mpeg', 1809 'oda' => 'application/oda', 1810 'pdf' => 'application/pdf', 1811 'php' => 'application/x-httpd-php', 1812 'php3' => 'application/x-httpd-php', 1813 'php4' => 'application/x-httpd-php', 1814 'phps' => 'application/x-httpd-php-source', 1815 'phtml' => 'application/x-httpd-php', 1816 'png' => 'image/png', 1817 'ppt' => 'application/vnd.ms-powerpoint', 1818 'ps' => 'application/postscript', 1819 'psd' => 'application/octet-stream', 1820 'qt' => 'video/quicktime', 1821 'ra' => 'audio/x-realaudio', 1822 'ram' => 'audio/x-pn-realaudio', 1823 'rm' => 'audio/x-pn-realaudio', 1824 'rpm' => 'audio/x-pn-realaudio-plugin', 1825 'rtf' => 'text/rtf', 1826 'rtx' => 'text/richtext', 1827 'rv' => 'video/vnd.rn-realvideo', 1828 'sea' => 'application/octet-stream', 1829 'shtml' => 'text/html', 1830 'sit' => 'application/x-stuffit', 1831 'so' => 'application/octet-stream', 1832 'smi' => 'application/smil', 1833 'smil' => 'application/smil', 1834 'swf' => 'application/x-shockwave-flash', 1835 'tar' => 'application/x-tar', 1836 'text' => 'text/plain', 1837 'txt' => 'text/plain', 1838 'tgz' => 'application/x-tar', 1839 'tif' => 'image/tiff', 1840 'tiff' => 'image/tiff', 1841 'wav' => 'audio/x-wav', 1842 'wbxml' => 'application/vnd.wap.wbxml', 1843 'wmlc' => 'application/vnd.wap.wmlc', 1844 'word' => 'application/msword', 1845 'xht' => 'application/xhtml+xml', 1846 'xhtml' => 'application/xhtml+xml', 1847 'xl' => 'application/excel', 1848 'xls' => 'application/vnd.ms-excel', 1849 'xml' => 'text/xml', 1850 'xsl' => 'text/xml', 1851 'zip' => 'application/zip' 1852 ); 1853 return ( ! isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)]; 1854 } 1855 1856 /** 1857 * Set (or reset) Class Objects (variables) 1858 * 1859 * Usage Example: 1860 * $page->set('X-Priority', '3'); 1861 * 1862 * @access public 1863 * @param string $name Parameter Name 1864 * @param mixed $value Parameter Value 1865 * NOTE: will not work with arrays, there are no arrays to set/reset 1866 */ 1867 function set ( $name, $value = '' ) { 1868 if ( isset($this->$name) ) { 1869 $this->$name = $value; 1870 } else { 1871 $this->SetError('Cannot set or reset variable ' . $name); 1872 return false; 1873 } 1874 } 1875 1876 /** 1877 * Read a file from a supplied filename and return it. 1878 * 1879 * @access public 1880 * @param string $filename Parameter File Name 1881 */ 1882 function getFile($filename) { 1883 $return = ''; 1884 if ($fp = fopen($filename, 'rb')) { 1885 while (!feof($fp)) { 1886 $return .= fread($fp, 1024); 1887 } 1888 fclose($fp); 1889 return $return; 1890 } else { 1891 return false; 1892 } 1893 } 1894 1895 /** 1896 * Strips newlines to prevent header injection. 1897 * @access private 1898 * @param string $str String 1899 * @return string 1900 */ 1901 function SecureHeader($str) { 1902 $str = trim($str); 1903 $str = str_replace("\r", "", $str); 1904 $str = str_replace("\n", "", $str); 1905 return $str; 1906 } 1907 1908 /** 1909 * Set the private key file and password to sign the message. 1910 * 1911 * @access public 1912 * @param string $key_filename Parameter File Name 1913 * @param string $key_pass Password for private key 1914 */ 1915 function Sign($cert_filename, $key_filename, $key_pass) { 1916 $this->sign_cert_file = $cert_filename; 1917 $this->sign_key_file = $key_filename; 1918 $this->sign_key_pass = $key_pass; 1919 } 1920 1921 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Jun 20 03:58:50 2013 | Hosted by follow the white rabbit. |