[ Index ] |
|
Code source de Zen Cart E-Commerce Shopping Cart 1.3.7.1 |
1 <?php 2 //////////////////////////////////////////////////// 3 // PHPMailer - PHP email class 4 // 5 // Class for sending email using either 6 // sendmail, PHP mail(), or SMTP. Methods are 7 // based upon the standard AspEmail(tm) classes. 8 // 9 // Copyright (C) 2001 - 2003 Brent R. Matzelle 10 // 11 // License: LGPL, see LICENSE 12 //////////////////////////////////////////////////// 13 14 /** 15 * PHPMailer - PHP email transport class 16 * 17 * @package classes 18 * @author Brent R. Matzelle 19 * @copyright 2001 - 2003 Brent R. Matzelle 20 * @version (within Zen Cart) $Id: class.phpmailer.php 6532 2007-06-26 00:55:57Z drbyte $ 21 */ 22 /** 23 * PHPMailer - PHP email transport class 24 * 25 * @package classes 26 */ 27 class PHPMailer 28 { 29 ///////////////////////////////////////////////// 30 // PUBLIC VARIABLES 31 ///////////////////////////////////////////////// 32 33 /** 34 * Email priority (1 = High, 3 = Normal, 5 = low). 35 * @var int 36 */ 37 var $Priority = 3; 38 39 /** 40 * Sets the CharSet of the message. 41 * @var string 42 */ 43 var $CharSet = "iso-8859-1"; 44 45 /** 46 * Sets the Content-type of the message. 47 * @var string 48 */ 49 var $ContentType = "text/plain"; 50 51 /** 52 * Sets the Encoding of the message. Options for this are "8bit", 53 * "7bit", "binary", "base64", and "quoted-printable". 54 * @var string 55 */ 56 var $Encoding = "8bit"; 57 58 /** 59 * Holds the most recent mailer error message. 60 * @var string 61 */ 62 var $ErrorInfo = ""; 63 64 /** 65 * Sets the From email address for the message. 66 * @var string 67 */ 68 var $From = "root@localhost"; 69 70 /** 71 * Sets the From name of the message. 72 * @var string 73 */ 74 var $FromName = "Root User"; 75 76 /** 77 * Sets the Sender email (Return-Path) of the message. If not empty, 78 * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode. 79 * @var string 80 */ 81 var $Sender = ""; 82 83 /** 84 * Sets the Subject of the message. 85 * @var string 86 */ 87 var $Subject = ""; 88 89 /** 90 * Sets the Body of the message. This can be either an HTML or text body. 91 * If HTML then run IsHTML(true). 92 * @var string 93 */ 94 var $Body = ""; 95 96 /** 97 * Sets the text-only body of the message. This automatically sets the 98 * email to multipart/alternative. This body can be read by mail 99 * clients that do not have HTML email capability such as mutt. Clients 100 * that can read HTML will view the normal Body. 101 * @var string 102 */ 103 var $AltBody = ""; 104 105 /** 106 * Sets word wrapping on the body of the message to a given number of 107 * characters. 108 * @var int 109 */ 110 var $WordWrap = 0; 111 112 /** 113 * Method to send mail: ("mail", "sendmail", or "smtp"). 114 * @var string 115 */ 116 var $Mailer = "mail"; 117 118 /** 119 * Sets the path of the sendmail program. 120 * @var string 121 */ 122 var $Sendmail = "/usr/sbin/sendmail"; 123 124 /** 125 * Path to PHPMailer plugins. This is now only useful if the SMTP class 126 * is in a different directory than the PHP include path. 127 * @var string 128 */ 129 var $PluginDir = ""; 130 131 /** 132 * Holds PHPMailer version. 133 * @var string 134 */ 135 var $Version = "1.73"; 136 137 /** 138 * Sets the email address that a reading confirmation will be sent. 139 * @var string 140 */ 141 var $ConfirmReadingTo = ""; 142 143 /** 144 * Sets the hostname to use in Message-Id and Received headers 145 * and as default HELO string. If empty, the value returned 146 * by SERVER_NAME is used or 'localhost.localdomain'. 147 * @var string 148 */ 149 var $Hostname = ""; 150 151 ///////////////////////////////////////////////// 152 // SMTP VARIABLES 153 ///////////////////////////////////////////////// 154 155 /** 156 * Sets the SMTP hosts. All hosts must be separated by a 157 * semicolon. You can also specify a different port 158 * for each host by using this format: [hostname:port] 159 * (e.g. "smtp1.example.com:25;smtp2.example.com"). 160 * Hosts will be tried in order. 161 * @var string 162 */ 163 var $Host = "localhost"; 164 165 /** 166 * Sets the default SMTP server port. 167 * @var int 168 */ 169 var $Port = 25; 170 171 /** 172 * Sets the SMTP HELO of the message (Default is $Hostname). 173 * @var string 174 */ 175 var $Helo = ""; 176 177 /** 178 * Sets SMTP authentication. Utilizes the Username and Password variables. 179 * @var bool 180 */ 181 var $SMTPAuth = false; 182 183 /** 184 * Sets SMTP username. 185 * @var string 186 */ 187 var $Username = ""; 188 189 /** 190 * Sets SMTP password. 191 * @var string 192 */ 193 var $Password = ""; 194 195 /** 196 * Sets the SMTP server timeout in seconds. This function will not 197 * work with the win32 version. 198 * @var int 199 */ 200 var $Timeout = 10; 201 202 /** 203 * Sets SMTP class debugging on or off. 204 * @var bool 205 */ 206 var $SMTPDebug = false; 207 208 /** 209 * Prevents the SMTP connection from being closed after each mail 210 * sending. If this is set to true then to close the connection 211 * requires an explicit call to SmtpClose(). 212 * @var bool 213 */ 214 var $SMTPKeepAlive = false; 215 216 /**#@+ 217 * @access private 218 */ 219 var $smtp = NULL; 220 var $to = array(); 221 var $cc = array(); 222 var $bcc = array(); 223 var $ReplyTo = array(); 224 var $attachment = array(); 225 var $CustomHeader = array(); 226 var $message_type = ""; 227 var $boundary = array(); 228 var $language = array(); 229 var $error_count = 0; 230 var $LE = "\n"; 231 /**#@-*/ 232 233 ///////////////////////////////////////////////// 234 // VARIABLE METHODS 235 ///////////////////////////////////////////////// 236 237 /** 238 * Sets message type to HTML. 239 * @param bool $bool 240 * @return void 241 */ 242 function IsHTML($bool) { 243 if($bool == true) 244 $this->ContentType = "text/html"; 245 else 246 $this->ContentType = "text/plain"; 247 } 248 249 /** 250 * Sets Mailer to send message using SMTP. 251 * @return void 252 */ 253 function IsSMTP() { 254 $this->Mailer = "smtp"; 255 } 256 257 /** 258 * Sets Mailer to send message using PHP mail() function. 259 * @return void 260 */ 261 function IsMail() { 262 $this->Mailer = "mail"; 263 } 264 265 /** 266 * Sets Mailer to send message using the $Sendmail program. 267 * @return void 268 */ 269 function IsSendmail() { 270 $this->Mailer = "sendmail"; 271 } 272 273 /** 274 * Sets Mailer to send message using the qmail MTA. 275 * @return void 276 */ 277 function IsQmail() { 278 $this->Sendmail = "/var/qmail/bin/sendmail"; 279 $this->Mailer = "sendmail"; 280 } 281 282 283 ///////////////////////////////////////////////// 284 // RECIPIENT METHODS 285 ///////////////////////////////////////////////// 286 287 /** 288 * Adds a "To" address. 289 * @param string $address 290 * @param string $name 291 * @return void 292 */ 293 function AddAddress($address, $name = "") { 294 $cur = count($this->to); 295 $this->to[$cur][0] = trim($address); 296 $this->to[$cur][1] = $name; 297 } 298 299 /** 300 * Adds a "Cc" address. Note: this function works 301 * with the SMTP mailer on win32, not with the "mail" 302 * mailer. 303 * @param string $address 304 * @param string $name 305 * @return void 306 */ 307 function AddCC($address, $name = "") { 308 $cur = count($this->cc); 309 $this->cc[$cur][0] = trim($address); 310 $this->cc[$cur][1] = $name; 311 } 312 313 /** 314 * Adds a "Bcc" address. Note: this function works 315 * with the SMTP mailer on win32, not with the "mail" 316 * mailer. 317 * @param string $address 318 * @param string $name 319 * @return void 320 */ 321 function AddBCC($address, $name = "") { 322 $cur = count($this->bcc); 323 $this->bcc[$cur][0] = trim($address); 324 $this->bcc[$cur][1] = $name; 325 } 326 327 /** 328 * Adds a "Reply-to" address. 329 * @param string $address 330 * @param string $name 331 * @return void 332 */ 333 function AddReplyTo($address, $name = "") { 334 $cur = count($this->ReplyTo); 335 $this->ReplyTo[$cur][0] = trim($address); 336 $this->ReplyTo[$cur][1] = $name; 337 } 338 339 340 ///////////////////////////////////////////////// 341 // MAIL SENDING METHODS 342 ///////////////////////////////////////////////// 343 344 /** 345 * Creates message and assigns Mailer. If the message is 346 * not sent successfully then it returns false. Use the ErrorInfo 347 * variable to view description of the error. 348 * @return bool 349 */ 350 function Send() { 351 $header = ""; 352 $body = ""; 353 $result = true; 354 355 if((count($this->to) + count($this->cc) + count($this->bcc)) < 1) 356 { 357 $this->SetError($this->Lang("provide_address")); 358 return false; 359 } 360 361 // Set whether the message is multipart/alternative 362 if(!empty($this->AltBody)) 363 $this->ContentType = "multipart/alternative"; 364 365 $this->error_count = 0; // reset errors 366 $this->SetMessageType(); 367 $header .= $this->CreateHeader(); 368 $body = $this->CreateBody(); 369 370 if($body == "") { return false; } 371 372 // Choose the mailer 373 switch($this->Mailer) 374 { 375 case "sendmail": 376 $result = $this->SendmailSend($header, $body); 377 break; 378 case "mail": 379 $result = $this->MailSend($header, $body); 380 break; 381 case "smtp": 382 $result = $this->SmtpSend($header, $body); 383 break; 384 default: 385 $this->SetError($this->Mailer . $this->Lang("mailer_not_supported")); 386 $result = false; 387 break; 388 } 389 390 return $result; 391 } 392 393 /** 394 * Sends mail using the $Sendmail program. 395 * @access private 396 * @return bool 397 */ 398 function SendmailSend($header, $body) { 399 if ($this->Sender != "") 400 $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender)); 401 else 402 $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail)); 403 404 if(!@$mail = popen($sendmail, "w")) 405 { 406 $this->SetError($this->Lang("execute") . $this->Sendmail); 407 return false; 408 } 409 410 fputs($mail, $header); 411 fputs($mail, $body); 412 413 $result = pclose($mail) >> 8 & 0xFF; 414 if($result != 0) 415 { 416 $this->SetError($this->Lang("execute") . $this->Sendmail); 417 return false; 418 } 419 420 return true; 421 } 422 423 /** 424 * Sends mail using the PHP mail() function. 425 * @access private 426 * @return bool 427 */ 428 function MailSend($header, $body) { 429 $to = ""; 430 for($i = 0; $i < count($this->to); $i++) 431 { 432 if($i != 0) { $to .= ", "; } 433 $to .= $this->to[$i][0]; 434 } 435 436 if ($this->Sender != "" && strlen(ini_get("safe_mode"))< 1) 437 { 438 $old_from = ini_get("sendmail_from"); 439 ini_set("sendmail_from", $this->Sender); 440 $params = sprintf("-oi -f %s", $this->Sender); 441 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, 442 $header, $params); 443 } 444 else 445 $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header); 446 447 if (isset($old_from)) 448 ini_set("sendmail_from", $old_from); 449 450 if(!$rt) 451 { 452 $this->SetError($this->Lang("instantiate")); 453 return false; 454 } 455 456 return true; 457 } 458 459 /** 460 * Sends mail via SMTP using PhpSMTP (Author: 461 * Chris Ryan). Returns bool. Returns false if there is a 462 * bad MAIL FROM, RCPT, or DATA input. 463 * @access private 464 * @return bool 465 */ 466 function SmtpSend($header, $body) { 467 include_once($this->PluginDir . "class.smtp.php"); 468 $error = ""; 469 $bad_rcpt = array(); 470 471 if(!$this->SmtpConnect()) 472 return false; 473 474 $smtp_from = ($this->Sender == "") ? $this->From : $this->Sender; 475 if(!$this->smtp->Mail($smtp_from)) 476 { 477 $error = $this->Lang("from_failed") . $smtp_from; 478 $this->SetError($error); 479 $this->smtp->Reset(); 480 return false; 481 } 482 483 // Attempt to send attach all recipients 484 for($i = 0; $i < count($this->to); $i++) 485 { 486 if(!$this->smtp->Recipient($this->to[$i][0])) 487 $bad_rcpt[] = $this->to[$i][0]; 488 } 489 for($i = 0; $i < count($this->cc); $i++) 490 { 491 if(!$this->smtp->Recipient($this->cc[$i][0])) 492 $bad_rcpt[] = $this->cc[$i][0]; 493 } 494 for($i = 0; $i < count($this->bcc); $i++) 495 { 496 if(!$this->smtp->Recipient($this->bcc[$i][0])) 497 $bad_rcpt[] = $this->bcc[$i][0]; 498 } 499 500 if(count($bad_rcpt) > 0) // Create error message 501 { 502 for($i = 0; $i < count($bad_rcpt); $i++) 503 { 504 if($i != 0) { $error .= ", "; } 505 $error .= $bad_rcpt[$i]; 506 } 507 $error = $this->Lang("recipients_failed") . $error; 508 $this->SetError($error); 509 $this->smtp->Reset(); 510 return false; 511 } 512 513 if(!$this->smtp->Data($header . $body)) 514 { 515 $this->SetError($this->Lang("data_not_accepted")); 516 $this->smtp->Reset(); 517 return false; 518 } 519 if($this->SMTPKeepAlive == true) 520 $this->smtp->Reset(); 521 else 522 $this->SmtpClose(); 523 524 return true; 525 } 526 527 /** 528 * Initiates a connection to an SMTP server. Returns false if the 529 * operation failed. 530 * @access private 531 * @return bool 532 */ 533 function SmtpConnect() { 534 if($this->smtp == NULL) { $this->smtp = new SMTP(); } 535 536 $this->smtp->do_debug = $this->SMTPDebug; 537 $hosts = explode(";", $this->Host); 538 $index = 0; 539 $connection = ($this->smtp->Connected()); 540 541 // Retry while there is no connection 542 while($index < count($hosts) && $connection == false) 543 { 544 if(strstr($hosts[$index], ":")) 545 list($host, $port) = explode(":", $hosts[$index]); 546 else 547 { 548 $host = $hosts[$index]; 549 $port = $this->Port; 550 } 551 552 if($this->smtp->Connect($host, $port, $this->Timeout)) 553 { 554 if ($this->Helo != '') 555 $this->smtp->Hello($this->Helo); 556 else 557 $this->smtp->Hello($this->ServerHostname()); 558 559 if($this->SMTPAuth) 560 { 561 if(!$this->smtp->Authenticate($this->Username, 562 $this->Password)) 563 { 564 $this->SetError($this->Lang("authenticate")); 565 $this->smtp->Reset(); 566 $connection = false; 567 } 568 } 569 $connection = true; 570 } 571 $index++; 572 } 573 if(!$connection) 574 $this->SetError($this->Lang("connect_host")); 575 576 return $connection; 577 } 578 579 /** 580 * Closes the active SMTP session if one exists. 581 * @return void 582 */ 583 function SmtpClose() { 584 if($this->smtp != NULL) 585 { 586 if($this->smtp->Connected()) 587 { 588 $this->smtp->Quit(); 589 $this->smtp->Close(); 590 } 591 } 592 } 593 594 /** 595 * Sets the language for all class error messages. Returns false 596 * if it cannot load the language file. The default language type 597 * is English. 598 * @param string $lang_type Type of language (e.g. Portuguese: "br") 599 * @param string $lang_path Path to the language file directory 600 * @access public 601 * @return bool 602 */ 603 function SetLanguage($lang_type, $lang_path = "support/") { 604 if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php')) 605 include($lang_path.'phpmailer.lang-'.$lang_type.'.php'); 606 else if(file_exists($lang_path.'phpmailer.lang-en.php')) 607 include($lang_path.'phpmailer.lang-en.php'); 608 else 609 { 610 $this->SetError("Could not load language file"); 611 return false; 612 } 613 $this->language = $PHPMAILER_LANG; 614 615 return true; 616 } 617 618 ///////////////////////////////////////////////// 619 // MESSAGE CREATION METHODS 620 ///////////////////////////////////////////////// 621 622 /** 623 * Creates recipient headers. 624 * @access private 625 * @return string 626 */ 627 function AddrAppend($type, $addr) { 628 $addr_str = $type . ": "; 629 $addr_str .= $this->AddrFormat($addr[0]); 630 if(count($addr) > 1) 631 { 632 for($i = 1; $i < count($addr); $i++) 633 $addr_str .= ", " . $this->AddrFormat($addr[$i]); 634 } 635 $addr_str .= $this->LE; 636 637 return $addr_str; 638 } 639 640 /** 641 * Formats an address correctly. 642 * @access private 643 * @return string 644 */ 645 function AddrFormat($addr) { 646 if(empty($addr[1])) 647 $formatted = $addr[0]; 648 else 649 { 650 $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" . 651 $addr[0] . ">"; 652 } 653 654 return $formatted; 655 } 656 657 /** 658 * Wraps message for use with mailers that do not 659 * automatically perform wrapping and for quoted-printable. 660 * Original written by philippe. 661 * @access private 662 * @return string 663 */ 664 function WrapText($message, $length, $qp_mode = false) { 665 $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE; 666 667 $message = $this->FixEOL($message); 668 if (substr($message, -1) == $this->LE) 669 $message = substr($message, 0, -1); 670 671 $line = explode($this->LE, $message); 672 $message = ""; 673 for ($i=0 ;$i < count($line); $i++) 674 { 675 $line_part = explode(" ", $line[$i]); 676 $buf = ""; 677 for ($e = 0; $e<count($line_part); $e++) 678 { 679 $word = $line_part[$e]; 680 if ($qp_mode and (strlen($word) > $length)) 681 { 682 $space_left = $length - strlen($buf) - 1; 683 if ($e != 0) 684 { 685 if ($space_left > 20) 686 { 687 $len = $space_left; 688 if (substr($word, $len - 1, 1) == "=") 689 $len--; 690 elseif (substr($word, $len - 2, 1) == "=") 691 $len -= 2; 692 $part = substr($word, 0, $len); 693 $word = substr($word, $len); 694 $buf .= " " . $part; 695 $message .= $buf . sprintf("=%s", $this->LE); 696 } 697 else 698 { 699 $message .= $buf . $soft_break; 700 } 701 $buf = ""; 702 } 703 while (strlen($word) > 0) 704 { 705 $len = $length; 706 if (substr($word, $len - 1, 1) == "=") 707 $len--; 708 elseif (substr($word, $len - 2, 1) == "=") 709 $len -= 2; 710 $part = substr($word, 0, $len); 711 $word = substr($word, $len); 712 713 if (strlen($word) > 0) 714 $message .= $part . sprintf("=%s", $this->LE); 715 else 716 $buf = $part; 717 } 718 } 719 else 720 { 721 $buf_o = $buf; 722 $buf .= ($e == 0) ? $word : (" " . $word); 723 724 if (strlen($buf) > $length and $buf_o != "") 725 { 726 $message .= $buf_o . $soft_break; 727 $buf = $word; 728 } 729 } 730 } 731 $message .= $buf . $this->LE; 732 } 733 734 return $message; 735 } 736 737 /** 738 * Set the body wrapping. 739 * @access private 740 * @return void 741 */ 742 function SetWordWrap() { 743 if($this->WordWrap < 1) 744 return; 745 746 switch($this->message_type) 747 { 748 case "alt": 749 // fall through 750 case "alt_attachments": 751 $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap); 752 break; 753 default: 754 $this->Body = $this->WrapText($this->Body, $this->WordWrap); 755 break; 756 } 757 } 758 759 /** 760 * Assembles message header. 761 * @access private 762 * @return string 763 */ 764 function CreateHeader() { 765 $result = ""; 766 767 // Set the boundaries 768 $uniq_id = md5(uniqid(time())); 769 $this->boundary[1] = "b1_" . $uniq_id; 770 $this->boundary[2] = "b2_" . $uniq_id; 771 772 $result .= $this->HeaderLine("Date", $this->RFCDate()); 773 if($this->Sender == "") 774 $result .= $this->HeaderLine("Return-Path", trim($this->From)); 775 else 776 $result .= $this->HeaderLine("Return-Path", trim($this->Sender)); 777 778 // To be created automatically by mail() 779 if($this->Mailer != "mail") 780 { 781 if(count($this->to) > 0) 782 $result .= $this->AddrAppend("To", $this->to); 783 else if (count($this->cc) == 0) 784 $result .= $this->HeaderLine("To", "undisclosed-recipients:;"); 785 if(count($this->cc) > 0) 786 $result .= $this->AddrAppend("Cc", $this->cc); 787 } 788 789 $from = array(); 790 $from[0][0] = trim($this->From); 791 $from[0][1] = $this->FromName; 792 $result .= $this->AddrAppend("From", $from); 793 794 // sendmail and mail() extract Bcc from the header before sending 795 if((($this->Mailer == "sendmail") || ($this->Mailer == "mail")) && (count($this->bcc) > 0)) 796 $result .= $this->AddrAppend("Bcc", $this->bcc); 797 798 if(count($this->ReplyTo) > 0) 799 $result .= $this->AddrAppend("Reply-to", $this->ReplyTo); 800 801 // mail() sets the subject itself 802 if($this->Mailer != "mail") 803 $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject))); 804 805 $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE); 806 $result .= $this->HeaderLine("X-Priority", $this->Priority); 807 $result .= $this->HeaderLine("X-Mailer", "PHPMailer [version " . $this->Version . "] via Zen Cart"); 808 809 if($this->ConfirmReadingTo != "") 810 { 811 $result .= $this->HeaderLine("Disposition-Notification-To", 812 "<" . trim($this->ConfirmReadingTo) . ">"); 813 } 814 815 // Add custom headers 816 for($index = 0; $index < count($this->CustomHeader); $index++) 817 { 818 $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), 819 $this->EncodeHeader(trim($this->CustomHeader[$index][1]))); 820 } 821 $result .= $this->HeaderLine("MIME-Version", "1.0"); 822 823 switch($this->message_type) 824 { 825 case "plain": 826 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding); 827 $result .= sprintf("Content-Type: %s; charset=\"%s\"", 828 $this->ContentType, $this->CharSet); 829 break; 830 case "attachments": 831 // fall through 832 case "alt_attachments": 833 if($this->InlineImageExists()) 834 { 835 $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 836 "multipart/related", $this->LE, $this->LE, 837 $this->boundary[1], $this->LE); 838 } 839 else 840 { 841 $result .= $this->HeaderLine("Content-Type", "multipart/mixed;"); 842 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"'); 843 } 844 break; 845 case "alt": 846 $result .= $this->HeaderLine("Content-Type", "multipart/alternative;"); 847 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"'); 848 break; 849 } 850 851 if($this->Mailer != "mail") 852 $result .= $this->LE.$this->LE; 853 854 return $result; 855 } 856 857 /** 858 * Assembles the message body. Returns an empty string on failure. 859 * @access private 860 * @return string 861 */ 862 function CreateBody() { 863 $result = ""; 864 865 $this->SetWordWrap(); 866 867 switch($this->message_type) 868 { 869 case "alt": 870 $result .= $this->GetBoundary($this->boundary[1], "", 871 "text/plain", ""); 872 $result .= $this->EncodeString($this->AltBody, $this->Encoding); 873 $result .= $this->LE.$this->LE; 874 $result .= $this->GetBoundary($this->boundary[1], "", 875 "text/html", ""); 876 877 $result .= $this->EncodeString($this->Body, $this->Encoding); 878 $result .= $this->LE.$this->LE; 879 880 $result .= $this->EndBoundary($this->boundary[1]); 881 break; 882 case "plain": 883 $result .= $this->EncodeString($this->Body, $this->Encoding); 884 break; 885 case "attachments": 886 $result .= $this->GetBoundary($this->boundary[1], "", "", ""); 887 $result .= $this->EncodeString($this->Body, $this->Encoding); 888 $result .= $this->LE; 889 890 $result .= $this->AttachAll(); 891 break; 892 case "alt_attachments": 893 $result .= sprintf("--%s%s", $this->boundary[1], $this->LE); 894 $result .= sprintf("Content-Type: %s;%s" . 895 "\tboundary=\"%s\"%s", 896 "multipart/alternative", $this->LE, 897 $this->boundary[2], $this->LE.$this->LE); 898 899 // Create text body 900 $result .= $this->GetBoundary($this->boundary[2], "", 901 "text/plain", "") . $this->LE; 902 903 $result .= $this->EncodeString($this->AltBody, $this->Encoding); 904 $result .= $this->LE.$this->LE; 905 906 // Create the HTML body 907 $result .= $this->GetBoundary($this->boundary[2], "", 908 "text/html", "") . $this->LE; 909 910 $result .= $this->EncodeString($this->Body, $this->Encoding); 911 $result .= $this->LE.$this->LE; 912 913 $result .= $this->EndBoundary($this->boundary[2]); 914 915 $result .= $this->AttachAll(); 916 break; 917 } 918 if($this->IsError()) 919 $result = ""; 920 921 return $result; 922 } 923 924 /** 925 * Returns the start of a message boundary. 926 * @access private 927 */ 928 function GetBoundary($boundary, $charSet, $contentType, $encoding) { 929 $result = ""; 930 if($charSet == "") { $charSet = $this->CharSet; } 931 if($contentType == "") { $contentType = $this->ContentType; } 932 if($encoding == "") { $encoding = $this->Encoding; } 933 934 $result .= $this->TextLine("--" . $boundary); 935 $result .= sprintf("Content-Type: %s; charset = \"%s\"", 936 $contentType, $charSet); 937 $result .= $this->LE; 938 $result .= $this->HeaderLine("Content-Transfer-Encoding", $encoding); 939 $result .= $this->LE; 940 941 return $result; 942 } 943 944 /** 945 * Returns the end of a message boundary. 946 * @access private 947 */ 948 function EndBoundary($boundary) { 949 return $this->LE . "--" . $boundary . "--" . $this->LE; 950 } 951 952 /** 953 * Sets the message type. 954 * @access private 955 * @return void 956 */ 957 function SetMessageType() { 958 if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) 959 $this->message_type = "plain"; 960 else 961 { 962 if(count($this->attachment) > 0) 963 $this->message_type = "attachments"; 964 if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) 965 $this->message_type = "alt"; 966 if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) 967 $this->message_type = "alt_attachments"; 968 } 969 } 970 971 /** 972 * Returns a formatted header line. 973 * @access private 974 * @return string 975 */ 976 function HeaderLine($name, $value) { 977 return $name . ": " . $value . $this->LE; 978 } 979 980 /** 981 * Returns a formatted mail line. 982 * @access private 983 * @return string 984 */ 985 function TextLine($value) { 986 return $value . $this->LE; 987 } 988 989 ///////////////////////////////////////////////// 990 // ATTACHMENT METHODS 991 ///////////////////////////////////////////////// 992 993 /** 994 * Adds an attachment from a path on the filesystem. 995 * Returns false if the file could not be found 996 * or accessed. 997 * @param string $path Path to the attachment. 998 * @param string $name Overrides the attachment name. 999 * @param string $encoding File encoding (see $Encoding). 1000 * @param string $type File extension (MIME) type. 1001 * @return bool 1002 */ 1003 function AddAttachment($path, $name = "", $encoding = "base64", 1004 $type = "application/octet-stream") { 1005 if(!@is_file($path)) 1006 { 1007 $this->SetError($this->Lang("file_access") . $path); 1008 return false; 1009 } 1010 1011 $filename = basename($path); 1012 if($name == "") 1013 $name = $filename; 1014 1015 $cur = count($this->attachment); 1016 $this->attachment[$cur][0] = $path; 1017 $this->attachment[$cur][1] = $filename; 1018 $this->attachment[$cur][2] = $name; 1019 $this->attachment[$cur][3] = $encoding; 1020 $this->attachment[$cur][4] = $type; 1021 $this->attachment[$cur][5] = false; // isStringAttachment 1022 $this->attachment[$cur][6] = "attachment"; 1023 $this->attachment[$cur][7] = 0; 1024 1025 return true; 1026 } 1027 1028 /** 1029 * Attaches all fs, string, and binary attachments to the message. 1030 * Returns an empty string on failure. 1031 * @access private 1032 * @return string 1033 */ 1034 function AttachAll() { 1035 // Return text of body 1036 $mime = array(); 1037 1038 // Add all attachments 1039 for($i = 0; $i < count($this->attachment); $i++) 1040 { 1041 // Check for string attachment 1042 $bString = $this->attachment[$i][5]; 1043 if ($bString) 1044 $string = $this->attachment[$i][0]; 1045 else 1046 $path = $this->attachment[$i][0]; 1047 1048 $filename = $this->attachment[$i][1]; 1049 $name = $this->attachment[$i][2]; 1050 $encoding = $this->attachment[$i][3]; 1051 $type = $this->attachment[$i][4]; 1052 $disposition = $this->attachment[$i][6]; 1053 $cid = $this->attachment[$i][7]; 1054 1055 $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE); 1056 $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE); 1057 $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE); 1058 1059 if($disposition == "inline") 1060 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE); 1061 1062 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", 1063 $disposition, $name, $this->LE.$this->LE); 1064 1065 // Encode as string attachment 1066 if($bString) 1067 { 1068 $mime[] = $this->EncodeString($string, $encoding); 1069 if($this->IsError()) { return ""; } 1070 $mime[] = $this->LE.$this->LE; 1071 } 1072 else 1073 { 1074 $mime[] = $this->EncodeFile($path, $encoding); 1075 if($this->IsError()) { return ""; } 1076 $mime[] = $this->LE.$this->LE; 1077 } 1078 } 1079 1080 $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE); 1081 1082 return join("", $mime); 1083 } 1084 1085 /** 1086 * Encodes attachment in requested format. Returns an 1087 * empty string on failure. 1088 * @access private 1089 * @return string 1090 */ 1091 function EncodeFile ($path, $encoding = "base64") { 1092 if(!@$fd = fopen($path, "rb")) 1093 { 1094 $this->SetError($this->Lang("file_open") . $path); 1095 return ""; 1096 } 1097 $magic_quotes = get_magic_quotes_runtime(); 1098 set_magic_quotes_runtime(0); 1099 $file_buffer = fread($fd, filesize($path)); 1100 $file_buffer = $this->EncodeString($file_buffer, $encoding); 1101 fclose($fd); 1102 set_magic_quotes_runtime($magic_quotes); 1103 1104 return $file_buffer; 1105 } 1106 1107 /** 1108 * Encodes string to requested format. Returns an 1109 * empty string on failure. 1110 * @access private 1111 * @return string 1112 */ 1113 function EncodeString ($str, $encoding = "base64") { 1114 $encoded = ""; 1115 switch(strtolower($encoding)) { 1116 case "base64": 1117 // chunk_split is found in PHP >= 3.0.6 1118 $encoded = chunk_split(base64_encode($str), 76, $this->LE); 1119 break; 1120 case "7bit": 1121 case "8bit": 1122 $encoded = $this->FixEOL($str); 1123 if (substr($encoded, -(strlen($this->LE))) != $this->LE) 1124 $encoded .= $this->LE; 1125 break; 1126 case "binary": 1127 $encoded = $str; 1128 break; 1129 case "quoted-printable": 1130 $encoded = $this->EncodeQP($str); 1131 break; 1132 default: 1133 $this->SetError($this->Lang("encoding") . $encoding); 1134 break; 1135 } 1136 return $encoded; 1137 } 1138 1139 /** 1140 * Encode a header string to best of Q, B, quoted or none. 1141 * @access private 1142 * @return string 1143 */ 1144 function EncodeHeader ($str, $position = 'text') { 1145 $x = 0; 1146 1147 switch (strtolower($position)) { 1148 case 'phrase': 1149 if (!preg_match('/[\200-\377]/', $str)) { 1150 // Can't use addslashes as we don't know what value has magic_quotes_sybase. 1151 $encoded = addcslashes($str, "\0..\37\177\\\""); 1152 1153 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) 1154 return ($encoded); 1155 else 1156 return ("\"$encoded\""); 1157 } 1158 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches); 1159 break; 1160 case 'comment': 1161 $x = preg_match_all('/[()"]/', $str, $matches); 1162 // Fall-through 1163 case 'text': 1164 default: 1165 $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches); 1166 break; 1167 } 1168 1169 if ($x == 0) 1170 return ($str); 1171 1172 $maxlen = 75 - 7 - strlen($this->CharSet); 1173 // Try to select the encoding which should produce the shortest output 1174 if (strlen($str)/3 < $x) { 1175 $encoding = 'B'; 1176 $encoded = base64_encode($str); 1177 $maxlen -= $maxlen % 4; 1178 $encoded = trim(chunk_split($encoded, $maxlen, "\n")); 1179 } else { 1180 $encoding = 'Q'; 1181 $encoded = $this->EncodeQ($str, $position); 1182 $encoded = $this->WrapText($encoded, $maxlen, true); 1183 $encoded = str_replace("=".$this->LE, "\n", trim($encoded)); 1184 } 1185 1186 $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded); 1187 $encoded = trim(str_replace("\n", $this->LE, $encoded)); 1188 1189 return $encoded; 1190 } 1191 1192 /** 1193 * Encode string to quoted-printable. 1194 * @access private 1195 * @return string 1196 */ 1197 function EncodeQP ($str) { 1198 $encoded = $this->FixEOL($str); 1199 if (substr($encoded, -(strlen($this->LE))) != $this->LE) 1200 $encoded .= $this->LE; 1201 1202 // Replace every high ascii, control and = characters 1203 $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e', 1204 "'='.sprintf('%02X', ord('\\1'))", $encoded); 1205 // Replace every spaces and tabs when it's the last character on a line 1206 $encoded = preg_replace("/([\011\040])".$this->LE."/e", 1207 "'='.sprintf('%02X', ord('\\1')).'".$this->LE."'", $encoded); 1208 1209 // Maximum line length of 76 characters before CRLF (74 + space + '=') 1210 $encoded = $this->WrapText($encoded, 74, true); 1211 1212 return $encoded; 1213 } 1214 1215 /** 1216 * Encode string to q encoding. 1217 * @access private 1218 * @return string 1219 */ 1220 function EncodeQ ($str, $position = "text") { 1221 // There should not be any EOL in the string 1222 $encoded = preg_replace("[\r\n]", "", $str); 1223 1224 switch (strtolower($position)) { 1225 case "phrase": 1226 $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded); 1227 break; 1228 case "comment": 1229 $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded); 1230 case "text": 1231 default: 1232 // Replace every high ascii, control =, ? and _ characters 1233 $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e', 1234 "'='.sprintf('%02X', ord('\\1'))", $encoded); 1235 break; 1236 } 1237 1238 // Replace every spaces to _ (more readable than =20) 1239 $encoded = str_replace(" ", "_", $encoded); 1240 1241 return $encoded; 1242 } 1243 1244 /** 1245 * Adds a string or binary attachment (non-filesystem) to the list. 1246 * This method can be used to attach ascii or binary data, 1247 * such as a BLOB record from a database. 1248 * @param string $string String attachment data. 1249 * @param string $filename Name of the attachment. 1250 * @param string $encoding File encoding (see $Encoding). 1251 * @param string $type File extension (MIME) type. 1252 * @return void 1253 */ 1254 function AddStringAttachment($string, $filename, $encoding = "base64", 1255 $type = "application/octet-stream") { 1256 // Append to $attachment array 1257 $cur = count($this->attachment); 1258 $this->attachment[$cur][0] = $string; 1259 $this->attachment[$cur][1] = $filename; 1260 $this->attachment[$cur][2] = $filename; 1261 $this->attachment[$cur][3] = $encoding; 1262 $this->attachment[$cur][4] = $type; 1263 $this->attachment[$cur][5] = true; // isString 1264 $this->attachment[$cur][6] = "attachment"; 1265 $this->attachment[$cur][7] = 0; 1266 } 1267 1268 /** 1269 * Adds an embedded attachment. This can include images, sounds, and 1270 * just about any other document. Make sure to set the $type to an 1271 * image type. For JPEG images use "image/jpeg" and for GIF images 1272 * use "image/gif". 1273 * @param string $path Path to the attachment. 1274 * @param string $cid Content ID of the attachment. Use this to identify 1275 * the Id for accessing the image in an HTML form. 1276 * @param string $name Overrides the attachment name. 1277 * @param string $encoding File encoding (see $Encoding). 1278 * @param string $type File extension (MIME) type. 1279 * @return bool 1280 */ 1281 function AddEmbeddedImage($path, $cid, $name = "", $encoding = "base64", 1282 $type = "application/octet-stream") { 1283 1284 if(!@is_file($path)) 1285 { 1286 $this->SetError($this->Lang("file_access") . $path); 1287 return false; 1288 } 1289 1290 $filename = basename($path); 1291 if($name == "") 1292 $name = $filename; 1293 1294 // Append to $attachment array 1295 $cur = count($this->attachment); 1296 $this->attachment[$cur][0] = $path; 1297 $this->attachment[$cur][1] = $filename; 1298 $this->attachment[$cur][2] = $name; 1299 $this->attachment[$cur][3] = $encoding; 1300 $this->attachment[$cur][4] = $type; 1301 $this->attachment[$cur][5] = false; // isStringAttachment 1302 $this->attachment[$cur][6] = "inline"; 1303 $this->attachment[$cur][7] = $cid; 1304 1305 return true; 1306 } 1307 1308 /** 1309 * Returns true if an inline attachment is present. 1310 * @access private 1311 * @return bool 1312 */ 1313 function InlineImageExists() { 1314 $result = false; 1315 for($i = 0; $i < count($this->attachment); $i++) 1316 { 1317 if($this->attachment[$i][6] == "inline") 1318 { 1319 $result = true; 1320 break; 1321 } 1322 } 1323 1324 return $result; 1325 } 1326 1327 ///////////////////////////////////////////////// 1328 // MESSAGE RESET METHODS 1329 ///////////////////////////////////////////////// 1330 1331 /** 1332 * Clears all recipients assigned in the TO array. Returns void. 1333 * @return void 1334 */ 1335 function ClearAddresses() { 1336 $this->to = array(); 1337 } 1338 1339 /** 1340 * Clears all recipients assigned in the CC array. Returns void. 1341 * @return void 1342 */ 1343 function ClearCCs() { 1344 $this->cc = array(); 1345 } 1346 1347 /** 1348 * Clears all recipients assigned in the BCC array. Returns void. 1349 * @return void 1350 */ 1351 function ClearBCCs() { 1352 $this->bcc = array(); 1353 } 1354 1355 /** 1356 * Clears all recipients assigned in the ReplyTo array. Returns void. 1357 * @return void 1358 */ 1359 function ClearReplyTos() { 1360 $this->ReplyTo = array(); 1361 } 1362 1363 /** 1364 * Clears all recipients assigned in the TO, CC and BCC 1365 * array. Returns void. 1366 * @return void 1367 */ 1368 function ClearAllRecipients() { 1369 $this->to = array(); 1370 $this->cc = array(); 1371 $this->bcc = array(); 1372 } 1373 1374 /** 1375 * Clears all previously set filesystem, string, and binary 1376 * attachments. Returns void. 1377 * @return void 1378 */ 1379 function ClearAttachments() { 1380 $this->attachment = array(); 1381 } 1382 1383 /** 1384 * Clears all custom headers. Returns void. 1385 * @return void 1386 */ 1387 function ClearCustomHeaders() { 1388 $this->CustomHeader = array(); 1389 } 1390 1391 1392 ///////////////////////////////////////////////// 1393 // MISCELLANEOUS METHODS 1394 ///////////////////////////////////////////////// 1395 1396 /** 1397 * Adds the error message to the error container. 1398 * Returns void. 1399 * @access private 1400 * @return void 1401 */ 1402 function SetError($msg) { 1403 $this->error_count++; 1404 $this->ErrorInfo = $msg; 1405 } 1406 1407 /** 1408 * Returns the proper RFC 822 formatted date. 1409 * @access private 1410 * @return string 1411 */ 1412 function RFCDate() { 1413 $tz = date("Z"); 1414 $tzs = ($tz < 0) ? "-" : "+"; 1415 $tz = abs($tz); 1416 $tz = ($tz/3600)*100 + ($tz%3600)/60; 1417 $result = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz); 1418 1419 return $result; 1420 } 1421 1422 /** 1423 * Returns the appropriate server variable. Should work with both 1424 * PHP 4.1.0+ as well as older versions. Returns an empty string 1425 * if nothing is found. 1426 * @access private 1427 * @return mixed 1428 */ 1429 function ServerVar($varName) { 1430 global $HTTP_SERVER_VARS; 1431 global $HTTP_ENV_VARS; 1432 1433 if(!isset($_SERVER)) 1434 { 1435 $_SERVER = $HTTP_SERVER_VARS; 1436 if(!isset($_SERVER["REMOTE_ADDR"])) 1437 $_SERVER = $HTTP_ENV_VARS; // must be Apache 1438 } 1439 1440 if(isset($_SERVER[$varName])) 1441 return $_SERVER[$varName]; 1442 else 1443 return ""; 1444 } 1445 1446 /** 1447 * Returns the server hostname or 'localhost.localdomain' if unknown. 1448 * @access private 1449 * @return string 1450 */ 1451 function ServerHostname() { 1452 if ($this->Hostname != "") 1453 $result = $this->Hostname; 1454 elseif ($this->ServerVar('SERVER_NAME') != "") 1455 $result = $this->ServerVar('SERVER_NAME'); 1456 else 1457 $result = "localhost.localdomain"; 1458 1459 return $result; 1460 } 1461 1462 /** 1463 * Returns a message in the appropriate language. 1464 * @access private 1465 * @return string 1466 */ 1467 function Lang($key) { 1468 if(count($this->language) < 1) 1469 $this->SetLanguage("en"); // set the default language 1470 1471 if(isset($this->language[$key])) 1472 return $this->language[$key]; 1473 else 1474 return "Language string failed to load: " . $key; 1475 } 1476 1477 /** 1478 * Returns true if an error occurred. 1479 * @return bool 1480 */ 1481 function IsError() { 1482 return ($this->error_count > 0); 1483 } 1484 1485 /** 1486 * Changes every end of line from CR or LF to CRLF. 1487 * @access private 1488 * @return string 1489 */ 1490 function FixEOL($str) { 1491 $str = str_replace("\r\n", "\n", $str); 1492 $str = str_replace("\r", "\n", $str); 1493 $str = str_replace("\n", $this->LE, $str); 1494 return $str; 1495 } 1496 1497 /** 1498 * Adds a custom header. 1499 * @return void 1500 */ 1501 function AddCustomHeader($custom_header) { 1502 $this->CustomHeader[] = explode(":", $custom_header, 2); 1503 } 1504 } 1505 1506 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 16:45:43 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |