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