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