首页 > 代码库 > ThinkPHP发送邮件

ThinkPHP发送邮件

引入类文件:PHPMailer.class.php

技术分享
   1 <?php   2 /*~ class.phpmailer.php   3 .---------------------------------------------------------------------------.   4 |  Software: PHPMailer - PHP email class                                    |   5 |   Version: 5.0.2                                                          |   6 |   Contact: via sourceforge.net support pages (also www.codeworxtech.com)  |   7 |      Info: http://phpmailer.sourceforge.net                               |   8 |   Support: http://sourceforge.net/projects/phpmailer/                     |   9 | ------------------------------------------------------------------------- |  10 |     Admin: Andy Prevost (project admininistrator)                         |  11 |   Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |  12 |          : Marcus Bointon (coolbru) coolbru@users.sourceforge.net         |  13 |   Founder: Brent R. Matzelle (original founder)                           |  14 | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved.               |  15 | Copyright (c) 2001-2003, Brent R. Matzelle                                |  16 | ------------------------------------------------------------------------- |  17 |   License: Distributed under the Lesser General Public License (LGPL)     |  18 |            http://www.gnu.org/copyleft/lesser.html                        |  19 | This program is distributed in the hope that it will be useful - WITHOUT  |  20 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     |  21 | FITNESS FOR A PARTICULAR PURPOSE.                                         |  22 | ------------------------------------------------------------------------- |  23 | We offer a number of paid services (www.codeworxtech.com):                |  24 | - Web Hosting on highly optimized fast and secure servers                 |  25 | - Technology Consulting                                                   |  26 | - Oursourcing (highly qualified programmers and graphic designers)        |  27 ‘---------------------------------------------------------------------------‘  28 */  29   30 /**  31  * PHPMailer - PHP email transport class  32  * NOTE: Requires PHP version 5 or later  33  * @package PHPMailer  34  * @author Andy Prevost  35  * @author Marcus Bointon  36  * @copyright 2004 - 2009 Andy Prevost  37  * @version $Id: class.phpmailer.php 447 2009-05-25 01:36:38Z codeworxtech $  38  * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License  39  */  40 class PHPMailer {  41   42   /////////////////////////////////////////////////  43   // PROPERTIES, PUBLIC  44   /////////////////////////////////////////////////  45   46   /**  47    * Email priority (1 = High, 3 = Normal, 5 = low).  48    * @var int  49    */  50   public $Priority          = 3;  51   52   /**  53    * Sets the CharSet of the message.  54    * @var string  55    */  56   public $CharSet           = ‘iso-8859-1‘;  57   58   /**  59    * Sets the Content-type of the message.  60    * @var string  61    */  62   public $ContentType       = ‘text/plain‘;  63   64   /**  65    * Sets the Encoding of the message. Options for this are  66    *  "8bit", "7bit", "binary", "base64", and "quoted-printable".  67    * @var string  68    */  69   public $Encoding          = ‘8bit‘;  70   71   /**  72    * Holds the most recent mailer error message.  73    * @var string  74    */  75   public $ErrorInfo         = ‘‘;  76   77   /**  78    * Sets the From email address for the message.  79    * @var string  80    */  81   public $From              = ‘root@localhost‘;  82   83   /**  84    * Sets the From name of the message.  85    * @var string  86    */  87   public $FromName          = ‘Root User‘;  88   89   /**  90    * Sets the Sender email (Return-Path) of the message.  If not empty,  91    * will be sent via -f to sendmail or as ‘MAIL FROM‘ in smtp mode.  92    * @var string  93    */  94   public $Sender            = ‘‘;  95   96   /**  97    * Sets the Subject of the message.  98    * @var string  99    */ 100   public $Subject           = ‘‘; 101  102   /** 103    * Sets the Body of the message.  This can be either an HTML or text body. 104    * If HTML then run IsHTML(true). 105    * @var string 106    */ 107   public $Body              = ‘‘; 108  109   /** 110    * Sets the text-only body of the message.  This automatically sets the 111    * email to multipart/alternative.  This body can be read by mail 112    * clients that do not have HTML email capability such as mutt. Clients 113    * that can read HTML will view the normal Body. 114    * @var string 115    */ 116   public $AltBody           = ‘‘; 117  118   /** 119    * Sets word wrapping on the body of the message to a given number of 120    * characters. 121    * @var int 122    */ 123   public $WordWrap          = 0; 124  125   /** 126    * Method to send mail: ("mail", "sendmail", or "smtp"). 127    * @var string 128    */ 129   public $Mailer            = ‘mail‘; 130  131   /** 132    * Sets the path of the sendmail program. 133    * @var string 134    */ 135   public $Sendmail          = ‘/usr/sbin/sendmail‘; 136  137   /** 138    * Path to PHPMailer plugins.  Useful if the SMTP class 139    * is in a different directory than the PHP include path. 140    * @var string 141    */ 142   public $PluginDir         = ‘‘; 143  144   /** 145    * Sets the email address that a reading confirmation will be sent. 146    * @var string 147    */ 148   public $ConfirmReadingTo  = ‘‘; 149  150   /** 151    * Sets the hostname to use in Message-Id and Received headers 152    * and as default HELO string. If empty, the value returned 153    * by SERVER_NAME is used or ‘localhost.localdomain‘. 154    * @var string 155    */ 156   public $Hostname          = ‘‘; 157  158   /** 159    * Sets the message ID to be used in the Message-Id header. 160    * If empty, a unique id will be generated. 161    * @var string 162    */ 163   public $MessageID         = ‘‘; 164  165   ///////////////////////////////////////////////// 166   // PROPERTIES FOR SMTP 167   ///////////////////////////////////////////////// 168  169   /** 170    * Sets the SMTP hosts.  All hosts must be separated by a 171    * semicolon.  You can also specify a different port 172    * for each host by using this format: [hostname:port] 173    * (e.g. "smtp1.example.com:25;smtp2.example.com"). 174    * Hosts will be tried in order. 175    * @var string 176    */ 177   public $Host          = ‘localhost‘; 178  179   /** 180    * Sets the default SMTP server port. 181    * @var int 182    */ 183   public $Port          = 25; 184  185   /** 186    * Sets the SMTP HELO of the message (Default is $Hostname). 187    * @var string 188    */ 189   public $Helo          = ‘‘; 190  191   /** 192    * Sets connection prefix. 193    * Options are "", "ssl" or "tls" 194    * @var string 195    */ 196   public $SMTPSecure    = ‘‘; 197  198   /** 199    * Sets SMTP authentication. Utilizes the Username and Password variables. 200    * @var bool 201    */ 202   public $SMTPAuth      = false; 203  204   /** 205    * Sets SMTP username. 206    * @var string 207    */ 208   public $Username      = ‘‘; 209  210   /** 211    * Sets SMTP password. 212    * @var string 213    */ 214   public $Password      = ‘‘; 215  216   /** 217    * Sets the SMTP server timeout in seconds. 218    * This function will not work with the win32 version. 219    * @var int 220    */ 221   public $Timeout       = 10; 222  223   /** 224    * Sets SMTP class debugging on or off. 225    * @var bool 226    */ 227   public $SMTPDebug     = false; 228  229   /** 230    * Prevents the SMTP connection from being closed after each mail 231    * sending.  If this is set to true then to close the connection 232    * requires an explicit call to SmtpClose(). 233    * @var bool 234    */ 235   public $SMTPKeepAlive = false; 236  237   /** 238    * Provides the ability to have the TO field process individual 239    * emails, instead of sending to entire TO addresses 240    * @var bool 241    */ 242   public $SingleTo      = false; 243  244   /** 245    * Provides the ability to change the line ending 246    * @var string 247    */ 248   public $LE              = "\n"; 249  250   /** 251    * Sets the PHPMailer Version number 252    * @var string 253    */ 254   public $Version         = ‘5.0.2‘; 255  256   ///////////////////////////////////////////////// 257   // PROPERTIES, PRIVATE AND PROTECTED 258   ///////////////////////////////////////////////// 259  260   private   $smtp           = NULL; 261   private   $to             = array(); 262   private   $cc             = array(); 263   private   $bcc            = array(); 264   private   $ReplyTo        = array(); 265   private   $all_recipients = array(); 266   private   $attachment     = array(); 267   private   $CustomHeader   = array(); 268   private   $message_type   = ‘‘; 269   private   $boundary       = array(); 270   protected $language       = array(); 271   private   $error_count    = 0; 272   private   $sign_cert_file = ""; 273   private   $sign_key_file  = ""; 274   private   $sign_key_pass  = ""; 275   private   $exceptions     = false; 276  277   ///////////////////////////////////////////////// 278   // CONSTANTS 279   ///////////////////////////////////////////////// 280  281   const STOP_MESSAGE = 0; // message only, continue processing 282   const STOP_CONTINUE = 1; // message?, likely ok to continue processing 283   const STOP_CRITICAL = 2; // message, plus full stop, critical error reached 284  285   ///////////////////////////////////////////////// 286   // METHODS, VARIABLES 287   ///////////////////////////////////////////////// 288  289   /** 290    * Constructor 291    * @param boolean $exceptions Should we throw external exceptions? 292    */ 293   public function __construct($exceptions = false) { 294     $this->exceptions = ($exceptions == true); 295   } 296  297   /** 298    * Sets message type to HTML. 299    * @param bool $ishtml 300    * @return void 301    */ 302   public function IsHTML($ishtml = true) { 303     if ($ishtml) { 304       $this->ContentType = ‘text/html‘; 305     } else { 306       $this->ContentType = ‘text/plain‘; 307     } 308   } 309  310   /** 311    * Sets Mailer to send message using SMTP. 312    * @return void 313    */ 314   public function IsSMTP() { 315     $this->Mailer = ‘smtp‘; 316   } 317  318   /** 319    * Sets Mailer to send message using PHP mail() function. 320    * @return void 321    */ 322   public function IsMail() { 323     $this->Mailer = ‘mail‘; 324   } 325  326   /** 327    * Sets Mailer to send message using the $Sendmail program. 328    * @return void 329    */ 330   public function IsSendmail() { 331     if (!stristr(ini_get(‘sendmail_path‘), ‘sendmail‘)) { 332       $this->Sendmail = ‘/var/qmail/bin/sendmail‘; 333     } 334     $this->Mailer = ‘sendmail‘; 335   } 336  337   /** 338    * Sets Mailer to send message using the qmail MTA. 339    * @return void 340    */ 341   public function IsQmail() { 342     if (stristr(ini_get(‘sendmail_path‘), ‘qmail‘)) { 343       $this->Sendmail = ‘/var/qmail/bin/sendmail‘; 344     } 345     $this->Mailer = ‘sendmail‘; 346   } 347  348   ///////////////////////////////////////////////// 349   // METHODS, RECIPIENTS 350   ///////////////////////////////////////////////// 351  352   /** 353    * Adds a "To" address. 354    * @param string $address 355    * @param string $name 356    * @return boolean true on success, false if address already used 357    */ 358   public function AddAddress($address, $name = ‘‘) { 359     return $this->AddAnAddress(‘to‘, $address, $name); 360   } 361  362   /** 363    * Adds a "Cc" address. 364    * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer. 365    * @param string $address 366    * @param string $name 367    * @return boolean true on success, false if address already used 368    */ 369   public function AddCC($address, $name = ‘‘) { 370     return $this->AddAnAddress(‘cc‘, $address, $name); 371   } 372  373   /** 374    * Adds a "Bcc" address. 375    * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer. 376    * @param string $address 377    * @param string $name 378    * @return boolean true on success, false if address already used 379    */ 380   public function AddBCC($address, $name = ‘‘) { 381     return $this->AddAnAddress(‘bcc‘, $address, $name); 382   } 383  384   /** 385    * Adds a "Reply-to" address. 386    * @param string $address 387    * @param string $name 388    * @return boolean 389    */ 390   public function AddReplyTo($address, $name = ‘‘) { 391     return $this->AddAnAddress(‘ReplyTo‘, $address, $name); 392   } 393  394   /** 395    * Adds an address to one of the recipient arrays 396    * Addresses that have been added already return false, but do not throw exceptions 397    * @param string $kind One of ‘to‘, ‘cc‘, ‘bcc‘, ‘ReplyTo‘ 398    * @param string $address The email address to send to 399    * @param string $name 400    * @return boolean true on success, false if address already used or invalid in some way 401    * @access private 402    */ 403   private function AddAnAddress($kind, $address, $name = ‘‘) { 404     if (!preg_match(‘/^(to|cc|bcc|ReplyTo)$/‘, $kind)) { 405       echo ‘Invalid recipient array: ‘ . kind; 406       return false; 407     } 408     $address = trim($address); 409     $name = trim(preg_replace(‘/[\r\n]+/‘, ‘‘, $name)); //Strip breaks and trim 410     if (!self::ValidateAddress($address)) { 411       $this->SetError($this->Lang(‘invalid_address‘).‘: ‘. $address); 412       if ($this->exceptions) { 413         throw new phpmailerException($this->Lang(‘invalid_address‘).‘: ‘.$address); 414       } 415       echo $this->Lang(‘invalid_address‘).‘: ‘.$address; 416       return false; 417     } 418   if ($kind != ‘ReplyTo‘) { 419     if (!isset($this->all_recipients[strtolower($address)])) { 420         array_push($this->$kind, array($address, $name)); 421         $this->all_recipients[strtolower($address)] = true; 422     return true; 423       } 424   } else { 425     if (!array_key_exists(strtolower($address), $this->ReplyTo)) { 426         $this->ReplyTo[strtolower($address)] = array($address, $name); 427     return true; 428     } 429   } 430     return false; 431   } 432  433 /** 434  * Set the From and FromName properties 435  * @param string $address 436  * @param string $name 437  * @return boolean 438  */ 439   public function SetFrom($address, $name = ‘‘) { 440     $address = trim($address); 441     $name = trim(preg_replace(‘/[\r\n]+/‘, ‘‘, $name)); //Strip breaks and trim 442     if (!self::ValidateAddress($address)) { 443       $this->SetError($this->Lang(‘invalid_address‘).‘: ‘. $address); 444       if ($this->exceptions) { 445         throw new phpmailerException($this->Lang(‘invalid_address‘).‘: ‘.$address); 446       } 447       echo $this->Lang(‘invalid_address‘).‘: ‘.$address; 448       return false; 449     } 450   $this->From = $address; 451   $this->FromName = $name; 452   return true; 453   } 454  455   /** 456    * Check that a string looks roughly like an email address should 457    * Static so it can be used without instantiation 458    * Tries to use PHP built-in validator in the filter extension (from PHP 5.2), falls back to a reasonably competent regex validator 459    * Conforms approximately to RFC2822 460    * @link http://www.hexillion.com/samples/#Regex Original pattern found here 461    * @param string $address The email address to check 462    * @return boolean 463    * @static 464    * @access public 465    */ 466   public static function ValidateAddress($address) { 467     if (function_exists(‘filter_var‘)) { //Introduced in PHP 5.2 468       if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) { 469         return false; 470       } else { 471         return true; 472       } 473     } else { 474       return preg_match(‘/^(?:[\w\!\#\$\%\&\‘\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\‘\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/‘, $address); 475     } 476   } 477  478   ///////////////////////////////////////////////// 479   // METHODS, MAIL SENDING 480   ///////////////////////////////////////////////// 481  482   /** 483    * Creates message and assigns Mailer. If the message is 484    * not sent successfully then it returns false.  Use the ErrorInfo 485    * variable to view description of the error. 486    * @return bool 487    */ 488   public function Send() { 489     try { 490       if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) { 491         throw new phpmailerException($this->Lang(‘provide_address‘), self::STOP_CRITICAL); 492       } 493  494       // Set whether the message is multipart/alternative 495       if(!empty($this->AltBody)) { 496         $this->ContentType = ‘multipart/alternative‘; 497       } 498  499       $this->error_count = 0; // reset errors 500       $this->SetMessageType(); 501       $header = $this->CreateHeader(); 502       $body = $this->CreateBody(); 503  504       if (empty($this->Body)) { 505         throw new phpmailerException($this->Lang(‘empty_message‘), self::STOP_CRITICAL); 506       } 507  508       // Choose the mailer and send through it 509       switch($this->Mailer) { 510         case ‘sendmail‘: 511           return $this->SendmailSend($header, $body); 512         case ‘smtp‘: 513           return $this->SmtpSend($header, $body); 514         case ‘mail‘: 515         default: 516           return $this->MailSend($header, $body); 517       } 518  519     } catch (phpmailerException $e) { 520       $this->SetError($e->getMessage()); 521       if ($this->exceptions) { 522         throw $e; 523       } 524       echo $e->getMessage()."\n"; 525       return false; 526     } 527   } 528  529   /** 530    * Sends mail using the $Sendmail program. 531    * @param string $header The message headers 532    * @param string $body The message body 533    * @access protected 534    * @return bool 535    */ 536   protected function SendmailSend($header, $body) { 537     if ($this->Sender != ‘‘) { 538       $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender)); 539     } else { 540       $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail)); 541     } 542     if(!@$mail = popen($sendmail, ‘w‘)) { 543       throw new phpmailerException($this->Lang(‘execute‘) . $this->Sendmail, self::STOP_CRITICAL); 544     } 545     fputs($mail, $header); 546     fputs($mail, $body); 547     $result = pclose($mail); 548     if($result != 0) { 549       throw new phpmailerException($this->Lang(‘execute‘) . $this->Sendmail, self::STOP_CRITICAL); 550     } 551     return true; 552   } 553  554   /** 555    * Sends mail using the PHP mail() function. 556    * @param string $header The message headers 557    * @param string $body The message body 558    * @access protected 559    * @return bool 560    */ 561   protected function MailSend($header, $body) { 562     $toArr = array(); 563     foreach($this->to as $t) { 564       $toArr[] = $this->AddrFormat($t); 565     } 566     $to = implode(‘, ‘, $toArr); 567  568     $params = sprintf("-oi -f %s", $this->Sender); 569     if ($this->Sender != ‘‘ && strlen(ini_get(‘safe_mode‘))< 1) { 570       $old_from = ini_get(‘sendmail_from‘); 571       ini_set(‘sendmail_from‘, $this->Sender); 572       if ($this->SingleTo === true && count($toArr) > 1) { 573         foreach ($toArr as $key => $val) { 574           $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params); 575         } 576       } else { 577         $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params); 578       } 579     } else { 580       if ($this->SingleTo === true && count($toArr) > 1) { 581         foreach ($toArr as $key => $val) { 582           $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params); 583         } 584       } else { 585         $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header); 586       } 587     } 588     if (isset($old_from)) { 589       ini_set(‘sendmail_from‘, $old_from); 590     } 591     if(!$rt) { 592       throw new phpmailerException($this->Lang(‘instantiate‘), self::STOP_CRITICAL); 593     } 594     return true; 595   } 596  597   /** 598    * Sends mail via SMTP using PhpSMTP 599    * Returns false if there is a bad MAIL FROM, RCPT, or DATA input. 600    * @param string $header The message headers 601    * @param string $body The message body 602    * @uses SMTP 603    * @access protected 604    * @return bool 605    */ 606   protected function SmtpSend($header, $body) { 607     require_once $this->PluginDir . ‘class.smtp.php‘; 608     $bad_rcpt = array(); 609  610     if(!$this->SmtpConnect()) { 611       throw new phpmailerException($this->Lang(‘smtp_connect_failed‘), self::STOP_CRITICAL); 612     } 613     $smtp_from = ($this->Sender == ‘‘) ? $this->From : $this->Sender; 614     if(!$this->smtp->Mail($smtp_from)) { 615       throw new phpmailerException($this->Lang(‘from_failed‘) . $smtp_from, self::STOP_CRITICAL); 616     } 617  618     // Attempt to send attach all recipients 619     foreach($this->to as $to) { 620       if (!$this->smtp->Recipient($to[0])) { 621         $bad_rcpt[] = $to[0]; 622       } 623     } 624     foreach($this->cc as $cc) { 625       if (!$this->smtp->Recipient($cc[0])) { 626         $bad_rcpt[] = $cc[0]; 627       } 628     } 629     foreach($this->bcc as $bcc) { 630       if (!$this->smtp->Recipient($bcc[0])) { 631         $bad_rcpt[] = $bcc[0]; 632       } 633     } 634     if (count($bad_rcpt) > 0 ) { //Create error message for any bad addresses 635       $badaddresses = implode(‘, ‘, $bad_rcpt); 636       throw new phpmailerException($this->Lang(‘recipients_failed‘) . $badaddresses); 637     } 638     if(!$this->smtp->Data($header . $body)) { 639       throw new phpmailerException($this->Lang(‘data_not_accepted‘), self::STOP_CRITICAL); 640     } 641     if($this->SMTPKeepAlive == true) { 642       $this->smtp->Reset(); 643     } 644     return true; 645   } 646  647   /** 648    * Initiates a connection to an SMTP server. 649    * Returns false if the operation failed. 650    * @uses SMTP 651    * @access public 652    * @return bool 653    */ 654   public function SmtpConnect() { 655     if(is_null($this->smtp)) { 656       $this->smtp = new SMTP(); 657     } 658  659     $this->smtp->do_debug = $this->SMTPDebug; 660     $hosts = explode(‘;‘, $this->Host); 661     $index = 0; 662     $connection = $this->smtp->Connected(); 663  664     // Retry while there is no connection 665     try { 666       while($index < count($hosts) && !$connection) { 667         $hostinfo = array(); 668         if (preg_match(‘/^(.+):([0-9]+)$/‘, $hosts[$index], $hostinfo)) { 669           $host = $hostinfo[1]; 670           $port = $hostinfo[2]; 671         } else { 672           $host = $hosts[$index]; 673           $port = $this->Port; 674         } 675  676         $tls = ($this->SMTPSecure == ‘tls‘); 677         $ssl = ($this->SMTPSecure == ‘ssl‘); 678  679         if ($this->smtp->Connect(($ssl ? ‘ssl://‘:‘‘).$host, $port, $this->Timeout)) { 680  681           $hello = ($this->Helo != ‘‘ ? $this->Helo : $this->ServerHostname()); 682           $this->smtp->Hello($hello); 683  684           if ($tls) { 685             if (!$this->smtp->StartTLS()) { 686               throw new phpmailerException($this->Lang(‘tls‘)); 687             } 688  689             //We must resend HELO after tls negotiation 690             $this->smtp->Hello($hello); 691           } 692  693           $connection = true; 694           if ($this->SMTPAuth) { 695             if (!$this->smtp->Authenticate($this->Username, $this->Password)) { 696               throw new phpmailerException($this->Lang(‘authenticate‘)); 697             } 698           } 699         } 700         $index++; 701         if (!$connection) { 702           throw new phpmailerException($this->Lang(‘connect_host‘)); 703         } 704       } 705     } catch (phpmailerException $e) { 706       $this->smtp->Reset(); 707       throw $e; 708     } 709     return true; 710   } 711  712   /** 713    * Closes the active SMTP session if one exists. 714    * @return void 715    */ 716   public function SmtpClose() { 717     if(!is_null($this->smtp)) { 718       if($this->smtp->Connected()) { 719         $this->smtp->Quit(); 720         $this->smtp->Close(); 721       } 722     } 723   } 724  725   /** 726   * Sets the language for all class error messages. 727   * Returns false if it cannot load the language file.  The default language is English. 728   * @param string $langcode ISO 639-1 2-character language code (e.g. Portuguese: "br") 729   * @param string $lang_path Path to the language file directory 730   * @access public 731   */ 732   function SetLanguage($langcode = ‘en‘, $lang_path = ‘language/‘) { 733     //Define full set of translatable strings 734     $PHPMAILER_LANG = array( 735       ‘provide_address‘ => ‘You must provide at least one recipient email address.‘, 736       ‘mailer_not_supported‘ => ‘ mailer is not supported.‘, 737       ‘execute‘ => ‘Could not execute: ‘, 738       ‘instantiate‘ => ‘Could not instantiate mail function.‘, 739       ‘authenticate‘ => ‘SMTP Error: Could not authenticate.‘, 740       ‘from_failed‘ => ‘The following From address failed: ‘, 741       ‘recipients_failed‘ => ‘SMTP Error: The following recipients failed: ‘, 742       ‘data_not_accepted‘ => ‘SMTP Error: Data not accepted.‘, 743       ‘connect_host‘ => ‘SMTP Error: Could not connect to SMTP host.‘, 744       ‘file_access‘ => ‘Could not access file: ‘, 745       ‘file_open‘ => ‘File Error: Could not open file: ‘, 746       ‘encoding‘ => ‘Unknown encoding: ‘, 747       ‘signing‘ => ‘Signing Error: ‘, 748       ‘smtp_error‘ => ‘SMTP server error: ‘, 749       ‘empty_message‘ => ‘Message body empty‘, 750       ‘invalid_address‘ => ‘Invalid address‘, 751       ‘variable_set‘ => ‘Cannot set or reset variable: ‘ 752     ); 753     //Overwrite language-specific strings. This way we‘ll never have missing translations - no more "language string failed to load"! 754     $l = true; 755     if ($langcode != ‘en‘) { //There is no English translation file 756       $l = @include $lang_path.‘phpmailer.lang-‘.$langcode.‘.php‘; 757     } 758     $this->language = $PHPMAILER_LANG; 759     return ($l == true); //Returns false if language not found 760   } 761  762   /** 763   * Return the current array of language strings 764   * @return array 765   */ 766   public function GetTranslations() { 767     return $this->language; 768   } 769  770   ///////////////////////////////////////////////// 771   // METHODS, MESSAGE CREATION 772   ///////////////////////////////////////////////// 773  774   /** 775    * Creates recipient headers. 776    * @access public 777    * @return string 778    */ 779   public function AddrAppend($type, $addr) { 780     $addr_str = $type . ‘: ‘; 781     $addresses = array(); 782     foreach ($addr as $a) { 783       $addresses[] = $this->AddrFormat($a); 784     } 785     $addr_str .= implode(‘, ‘, $addresses); 786     $addr_str .= $this->LE; 787  788     return $addr_str; 789   } 790  791   /** 792    * Formats an address correctly. 793    * @access public 794    * @return string 795    */ 796   public function AddrFormat($addr) { 797     if (empty($addr[1])) { 798       return $this->SecureHeader($addr[0]); 799     } else { 800       return $this->EncodeHeader($this->SecureHeader($addr[1]), ‘phrase‘) . " <" . $this->SecureHeader($addr[0]) . ">"; 801     } 802   } 803  804   /** 805    * Wraps message for use with mailers that do not 806    * automatically perform wrapping and for quoted-printable. 807    * Original written by philippe. 808    * @param string $message The message to wrap 809    * @param integer $length The line length to wrap to 810    * @param boolean $qp_mode Whether to run in Quoted-Printable mode 811    * @access public 812    * @return string 813    */ 814   public function WrapText($message, $length, $qp_mode = false) { 815     $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE; 816     // If utf-8 encoding is used, we will need to make sure we don‘t 817     // split multibyte characters when we wrap 818     $is_utf8 = (strtolower($this->CharSet) == "utf-8"); 819  820     $message = $this->FixEOL($message); 821     if (substr($message, -1) == $this->LE) { 822       $message = substr($message, 0, -1); 823     } 824  825     $line = explode($this->LE, $message); 826     $message = ‘‘; 827     for ($i=0 ;$i < count($line); $i++) { 828       $line_part = explode(‘ ‘, $line[$i]); 829       $buf = ‘‘; 830       for ($e = 0; $e<count($line_part); $e++) { 831         $word = $line_part[$e]; 832         if ($qp_mode and (strlen($word) > $length)) { 833           $space_left = $length - strlen($buf) - 1; 834           if ($e != 0) { 835             if ($space_left > 20) { 836               $len = $space_left; 837               if ($is_utf8) { 838                 $len = $this->UTF8CharBoundary($word, $len); 839               } elseif (substr($word, $len - 1, 1) == "=") { 840                 $len--; 841               } elseif (substr($word, $len - 2, 1) == "=") { 842                 $len -= 2; 843               } 844               $part = substr($word, 0, $len); 845               $word = substr($word, $len); 846               $buf .= ‘ ‘ . $part; 847               $message .= $buf . sprintf("=%s", $this->LE); 848             } else { 849               $message .= $buf . $soft_break; 850             } 851             $buf = ‘‘; 852           } 853           while (strlen($word) > 0) { 854             $len = $length; 855             if ($is_utf8) { 856               $len = $this->UTF8CharBoundary($word, $len); 857             } elseif (substr($word, $len - 1, 1) == "=") { 858               $len--; 859             } elseif (substr($word, $len - 2, 1) == "=") { 860               $len -= 2; 861             } 862             $part = substr($word, 0, $len); 863             $word = substr($word, $len); 864  865             if (strlen($word) > 0) { 866               $message .= $part . sprintf("=%s", $this->LE); 867             } else { 868               $buf = $part; 869             } 870           } 871         } else { 872           $buf_o = $buf; 873           $buf .= ($e == 0) ? $word : (‘ ‘ . $word); 874  875           if (strlen($buf) > $length and $buf_o != ‘‘) { 876             $message .= $buf_o . $soft_break; 877             $buf = $word; 878           } 879         } 880       } 881       $message .= $buf . $this->LE; 882     } 883  884     return $message; 885   } 886  887   /** 888    * Finds last character boundary prior to maxLength in a utf-8 889    * quoted (printable) encoded string. 890    * Original written by Colin Brown. 891    * @access public 892    * @param string $encodedText utf-8 QP text 893    * @param int    $maxLength   find last character boundary prior to this length 894    * @return int 895    */ 896   public function UTF8CharBoundary($encodedText, $maxLength) { 897     $foundSplitPos = false; 898     $lookBack = 3; 899     while (!$foundSplitPos) { 900       $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack); 901       $encodedCharPos = strpos($lastChunk, "="); 902       if ($encodedCharPos !== false) { 903         // Found start of encoded character byte within $lookBack block. 904         // Check the encoded byte value (the 2 chars after the ‘=‘) 905         $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2); 906         $dec = hexdec($hex); 907         if ($dec < 128) { // Single byte character. 908           // If the encoded char was found at pos 0, it will fit 909           // otherwise reduce maxLength to start of the encoded char 910           $maxLength = ($encodedCharPos == 0) ? $maxLength : 911           $maxLength - ($lookBack - $encodedCharPos); 912           $foundSplitPos = true; 913         } elseif ($dec >= 192) { // First byte of a multi byte character 914           // Reduce maxLength to split at start of character 915           $maxLength = $maxLength - ($lookBack - $encodedCharPos); 916           $foundSplitPos = true; 917         } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back 918           $lookBack += 3; 919         } 920       } else { 921         // No encoded character found 922         $foundSplitPos = true; 923       } 924     } 925     return $maxLength; 926   } 927  928  929   /** 930    * Set the body wrapping. 931    * @access public 932    * @return void 933    */ 934   public function SetWordWrap() { 935     if($this->WordWrap < 1) { 936       return; 937     } 938  939     switch($this->message_type) { 940       case ‘alt‘: 941       case ‘alt_attachments‘: 942         $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap); 943         break; 944       default: 945         $this->Body = $this->WrapText($this->Body, $this->WordWrap); 946         break; 947     } 948   } 949  950   /** 951    * Assembles message header. 952    * @access public 953    * @return string The assembled header 954    */ 955   public function CreateHeader() { 956     $result = ‘‘; 957  958     // Set the boundaries 959     $uniq_id = md5(uniqid(time())); 960     $this->boundary[1] = ‘b1_‘ . $uniq_id; 961     $this->boundary[2] = ‘b2_‘ . $uniq_id; 962  963     $result .= $this->HeaderLine(‘Date‘, self::RFCDate()); 964     if($this->Sender == ‘‘) { 965       $result .= $this->HeaderLine(‘Return-Path‘, trim($this->From)); 966     } else { 967       $result .= $this->HeaderLine(‘Return-Path‘, trim($this->Sender)); 968     } 969  970     // To be created automatically by mail() 971     if($this->Mailer != ‘mail‘) { 972       if(count($this->to) > 0) { 973         $result .= $this->AddrAppend(‘To‘, $this->to); 974       } elseif (count($this->cc) == 0) { 975         $result .= $this->HeaderLine(‘To‘, ‘undisclosed-recipients:;‘); 976       } 977     } 978  979     $from = array(); 980     $from[0][0] = trim($this->From); 981     $from[0][1] = $this->FromName; 982     $result .= $this->AddrAppend(‘From‘, $from); 983  984     // sendmail and mail() extract Cc from the header before sending 985     if(count($this->cc) > 0) { 986       $result .= $this->AddrAppend(‘Cc‘, $this->cc); 987     } 988  989     // sendmail and mail() extract Bcc from the header before sending 990     if((($this->Mailer == ‘sendmail‘) || ($this->Mailer == ‘mail‘)) && (count($this->bcc) > 0)) { 991       $result .= $this->AddrAppend(‘Bcc‘, $this->bcc); 992     } 993  994     if(count($this->ReplyTo) > 0) { 995       $result .= $this->AddrAppend(‘Reply-to‘, $this->ReplyTo); 996     } 997  998     // mail() sets the subject itself 999     if($this->Mailer != ‘mail‘) {1000       $result .= $this->HeaderLine(‘Subject‘, $this->EncodeHeader($this->SecureHeader($this->Subject)));1001     }1002 1003     if($this->MessageID != ‘‘) {1004       $result .= $this->HeaderLine(‘Message-ID‘,$this->MessageID);1005     } else {1006       $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);1007     }1008     $result .= $this->HeaderLine(‘X-Priority‘, $this->Priority);1009     $result .= $this->HeaderLine(‘X-Mailer‘, ‘PHPMailer ‘.$this->Version.‘ (phpmailer.codeworxtech.com)‘);1010 1011     if($this->ConfirmReadingTo != ‘‘) {1012       $result .= $this->HeaderLine(‘Disposition-Notification-To‘, ‘<‘ . trim($this->ConfirmReadingTo) . ‘>‘);1013     }1014 1015     // Add custom headers1016     for($index = 0; $index < count($this->CustomHeader); $index++) {1017       $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));1018     }1019     if (!$this->sign_key_file) {1020       $result .= $this->HeaderLine(‘MIME-Version‘, ‘1.0‘);1021       $result .= $this->GetMailMIME();1022     }1023 1024     return $result;1025   }1026 1027   /**1028    * Returns the message MIME.1029    * @access public1030    * @return string1031    */1032   public function GetMailMIME() {1033     $result = ‘‘;1034     switch($this->message_type) {1035       case ‘plain‘:1036         $result .= $this->HeaderLine(‘Content-Transfer-Encoding‘, $this->Encoding);1037         $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);1038         break;1039       case ‘attachments‘:1040       case ‘alt_attachments‘:1041         if($this->InlineImageExists()){1042           $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", ‘multipart/related‘, $this->LE, $this->LE, $this->boundary[1], $this->LE);1043         } else {1044           $result .= $this->HeaderLine(‘Content-Type‘, ‘multipart/mixed;‘);1045           $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . ‘"‘);1046         }1047         break;1048       case ‘alt‘:1049         $result .= $this->HeaderLine(‘Content-Type‘, ‘multipart/alternative;‘);1050         $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . ‘"‘);1051         break;1052     }1053 1054     if($this->Mailer != ‘mail‘) {1055       $result .= $this->LE.$this->LE;1056     }1057 1058     return $result;1059   }1060 1061   /**1062    * Assembles the message body.  Returns an empty string on failure.1063    * @access public1064    * @return string The assembled message body1065    */1066   public function CreateBody() {1067     $body = ‘‘;1068 1069     if ($this->sign_key_file) {1070       $body .= $this->GetMailMIME();1071     }1072 1073     $this->SetWordWrap();1074 1075     switch($this->message_type) {1076       case ‘alt‘:1077         $body .= $this->GetBoundary($this->boundary[1], ‘‘, ‘text/plain‘, ‘‘);1078         $body .= $this->EncodeString($this->AltBody, $this->Encoding);1079         $body .= $this->LE.$this->LE;1080         $body .= $this->GetBoundary($this->boundary[1], ‘‘, ‘text/html‘, ‘‘);1081         $body .= $this->EncodeString($this->Body, $this->Encoding);1082         $body .= $this->LE.$this->LE;1083         $body .= $this->EndBoundary($this->boundary[1]);1084         break;1085       case ‘plain‘:1086         $body .= $this->EncodeString($this->Body, $this->Encoding);1087         break;1088       case ‘attachments‘:1089         $body .= $this->GetBoundary($this->boundary[1], ‘‘, ‘‘, ‘‘);1090         $body .= $this->EncodeString($this->Body, $this->Encoding);1091         $body .= $this->LE;1092         $body .= $this->AttachAll();1093         break;1094       case ‘alt_attachments‘:1095         $body .= sprintf("--%s%s", $this->boundary[1], $this->LE);1096         $body .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", ‘multipart/alternative‘, $this->LE, $this->boundary[2], $this->LE.$this->LE);1097         $body .= $this->GetBoundary($this->boundary[2], ‘‘, ‘text/plain‘, ‘‘) . $this->LE; // Create text body1098         $body .= $this->EncodeString($this->AltBody, $this->Encoding);1099         $body .= $this->LE.$this->LE;1100         $body .= $this->GetBoundary($this->boundary[2], ‘‘, ‘text/html‘, ‘‘) . $this->LE; // Create the HTML body1101         $body .= $this->EncodeString($this->Body, $this->Encoding);1102         $body .= $this->LE.$this->LE;1103         $body .= $this->EndBoundary($this->boundary[2]);1104         $body .= $this->AttachAll();1105         break;1106     }1107 1108     if ($this->IsError()) {1109       $body = ‘‘;1110     } elseif ($this->sign_key_file) {1111       try {1112         $file = tempnam(‘‘, ‘mail‘);1113         file_put_contents($file, $body); //TODO check this worked1114         $signed = tempnam("", "signed");1115         if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), NULL)) {1116           @unlink($file);1117           @unlink($signed);1118           $body = file_get_contents($signed);1119         } else {1120           @unlink($file);1121           @unlink($signed);1122           throw new phpmailerException($this->Lang("signing").openssl_error_string());1123         }1124       } catch (phpmailerException $e) {1125         $body = ‘‘;1126         if ($this->exceptions) {1127           throw $e;1128         }1129       }1130     }1131 1132     return $body;1133   }1134 1135   /**1136    * Returns the start of a message boundary.1137    * @access private1138    */1139   private function GetBoundary($boundary, $charSet, $contentType, $encoding) {1140     $result = ‘‘;1141     if($charSet == ‘‘) {1142       $charSet = $this->CharSet;1143     }1144     if($contentType == ‘‘) {1145       $contentType = $this->ContentType;1146     }1147     if($encoding == ‘‘) {1148       $encoding = $this->Encoding;1149     }1150     $result .= $this->TextLine(‘--‘ . $boundary);1151     $result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);1152     $result .= $this->LE;1153     $result .= $this->HeaderLine(‘Content-Transfer-Encoding‘, $encoding);1154     $result .= $this->LE;1155 1156     return $result;1157   }1158 1159   /**1160    * Returns the end of a message boundary.1161    * @access private1162    */1163   private function EndBoundary($boundary) {1164     return $this->LE . ‘--‘ . $boundary . ‘--‘ . $this->LE;1165   }1166 1167   /**1168    * Sets the message type.1169    * @access private1170    * @return void1171    */1172   private function SetMessageType() {1173     if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {1174       $this->message_type = ‘plain‘;1175     } else {1176       if(count($this->attachment) > 0) {1177         $this->message_type = ‘attachments‘;1178       }1179       if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {1180         $this->message_type = ‘alt‘;1181       }1182       if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {1183         $this->message_type = ‘alt_attachments‘;1184       }1185     }1186   }1187 1188   /**1189    *  Returns a formatted header line.1190    * @access public1191    * @return string1192    */1193   public function HeaderLine($name, $value) {1194     return $name . ‘: ‘ . $value . $this->LE;1195   }1196 1197   /**1198    * Returns a formatted mail line.1199    * @access public1200    * @return string1201    */1202   public function TextLine($value) {1203     return $value . $this->LE;1204   }1205 1206   /////////////////////////////////////////////////1207   // CLASS METHODS, ATTACHMENTS1208   /////////////////////////////////////////////////1209 1210   /**1211    * Adds an attachment from a path on the filesystem.1212    * Returns false if the file could not be found1213    * or accessed.1214    * @param string $path Path to the attachment.1215    * @param string $name Overrides the attachment name.1216    * @param string $encoding File encoding (see $Encoding).1217    * @param string $type File extension (MIME) type.1218    * @return bool1219    */1220   public function AddAttachment($path, $name = ‘‘, $encoding = ‘base64‘, $type = ‘application/octet-stream‘) {1221     try {1222       if ( !@is_file($path) ) {1223         throw new phpmailerException($this->Lang(‘file_access‘) . $path, self::STOP_CONTINUE);1224       }1225       $filename = basename($path);1226       if ( $name == ‘‘ ) {1227         $name = $filename;1228       }1229 1230       $this->attachment[] = array(1231         0 => $path,1232         1 => $filename,1233         2 => $name,1234         3 => $encoding,1235         4 => $type,1236         5 => false,  // isStringAttachment1237         6 => ‘attachment‘,1238         7 => 01239       );1240 1241     } catch (phpmailerException $e) {1242       $this->SetError($e->getMessage());1243       if ($this->exceptions) {1244         throw $e;1245       }1246       echo $e->getMessage()."\n";1247       if ( $e->getCode() == self::STOP_CRITICAL ) {1248         return false;1249       }1250     }1251     return true;1252   }1253 1254   /**1255   * Return the current array of attachments1256   * @return array1257   */1258   public function GetAttachments() {1259     return $this->attachment;1260   }1261 1262   /**1263    * Attaches all fs, string, and binary attachments to the message.1264    * Returns an empty string on failure.1265    * @access private1266    * @return string1267    */1268   private function AttachAll() {1269     // Return text of body1270     $mime = array();1271     $cidUniq = array();1272     $incl = array();1273 1274     // Add all attachments1275     foreach ($this->attachment as $attachment) {1276       // Check for string attachment1277       $bString = $attachment[5];1278       if ($bString) {1279         $string = $attachment[0];1280       } else {1281         $path = $attachment[0];1282       }1283 1284       if (in_array($attachment[0], $incl)) { continue; }1285       $filename    = $attachment[1];1286       $name        = $attachment[2];1287       $encoding    = $attachment[3];1288       $type        = $attachment[4];1289       $disposition = $attachment[6];1290       $cid         = $attachment[7];1291       $incl[]      = $attachment[0];1292       if ( $disposition == ‘inline‘ && isset($cidUniq[$cid]) ) { continue; }1293       $cidUniq[$cid] = true;1294 1295       $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);1296       $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);1297       $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);1298 1299       if($disposition == ‘inline‘) {1300         $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);1301       }1302 1303       $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);1304 1305       // Encode as string attachment1306       if($bString) {1307         $mime[] = $this->EncodeString($string, $encoding);1308         if($this->IsError()) {1309           return ‘‘;1310         }1311         $mime[] = $this->LE.$this->LE;1312       } else {1313         $mime[] = $this->EncodeFile($path, $encoding);1314         if($this->IsError()) {1315           return ‘‘;1316         }1317         $mime[] = $this->LE.$this->LE;1318       }1319     }1320 1321     $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);1322 1323     return join(‘‘, $mime);1324   }1325 1326   /**1327    * Encodes attachment in requested format.1328    * Returns an empty string on failure.1329    * @param string $path The full path to the file1330    * @param string $encoding The encoding to use; one of ‘base64‘, ‘7bit‘, ‘8bit‘, ‘binary‘, ‘quoted-printable‘1331    * @see EncodeFile()1332    * @access private1333    * @return string1334    */1335   private function EncodeFile($path, $encoding = ‘base64‘) {1336     try {1337       if (!is_readable($path)) {1338         throw new phpmailerException($this->Lang(‘file_open‘) . $path, self::STOP_CONTINUE);1339       }1340       if (function_exists(‘get_magic_quotes‘)) {1341         function get_magic_quotes() {1342           return false;1343         }1344       }1345       if (PHP_VERSION < 6) {1346         $magic_quotes = get_magic_quotes_runtime();1347         set_magic_quotes_runtime(0);1348       }1349       $file_buffer  = file_get_contents($path);1350       $file_buffer  = $this->EncodeString($file_buffer, $encoding);1351       if (PHP_VERSION < 6) { set_magic_quotes_runtime($magic_quotes); }1352       return $file_buffer;1353     } catch (Exception $e) {1354       $this->SetError($e->getMessage());1355       return ‘‘;1356     }1357   }1358 1359   /**1360    * Encodes string to requested format.1361    * Returns an empty string on failure.1362    * @param string $str The text to encode1363    * @param string $encoding The encoding to use; one of ‘base64‘, ‘7bit‘, ‘8bit‘, ‘binary‘, ‘quoted-printable‘1364    * @access public1365    * @return string1366    */1367   public function EncodeString ($str, $encoding = ‘base64‘) {1368     $encoded = ‘‘;1369     switch(strtolower($encoding)) {1370       case ‘base64‘:1371         $encoded = chunk_split(base64_encode($str), 76, $this->LE);1372         break;1373       case ‘7bit‘:1374       case ‘8bit‘:1375         $encoded = $this->FixEOL($str);1376         //Make sure it ends with a line break1377         if (substr($encoded, -(strlen($this->LE))) != $this->LE)1378           $encoded .= $this->LE;1379         break;1380       case ‘binary‘:1381         $encoded = $str;1382         break;1383       case ‘quoted-printable‘:1384         $encoded = $this->EncodeQP($str);1385         break;1386       default:1387         $this->SetError($this->Lang(‘encoding‘) . $encoding);1388         break;1389     }1390     return $encoded;1391   }1392 1393   /**1394    * Encode a header string to best (shortest) of Q, B, quoted or none.1395    * @access public1396    * @return string1397    */1398   public function EncodeHeader($str, $position = ‘text‘) {1399     $x = 0;1400 1401     switch (strtolower($position)) {1402       case ‘phrase‘:1403         if (!preg_match(‘/[\200-\377]/‘, $str)) {1404           // Can‘t use addslashes as we don‘t know what value has magic_quotes_sybase1405           $encoded = addcslashes($str, "\0..\37\177\\\"");1406           if (($str == $encoded) && !preg_match(‘/[^A-Za-z0-9!#$%&\‘*+\/=?^_`{|}~ -]/‘, $str)) {1407             return ($encoded);1408           } else {1409             return ("\"$encoded\"");1410           }1411         }1412         $x = preg_match_all(‘/[^\040\041\043-\133\135-\176]/‘, $str, $matches);1413         break;1414       case ‘comment‘:1415         $x = preg_match_all(‘/[()"]/‘, $str, $matches);1416         // Fall-through1417       case ‘text‘:1418       default:1419         $x += preg_match_all(‘/[\000-\010\013\014\016-\037\177-\377]/‘, $str, $matches);1420         break;1421     }1422 1423     if ($x == 0) {1424       return ($str);1425     }1426 1427     $maxlen = 75 - 7 - strlen($this->CharSet);1428     // Try to select the encoding which should produce the shortest output1429     if (strlen($str)/3 < $x) {1430       $encoding = ‘B‘;1431       if (function_exists(‘mb_strlen‘) && $this->HasMultiBytes($str)) {1432         // Use a custom function which correctly encodes and wraps long1433         // multibyte strings without breaking lines within a character1434         $encoded = $this->Base64EncodeWrapMB($str);1435       } else {1436         $encoded = base64_encode($str);1437         $maxlen -= $maxlen % 4;1438         $encoded = trim(chunk_split($encoded, $maxlen, "\n"));1439       }1440     } else {1441       $encoding = ‘Q‘;1442       $encoded = $this->EncodeQ($str, $position);1443       $encoded = $this->WrapText($encoded, $maxlen, true);1444       $encoded = str_replace(‘=‘.$this->LE, "\n", trim($encoded));1445     }1446 1447     $encoded = preg_replace(‘/^(.*)$/m‘, " =?".$this->CharSet."?$encoding?\\1?=", $encoded);1448     $encoded = trim(str_replace("\n", $this->LE, $encoded));1449 1450     return $encoded;1451   }1452 1453   /**1454    * Checks if a string contains multibyte characters.1455    * @access public1456    * @param string $str multi-byte text to wrap encode1457    * @return bool1458    */1459   public function HasMultiBytes($str) {1460     if (function_exists(‘mb_strlen‘)) {1461       return (strlen($str) > mb_strlen($str, $this->CharSet));1462     } else { // Assume no multibytes (we can‘t handle without mbstring functions anyway)1463       return false;1464     }1465   }1466 1467   /**1468    * Correctly encodes and wraps long multibyte strings for mail headers1469    * without breaking lines within a character.1470    * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php1471    * @access public1472    * @param string $str multi-byte text to wrap encode1473    * @return string1474    */1475   public function Base64EncodeWrapMB($str) {1476     $start = "=?".$this->CharSet."?B?";1477     $end = "?=";1478     $encoded = "";1479 1480     $mb_length = mb_strlen($str, $this->CharSet);1481     // Each line must have length <= 75, including $start and $end1482     $length = 75 - strlen($start) - strlen($end);1483     // Average multi-byte ratio1484     $ratio = $mb_length / strlen($str);1485     // Base64 has a 4:3 ratio1486     $offset = $avgLength = floor($length * $ratio * .75);1487 1488     for ($i = 0; $i < $mb_length; $i += $offset) {1489       $lookBack = 0;1490 1491       do {1492         $offset = $avgLength - $lookBack;1493         $chunk = mb_substr($str, $i, $offset, $this->CharSet);1494         $chunk = base64_encode($chunk);1495         $lookBack++;1496       }1497       while (strlen($chunk) > $length);1498 1499       $encoded .= $chunk . $this->LE;1500     }1501 1502     // Chomp the last linefeed1503     $encoded = substr($encoded, 0, -strlen($this->LE));1504     return $encoded;1505   }1506 1507   /**1508   * Encode string to quoted-printable.1509   * Only uses standard PHP, slow, but will always work1510   * @access public1511   * @param string $string the text to encode1512   * @param integer $line_max Number of chars allowed on a line before wrapping1513   * @return string1514   */1515   public function EncodeQPphp( $input = ‘‘, $line_max = 76, $space_conv = false) {1516     $hex = array(‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘);1517     $lines = preg_split(‘/(?:\r\n|\r|\n)/‘, $input);1518     $eol = "\r\n";1519     $escape = ‘=‘;1520     $output = ‘‘;1521     while( list(, $line) = each($lines) ) {1522       $linlen = strlen($line);1523       $newline = ‘‘;1524       for($i = 0; $i < $linlen; $i++) {1525         $c = substr( $line, $i, 1 );1526         $dec = ord( $c );1527         if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E1528           $c = ‘=2E‘;1529         }1530         if ( $dec == 32 ) {1531           if ( $i == ( $linlen - 1 ) ) { // convert space at eol only1532             $c = ‘=20‘;1533           } else if ( $space_conv ) {1534             $c = ‘=20‘;1535           }1536         } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required1537           $h2 = floor($dec/16);1538           $h1 = floor($dec%16);1539           $c = $escape.$hex[$h2].$hex[$h1];1540         }1541         if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted1542           $output .= $newline.$escape.$eol; //  soft line break; " =\r\n" is okay1543           $newline = ‘‘;1544           // check if newline first character will be point or not1545           if ( $dec == 46 ) {1546             $c = ‘=2E‘;1547           }1548         }1549         $newline .= $c;1550       } // end of for1551       $output .= $newline.$eol;1552     } // end of while1553     return $output;1554   }1555 1556   /**1557   * Encode string to RFC2045 (6.7) quoted-printable format1558   * Uses a PHP5 stream filter to do the encoding about 64x faster than the old version1559   * Also results in same content as you started with after decoding1560   * @see EncodeQPphp()1561   * @access public1562   * @param string $string the text to encode1563   * @param integer $line_max Number of chars allowed on a line before wrapping1564   * @param boolean $space_conv Dummy param for compatibility with existing EncodeQP function1565   * @return string1566   * @author Marcus Bointon1567   */1568   public function EncodeQP($string, $line_max = 76, $space_conv = false) {1569     if (function_exists(‘quoted_printable_encode‘)) { //Use native function if it‘s available (>= PHP5.3)1570       return quoted_printable_encode($string);1571     }1572     $filters = stream_get_filters();1573     if (!in_array(‘convert.*‘, $filters)) { //Got convert stream filter?1574       return $this->EncodeQPphp($string, $line_max, $space_conv); //Fall back to old implementation1575     }1576     $fp = fopen(‘php://temp/‘, ‘r+‘);1577     $string = preg_replace(‘/\r\n?/‘, $this->LE, $string); //Normalise line breaks1578     $params = array(‘line-length‘ => $line_max, ‘line-break-chars‘ => $this->LE);1579     $s = stream_filter_append($fp, ‘convert.quoted-printable-encode‘, STREAM_FILTER_READ, $params);1580     fputs($fp, $string);1581     rewind($fp);1582     $out = stream_get_contents($fp);1583     stream_filter_remove($s);1584     $out = preg_replace(‘/^\./m‘, ‘=2E‘, $out); //Encode . if it is first char on a line, workaround for bug in Exchange1585     fclose($fp);1586     return $out;1587   }1588 1589   /**1590    * Encode string to q encoding.1591    * @link http://tools.ietf.org/html/rfc20471592    * @param string $str the text to encode1593    * @param string $position Where the text is going to be used, see the RFC for what that means1594    * @access public1595    * @return string1596    */1597   public function EncodeQ ($str, $position = ‘text‘) {1598     // There should not be any EOL in the string1599     $encoded = preg_replace(‘/[\r\n]*/‘, ‘‘, $str);1600 1601     switch (strtolower($position)) {1602       case ‘phrase‘:1603         $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "‘=‘.sprintf(‘%02X‘, ord(‘\\1‘))", $encoded);1604         break;1605       case ‘comment‘:1606         $encoded = preg_replace("/([\(\)\"])/e", "‘=‘.sprintf(‘%02X‘, ord(‘\\1‘))", $encoded);1607       case ‘text‘:1608       default:1609         // Replace every high ascii, control =, ? and _ characters1610         //TODO using /e (equivalent to eval()) is probably not a good idea1611         $encoded = preg_replace(‘/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e‘,1612               "‘=‘.sprintf(‘%02X‘, ord(‘\\1‘))", $encoded);1613         break;1614     }1615 1616     // Replace every spaces to _ (more readable than =20)1617     $encoded = str_replace(‘ ‘, ‘_‘, $encoded);1618 1619     return $encoded;1620   }1621 1622   /**1623    * Adds a string or binary attachment (non-filesystem) to the list.1624    * This method can be used to attach ascii or binary data,1625    * such as a BLOB record from a database.1626    * @param string $string String attachment data.1627    * @param string $filename Name of the attachment.1628    * @param string $encoding File encoding (see $Encoding).1629    * @param string $type File extension (MIME) type.1630    * @return void1631    */1632   public function AddStringAttachment($string, $filename, $encoding = ‘base64‘, $type = ‘application/octet-stream‘) {1633     // Append to $attachment array1634     $this->attachment[] = array(1635       0 => $string,1636       1 => $filename,1637       2 => $filename,1638       3 => $encoding,1639       4 => $type,1640       5 => true,  // isStringAttachment1641       6 => ‘attachment‘,1642       7 => 01643     );1644   }1645 1646   /**1647    * Adds an embedded attachment.  This can include images, sounds, and1648    * just about any other document.  Make sure to set the $type to an1649    * image type.  For JPEG images use "image/jpeg" and for GIF images1650    * use "image/gif".1651    * @param string $path Path to the attachment.1652    * @param string $cid Content ID of the attachment.  Use this to identify1653    *        the Id for accessing the image in an HTML form.1654    * @param string $name Overrides the attachment name.1655    * @param string $encoding File encoding (see $Encoding).1656    * @param string $type File extension (MIME) type.1657    * @return bool1658    */1659   public function AddEmbeddedImage($path, $cid, $name = ‘‘, $encoding = ‘base64‘, $type = ‘application/octet-stream‘) {1660 1661     if ( !@is_file($path) ) {1662       $this->SetError($this->Lang(‘file_access‘) . $path);1663       return false;1664     }1665 1666     $filename = basename($path);1667     if ( $name == ‘‘ ) {1668       $name = $filename;1669     }1670 1671     // Append to $attachment array1672     $this->attachment[] = array(1673       0 => $path,1674       1 => $filename,1675       2 => $name,1676       3 => $encoding,1677       4 => $type,1678       5 => false,  // isStringAttachment1679       6 => ‘inline‘,1680       7 => $cid1681     );1682 1683     return true;1684   }1685 1686   /**1687    * Returns true if an inline attachment is present.1688    * @access public1689    * @return bool1690    */1691   public function InlineImageExists() {1692     foreach($this->attachment as $attachment) {1693       if ($attachment[6] == ‘inline‘) {1694         return true;1695       }1696     }1697     return false;1698   }1699 1700   /////////////////////////////////////////////////1701   // CLASS METHODS, MESSAGE RESET1702   /////////////////////////////////////////////////1703 1704   /**1705    * Clears all recipients assigned in the TO array.  Returns void.1706    * @return void1707    */1708   public function ClearAddresses() {1709     foreach($this->to as $to) {1710       unset($this->all_recipients[strtolower($to[0])]);1711     }1712     $this->to = array();1713   }1714 1715   /**1716    * Clears all recipients assigned in the CC array.  Returns void.1717    * @return void1718    */1719   public function ClearCCs() {1720     foreach($this->cc as $cc) {1721       unset($this->all_recipients[strtolower($cc[0])]);1722     }1723     $this->cc = array();1724   }1725 1726   /**1727    * Clears all recipients assigned in the BCC array.  Returns void.1728    * @return void1729    */1730   public function ClearBCCs() {1731     foreach($this->bcc as $bcc) {1732       unset($this->all_recipients[strtolower($bcc[0])]);1733     }1734     $this->bcc = array();1735   }1736 1737   /**1738    * Clears all recipients assigned in the ReplyTo array.  Returns void.1739    * @return void1740    */1741   public function ClearReplyTos() {1742     $this->ReplyTo = array();1743   }1744 1745   /**1746    * Clears all recipients assigned in the TO, CC and BCC1747    * array.  Returns void.1748    * @return void1749    */1750   public function ClearAllRecipients() {1751     $this->to = array();1752     $this->cc = array();1753     $this->bcc = array();1754     $this->all_recipients = array();1755   }1756 1757   /**1758    * Clears all previously set filesystem, string, and binary1759    * attachments.  Returns void.1760    * @return void1761    */1762   public function ClearAttachments() {1763     $this->attachment = array();1764   }1765 1766   /**1767    * Clears all custom headers.  Returns void.1768    * @return void1769    */1770   public function ClearCustomHeaders() {1771     $this->CustomHeader = array();1772   }1773 1774   /////////////////////////////////////////////////1775   // CLASS METHODS, MISCELLANEOUS1776   /////////////////////////////////////////////////1777 1778   /**1779    * Adds the error message to the error container.1780    * @access protected1781    * @return void1782    */1783   protected function SetError($msg) {1784     $this->error_count++;1785     if ($this->Mailer == ‘smtp‘ and !is_null($this->smtp)) {1786       $lasterror = $this->smtp->getError();1787       if (!empty($lasterror) and array_key_exists(‘smtp_msg‘, $lasterror)) {1788         $msg .= ‘<p>‘ . $this->Lang(‘smtp_error‘) . $lasterror[‘smtp_msg‘] . "</p>\n";1789       }1790     }1791     $this->ErrorInfo = $msg;1792   }1793 1794   /**1795    * Returns the proper RFC 822 formatted date.1796    * @access public1797    * @return string1798    * @static1799    */1800   public static function RFCDate() {1801     $tz = date(‘Z‘);1802     $tzs = ($tz < 0) ? ‘-‘ : ‘+‘;1803     $tz = abs($tz);1804     $tz = (int)($tz/3600)*100 + ($tz%3600)/60;1805     $result = sprintf("%s %s%04d", date(‘D, j M Y H:i:s‘), $tzs, $tz);1806 1807     return $result;1808   }1809 1810   /**1811    * Returns the server hostname or ‘localhost.localdomain‘ if unknown.1812    * @access private1813    * @return string1814    */1815   private function ServerHostname() {1816     if (!empty($this->Hostname)) {1817       $result = $this->Hostname;1818     } elseif (isset($_SERVER[‘SERVER_NAME‘])) {1819       $result = $_SERVER[‘SERVER_NAME‘];1820     } else {1821       $result = ‘localhost.localdomain‘;1822     }1823 1824     return $result;1825   }1826 1827   /**1828    * Returns a message in the appropriate language.1829    * @access private1830    * @return string1831    */1832   private function Lang($key) {1833     if(count($this->language) < 1) {1834       $this->SetLanguage(‘en‘); // set the default language1835     }1836 1837     if(isset($this->language[$key])) {1838       return $this->language[$key];1839     } else {1840       return ‘Language string failed to load: ‘ . $key;1841     }1842   }1843 1844   /**1845    * Returns true if an error occurred.1846    * @access public1847    * @return bool1848    */1849   public function IsError() {1850     return ($this->error_count > 0);1851   }1852 1853   /**1854    * Changes every end of line from CR or LF to CRLF.1855    * @access private1856    * @return string1857    */1858   private function FixEOL($str) {1859     $str = str_replace("\r\n", "\n", $str);1860     $str = str_replace("\r", "\n", $str);1861     $str = str_replace("\n", $this->LE, $str);1862     return $str;1863   }1864 1865   /**1866    * Adds a custom header.1867    * @access public1868    * @return void1869    */1870   public function AddCustomHeader($custom_header) {1871     $this->CustomHeader[] = explode(‘:‘, $custom_header, 2);1872   }1873 1874   /**1875    * Evaluates the message and returns modifications for inline images and backgrounds1876    * @access public1877    * @return $message1878    */1879   public function MsgHTML($message, $basedir = ‘‘) {1880     preg_match_all("/(src|background)=\"(.*)\"/Ui", $message, $images);1881     if(isset($images[2])) {1882       foreach($images[2] as $i => $url) {1883         // do not change urls for absolute images (thanks to corvuscorax)1884         if (!preg_match(‘#^[A-z]+://#‘,$url)) {1885           $filename = basename($url);1886           $directory = dirname($url);1887           ($directory == ‘.‘)?$directory=‘‘:‘‘;1888           $cid = ‘cid:‘ . md5($filename);1889           $ext = pathinfo($filename, PATHINFO_EXTENSION);1890           $mimeType  = self::_mime_types($ext);1891           if ( strlen($basedir) > 1 && substr($basedir,-1) != ‘/‘) { $basedir .= ‘/‘; }1892           if ( strlen($directory) > 1 && substr($directory,-1) != ‘/‘) { $directory .= ‘/‘; }1893           if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, ‘base64‘,$mimeType) ) {1894             $message = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, ‘/‘)."\"/Ui", $images[1][$i]."=\"".$cid."\"", $message);1895           }1896         }1897       }1898     }1899     $this->IsHTML(true);1900     $this->Body = $message;1901     $textMsg = trim(strip_tags(preg_replace(‘/<(head|title|style|script)[^>]*>.*?<\/\\1>/s‘,‘‘,$message)));1902     if (!empty($textMsg) && empty($this->AltBody)) {1903       $this->AltBody = html_entity_decode($textMsg);1904     }1905     if (empty($this->AltBody)) {1906       $this->AltBody = ‘To view this email message, open it in a program that understands HTML!‘ . "\n\n";1907     }1908   }1909 1910   /**1911    * Gets the MIME type of the embedded or inline image1912    * @param string File extension1913    * @access public1914    * @return string MIME type of ext1915    * @static1916    */1917   public static function _mime_types($ext = ‘‘) {1918     $mimes = array(1919       ‘hqx‘   =>  ‘application/mac-binhex40‘,1920       ‘cpt‘   =>  ‘application/mac-compactpro‘,1921       ‘doc‘   =>  ‘application/msword‘,1922       ‘bin‘   =>  ‘application/macbinary‘,1923       ‘dms‘   =>  ‘application/octet-stream‘,1924       ‘lha‘   =>  ‘application/octet-stream‘,1925       ‘lzh‘   =>  ‘application/octet-stream‘,1926       ‘exe‘   =>  ‘application/octet-stream‘,1927       ‘class‘ =>  ‘application/octet-stream‘,1928       ‘psd‘   =>  ‘application/octet-stream‘,1929       ‘so‘    =>  ‘application/octet-stream‘,1930       ‘sea‘   =>  ‘application/octet-stream‘,1931       ‘dll‘   =>  ‘application/octet-stream‘,1932       ‘oda‘   =>  ‘application/oda‘,1933       ‘pdf‘   =>  ‘application/pdf‘,1934       ‘ai‘    =>  ‘application/postscript‘,1935       ‘eps‘   =>  ‘application/postscript‘,1936       ‘ps‘    =>  ‘application/postscript‘,1937       ‘smi‘   =>  ‘application/smil‘,1938       ‘smil‘  =>  ‘application/smil‘,1939       ‘mif‘   =>  ‘application/vnd.mif‘,1940       ‘xls‘   =>  ‘application/vnd.ms-excel‘,1941       ‘ppt‘   =>  ‘application/vnd.ms-powerpoint‘,1942       ‘wbxml‘ =>  ‘application/vnd.wap.wbxml‘,1943       ‘wmlc‘  =>  ‘application/vnd.wap.wmlc‘,1944       ‘dcr‘   =>  ‘application/x-director‘,1945       ‘dir‘   =>  ‘application/x-director‘,1946       ‘dxr‘   =>  ‘application/x-director‘,1947       ‘dvi‘   =>  ‘application/x-dvi‘,1948       ‘gtar‘  =>  ‘application/x-gtar‘,1949       ‘php‘   =>  ‘application/x-httpd-php‘,1950       ‘php4‘  =>  ‘application/x-httpd-php‘,1951       ‘php3‘  =>  ‘application/x-httpd-php‘,1952       ‘phtml‘ =>  ‘application/x-httpd-php‘,1953       ‘phps‘  =>  ‘application/x-httpd-php-source‘,1954       ‘js‘    =>  ‘application/x-javascript‘,1955       ‘swf‘   =>  ‘application/x-shockwave-flash‘,1956       ‘sit‘   =>  ‘application/x-stuffit‘,1957       ‘tar‘   =>  ‘application/x-tar‘,1958       ‘tgz‘   =>  ‘application/x-tar‘,1959       ‘xhtml‘ =>  ‘application/xhtml+xml‘,1960       ‘xht‘   =>  ‘application/xhtml+xml‘,1961       ‘zip‘   =>  ‘application/zip‘,1962       ‘mid‘   =>  ‘audio/midi‘,1963       ‘midi‘  =>  ‘audio/midi‘,1964       ‘mpga‘  =>  ‘audio/mpeg‘,1965       ‘mp2‘   =>  ‘audio/mpeg‘,1966       ‘mp3‘   =>  ‘audio/mpeg‘,1967       ‘aif‘   =>  ‘audio/x-aiff‘,1968       ‘aiff‘  =>  ‘audio/x-aiff‘,1969       ‘aifc‘  =>  ‘audio/x-aiff‘,1970       ‘ram‘   =>  ‘audio/x-pn-realaudio‘,1971       ‘rm‘    =>  ‘audio/x-pn-realaudio‘,1972       ‘rpm‘   =>  ‘audio/x-pn-realaudio-plugin‘,1973       ‘ra‘    =>  ‘audio/x-realaudio‘,1974       ‘rv‘    =>  ‘video/vnd.rn-realvideo‘,1975       ‘wav‘   =>  ‘audio/x-wav‘,1976       ‘bmp‘   =>  ‘image/bmp‘,1977       ‘gif‘   =>  ‘image/gif‘,1978       ‘jpeg‘  =>  ‘image/jpeg‘,1979       ‘jpg‘   =>  ‘image/jpeg‘,1980       ‘jpe‘   =>  ‘image/jpeg‘,1981       ‘png‘   =>  ‘image/png‘,1982       ‘tiff‘  =>  ‘image/tiff‘,1983       ‘tif‘   =>  ‘image/tiff‘,1984       ‘css‘   =>  ‘text/css‘,1985       ‘html‘  =>  ‘text/html‘,1986       ‘htm‘   =>  ‘text/html‘,1987       ‘shtml‘ =>  ‘text/html‘,1988       ‘txt‘   =>  ‘text/plain‘,1989       ‘text‘  =>  ‘text/plain‘,1990       ‘log‘   =>  ‘text/plain‘,1991       ‘rtx‘   =>  ‘text/richtext‘,1992       ‘rtf‘   =>  ‘text/rtf‘,1993       ‘xml‘   =>  ‘text/xml‘,1994       ‘xsl‘   =>  ‘text/xml‘,1995       ‘mpeg‘  =>  ‘video/mpeg‘,1996       ‘mpg‘   =>  ‘video/mpeg‘,1997       ‘mpe‘   =>  ‘video/mpeg‘,1998       ‘qt‘    =>  ‘video/quicktime‘,1999       ‘mov‘   =>  ‘video/quicktime‘,2000       ‘avi‘   =>  ‘video/x-msvideo‘,2001       ‘movie‘ =>  ‘video/x-sgi-movie‘,2002       ‘doc‘   =>  ‘application/msword‘,2003       ‘word‘  =>  ‘application/msword‘,2004       ‘xl‘    =>  ‘application/excel‘,2005       ‘eml‘   =>  ‘message/rfc822‘2006     );2007     return (!isset($mimes[strtolower($ext)])) ? ‘application/octet-stream‘ : $mimes[strtolower($ext)];2008   }2009 2010   /**2011   * Set (or reset) Class Objects (variables)2012   *2013   * Usage Example:2014   * $page->set(‘X-Priority‘, ‘3‘);2015   *2016   * @access public2017   * @param string $name Parameter Name2018   * @param mixed $value Parameter Value2019   * NOTE: will not work with arrays, there are no arrays to set/reset2020   * @todo Should this not be using __set() magic function?2021   */2022   public function set($name, $value = ‘‘) {2023     try {2024       if (isset($this->$name) ) {2025         $this->$name = $value;2026       } else {2027         throw new phpmailerException($this->Lang(‘variable_set‘) . $name, self::STOP_CRITICAL);2028       }2029     } catch (Exception $e) {2030       $this->SetError($e->getMessage());2031       if ($e->getCode() == self::STOP_CRITICAL) {2032         return false;2033       }2034     }2035     return true;2036   }2037 2038   /**2039    * Strips newlines to prevent header injection.2040    * @access public2041    * @param string $str String2042    * @return string2043    */2044   public function SecureHeader($str) {2045     $str = str_replace("\r", ‘‘, $str);2046     $str = str_replace("\n", ‘‘, $str);2047     return trim($str);2048   }2049 2050   /**2051    * Set the private key file and password to sign the message.2052    *2053    * @access public2054    * @param string $key_filename Parameter File Name2055    * @param string $key_pass Password for private key2056    */2057   public function Sign($cert_filename, $key_filename, $key_pass) {2058     $this->sign_cert_file = $cert_filename;2059     $this->sign_key_file = $key_filename;2060     $this->sign_key_pass = $key_pass;2061   }2062 }2063 2064 2065 class phpmailerException extends Exception {2066   public function errorMessage() {2067     $errorMsg = ‘<strong>‘ . $this->getMessage() . "</strong><br />\n";2068     return $errorMsg;2069   }2070 }2071 ?>
View Code

在控制器之中写调用方法:

 1 function SendEmail($email,$title,$content) 2 { 3   import(‘Com.PHPMailer‘); 4   $mail = new \PHPMailer(); 5   $title = "密码找回"; 6   $content = "亲爱的用户 ".$username.":您好! 7       <br> 8       <br> 9         您收到这封这封电子邮件是因为您 (也可能是某人冒充您的名义) 申请了一个新的密码。假如这不是您本人所申请, 请不用理会这封电子邮件, 但是如果您持续收到这类的信件骚扰, 请您尽快联络管理员。10       <br>";11   $mail->IsSMTP();                           // tell the class to use SMTP12   $mail->SMTPAuth   = true;                  // enable SMTP authentication13   $mail->Port       = 25;                    // set the SMTP server port14   $mail->Host       = "***.163.com"; // SMTP server15   $mail->Username   = "***@163.com";     // SMTP server username16   $mail->Password   = "tbamiabtpcgyurqo";            // SMTP server password17   //$mail->IsSendmail();  // tell the class to use Sendmail18   $mail->AddReplyTo("***@163.com","test网");19   $mail->From       = "***@163.com";20   $mail->FromName   = "test";21   $mail->Subject  = $title;22   $mail->AltBody    = $title; // optional, comment out and test23   $mail->WordWrap   = 80; // set word wrap24   $mail->MsgHTML($content);25   $mail->IsHTML(true); // send as HTML26   $mail->AddAddress($email);27   if($mail->Send()){28     return true;29   }else{30     return false;31   }32 }

 

ThinkPHP发送邮件