PageRenderTime 278ms CodeModel.GetById 56ms app.highlight 121ms RepoModel.GetById 60ms app.codeStats 1ms

/libraries/phpmailer/phpmailer.php

http://github.com/joomla/joomla-platform
PHP | 2830 lines | 1650 code | 214 blank | 966 comment | 290 complexity | 07173fe1b25492dab033c11c3c3bf87a MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2/*~ class.phpmailer.php
   3.---------------------------------------------------------------------------.
   4|  Software: PHPMailer - PHP email class                                    |
   5|   Version: 5.2.2                                                          |
   6|      Site: https://code.google.com/a/apache-extras.org/p/phpmailer/       |
   7| ------------------------------------------------------------------------- |
   8|     Admin: Jim Jagielski (project admininistrator)                        |
   9|   Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
  10|          : Marcus Bointon (coolbru) coolbru@users.sourceforge.net         |
  11|          : Jim Jagielski (jimjag) jimjag@gmail.com                        |
  12|   Founder: Brent R. Matzelle (original founder)                           |
  13| Copyright (c) 2010-2012, Jim Jagielski. All Rights Reserved.              |
  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*/
  24
  25/**
  26 * PHPMailer - PHP email creation and transport class
  27 * NOTE: Requires PHP version 5 or later
  28 * @package PHPMailer
  29 * @author Andy Prevost
  30 * @author Marcus Bointon
  31 * @author Jim Jagielski
  32 * @copyright 2010 - 2012 Jim Jagielski
  33 * @copyright 2004 - 2009 Andy Prevost
  34 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  35 */
  36
  37if (version_compare(PHP_VERSION, '5.0.0', '<') ) exit("Sorry, this version of PHPMailer will only run on PHP version 5 or greater!\n");
  38
  39/**
  40 * PHP email creation and transport class
  41 * @package PHPMailer
  42 */
  43class PHPMailer {
  44
  45  /////////////////////////////////////////////////
  46  // PROPERTIES, PUBLIC
  47  /////////////////////////////////////////////////
  48
  49  /**
  50   * Email priority (1 = High, 3 = Normal, 5 = low).
  51   * @var int
  52   */
  53  public $Priority          = 3;
  54
  55  /**
  56   * Sets the CharSet of the message.
  57   * @var string
  58   */
  59  public $CharSet           = 'iso-8859-1';
  60
  61  /**
  62   * Sets the Content-type of the message.
  63   * @var string
  64   */
  65  public $ContentType       = 'text/plain';
  66
  67  /**
  68   * Sets the Encoding of the message. Options for this are
  69   *  "8bit", "7bit", "binary", "base64", and "quoted-printable".
  70   * @var string
  71   */
  72  public $Encoding          = '8bit';
  73
  74  /**
  75   * Holds the most recent mailer error message.
  76   * @var string
  77   */
  78  public $ErrorInfo         = '';
  79
  80  /**
  81   * Sets the From email address for the message.
  82   * @var string
  83   */
  84  public $From              = 'root@localhost';
  85
  86  /**
  87   * Sets the From name of the message.
  88   * @var string
  89   */
  90  public $FromName          = 'Root User';
  91
  92  /**
  93   * Sets the Sender email (Return-Path) of the message.  If not empty,
  94   * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
  95   * @var string
  96   */
  97  public $Sender            = '';
  98
  99  /**
 100   * Sets the Return-Path of the message.  If empty, it will
 101   * be set to either From or Sender.
 102   * @var string
 103   */
 104  public $ReturnPath        = '';
 105
 106  /**
 107   * Sets the Subject of the message.
 108   * @var string
 109   */
 110  public $Subject           = '';
 111
 112  /**
 113   * Sets the Body of the message.  This can be either an HTML or text body.
 114   * If HTML then run IsHTML(true).
 115   * @var string
 116   */
 117  public $Body              = '';
 118
 119  /**
 120   * Sets the text-only body of the message.  This automatically sets the
 121   * email to multipart/alternative.  This body can be read by mail
 122   * clients that do not have HTML email capability such as mutt. Clients
 123   * that can read HTML will view the normal Body.
 124   * @var string
 125   */
 126  public $AltBody           = '';
 127
 128  /**
 129   * Stores the complete compiled MIME message body.
 130   * @var string
 131   * @access protected
 132   */
 133  protected $MIMEBody       = '';
 134
 135  /**
 136   * Stores the complete compiled MIME message headers.
 137   * @var string
 138   * @access protected
 139   */
 140  protected $MIMEHeader     = '';
 141
 142  /**
 143   * Stores the extra header list which CreateHeader() doesn't fold in
 144   * @var string
 145   * @access protected
 146  */
 147  protected $mailHeader     = '';
 148
 149  /**
 150   * Sets word wrapping on the body of the message to a given number of
 151   * characters.
 152   * @var int
 153   */
 154  public $WordWrap          = 0;
 155
 156  /**
 157   * Method to send mail: ("mail", "sendmail", or "smtp").
 158   * @var string
 159   */
 160  public $Mailer            = 'mail';
 161
 162  /**
 163   * Sets the path of the sendmail program.
 164   * @var string
 165   */
 166  public $Sendmail          = '/usr/sbin/sendmail';
 167
 168  /**
 169   * Determine if mail() uses a fully sendmail compatible MTA that
 170   * supports sendmail's "-oi -f" options
 171   * @var boolean
 172   */
 173  public $UseSendmailOptions	= true;
 174  
 175  /**
 176   * Path to PHPMailer plugins.  Useful if the SMTP class
 177   * is in a different directory than the PHP include path.
 178   * @var string
 179   */
 180  public $PluginDir         = '';
 181
 182  /**
 183   * Sets the email address that a reading confirmation will be sent.
 184   * @var string
 185   */
 186  public $ConfirmReadingTo  = '';
 187
 188  /**
 189   * Sets the hostname to use in Message-Id and Received headers
 190   * and as default HELO string. If empty, the value returned
 191   * by SERVER_NAME is used or 'localhost.localdomain'.
 192   * @var string
 193   */
 194  public $Hostname          = '';
 195
 196  /**
 197   * Sets the message ID to be used in the Message-Id header.
 198   * If empty, a unique id will be generated.
 199   * @var string
 200   */
 201  public $MessageID         = '';
 202
 203  /**
 204   * Sets the message Date to be used in the Date header.
 205   * If empty, the current date will be added.
 206   * @var string
 207   */
 208  public $MessageDate       = '';
 209
 210  /////////////////////////////////////////////////
 211  // PROPERTIES FOR SMTP
 212  /////////////////////////////////////////////////
 213
 214  /**
 215   * Sets the SMTP hosts.
 216   *
 217   * All hosts must be separated by a
 218   * semicolon.  You can also specify a different port
 219   * for each host by using this format: [hostname:port]
 220   * (e.g. "smtp1.example.com:25;smtp2.example.com").
 221   * Hosts will be tried in order.
 222   * @var string
 223   */
 224  public $Host          = 'localhost';
 225
 226  /**
 227   * Sets the default SMTP server port.
 228   * @var int
 229   */
 230  public $Port          = 25;
 231
 232  /**
 233   * Sets the SMTP HELO of the message (Default is $Hostname).
 234   * @var string
 235   */
 236  public $Helo          = '';
 237
 238  /**
 239   * Sets connection prefix. Options are "", "ssl" or "tls"
 240   * @var string
 241   */
 242  public $SMTPSecure    = '';
 243
 244  /**
 245   * Sets SMTP authentication. Utilizes the Username and Password variables.
 246   * @var bool
 247   */
 248  public $SMTPAuth      = false;
 249
 250  /**
 251   * Sets SMTP username.
 252   * @var string
 253   */
 254  public $Username      = '';
 255
 256  /**
 257   * Sets SMTP password.
 258   * @var string
 259   */
 260  public $Password      = '';
 261
 262  /**
 263   *  Sets SMTP auth type. Options are LOGIN | PLAIN | NTLM  (default LOGIN)
 264   *  @var string
 265   */
 266  public $AuthType      = '';
 267  
 268  /**
 269   *  Sets SMTP realm.
 270   *  @var string
 271   */
 272  public $Realm         = '';
 273
 274  /**
 275   *  Sets SMTP workstation.
 276   *  @var string
 277   */
 278  public $Workstation   = '';
 279
 280  /**
 281   * Sets the SMTP server timeout in seconds.
 282   * This function will not work with the win32 version.
 283   * @var int
 284   */
 285  public $Timeout       = 10;
 286
 287  /**
 288   * Sets SMTP class debugging on or off.
 289   * @var bool
 290   */
 291  public $SMTPDebug     = false;
 292
 293  /**
 294   * Sets the function/method to use for debugging output.
 295   * Right now we only honor "echo" or "error_log"
 296   * @var string
 297   */
 298  public $Debugoutput     = "echo";
 299
 300  /**
 301   * Prevents the SMTP connection from being closed after each mail
 302   * sending.  If this is set to true then to close the connection
 303   * requires an explicit call to SmtpClose().
 304   * @var bool
 305   */
 306  public $SMTPKeepAlive = false;
 307
 308  /**
 309   * Provides the ability to have the TO field process individual
 310   * emails, instead of sending to entire TO addresses
 311   * @var bool
 312   */
 313  public $SingleTo      = false;
 314
 315   /**
 316   * If SingleTo is true, this provides the array to hold the email addresses
 317   * @var bool
 318   */
 319  public $SingleToArray = array();
 320
 321 /**
 322   * Provides the ability to change the generic line ending
 323   * NOTE: The default remains '\n'. We force CRLF where we KNOW
 324   *        it must be used via self::CRLF
 325   * @var string
 326   */
 327  public $LE              = "\n";
 328
 329   /**
 330   * Used with DKIM Signing
 331   * required parameter if DKIM is enabled
 332   *
 333   * domain selector example domainkey
 334   * @var string
 335   */
 336  public $DKIM_selector   = '';
 337
 338  /**
 339   * Used with DKIM Signing
 340   * required if DKIM is enabled, in format of email address 'you@yourdomain.com' typically used as the source of the email
 341   * @var string
 342   */
 343  public $DKIM_identity   = '';
 344
 345  /**
 346   * Used with DKIM Signing
 347   * optional parameter if your private key requires a passphras
 348   * @var string
 349   */
 350  public $DKIM_passphrase   = '';
 351
 352  /**
 353   * Used with DKIM Singing
 354   * required if DKIM is enabled, in format of email address 'domain.com'
 355   * @var string
 356   */
 357  public $DKIM_domain     = '';
 358
 359  /**
 360   * Used with DKIM Signing
 361   * required if DKIM is enabled, path to private key file
 362   * @var string
 363   */
 364  public $DKIM_private    = '';
 365
 366  /**
 367   * Callback Action function name.
 368   * The function that handles the result of the send email action.
 369   * It is called out by Send() for each email sent.
 370   *
 371   * Value can be:
 372   * - 'function_name' for function names
 373   * - 'Class::Method' for static method calls
 374   * - array($object, 'Method') for calling methods on $object
 375   * See http://php.net/is_callable manual page for more details.
 376   *
 377   * Parameters:
 378   *   bool    $result        result of the send action
 379   *   string  $to            email address of the recipient
 380   *   string  $cc            cc email addresses
 381   *   string  $bcc           bcc email addresses
 382   *   string  $subject       the subject
 383   *   string  $body          the email body
 384   *   string  $from          email address of sender
 385   * @var string
 386   */
 387  public $action_function = ''; //'callbackAction';
 388
 389  /**
 390   * Sets the PHPMailer Version number
 391   * @var string
 392   */
 393  public $Version         = '5.2.2';
 394
 395  /**
 396   * What to use in the X-Mailer header
 397   * @var string NULL for default, whitespace for None, or actual string to use
 398   */
 399  public $XMailer         = '';
 400
 401  /////////////////////////////////////////////////
 402  // PROPERTIES, PRIVATE AND PROTECTED
 403  /////////////////////////////////////////////////
 404
 405  /**
 406   * @var SMTP An instance of the SMTP sender class
 407   * @access protected
 408   */
 409  protected   $smtp           = null;
 410  /**
 411   * @var array An array of 'to' addresses
 412   * @access protected
 413   */
 414  protected   $to             = array();
 415  /**
 416   * @var array An array of 'cc' addresses
 417   * @access protected
 418   */
 419  protected   $cc             = array();
 420  /**
 421   * @var array An array of 'bcc' addresses
 422   * @access protected
 423   */
 424  protected   $bcc            = array();
 425  /**
 426   * @var array An array of reply-to name and address
 427   * @access protected
 428   */
 429  protected   $ReplyTo        = array();
 430  /**
 431   * @var array An array of all kinds of addresses: to, cc, bcc, replyto
 432   * @access protected
 433   */
 434  protected   $all_recipients = array();
 435  /**
 436   * @var array An array of attachments
 437   * @access protected
 438   */
 439  protected   $attachment     = array();
 440  /**
 441   * @var array An array of custom headers
 442   * @access protected
 443   */
 444  protected   $CustomHeader   = array();
 445  /**
 446   * @var string The message's MIME type
 447   * @access protected
 448   */
 449  protected   $message_type   = '';
 450  /**
 451   * @var array An array of MIME boundary strings
 452   * @access protected
 453   */
 454  protected   $boundary       = array();
 455  /**
 456   * @var array An array of available languages
 457   * @access protected
 458   */
 459  protected   $language       = array();
 460  /**
 461   * @var integer The number of errors encountered
 462   * @access protected
 463   */
 464  protected   $error_count    = 0;
 465  /**
 466   * @var string The filename of a DKIM certificate file
 467   * @access protected
 468   */
 469  protected   $sign_cert_file = '';
 470  /**
 471   * @var string The filename of a DKIM key file
 472   * @access protected
 473   */
 474  protected   $sign_key_file  = '';
 475  /**
 476   * @var string The password of a DKIM key
 477   * @access protected
 478   */
 479  protected   $sign_key_pass  = '';
 480  /**
 481   * @var boolean Whether to throw exceptions for errors
 482   * @access protected
 483   */
 484  protected   $exceptions     = false;
 485
 486  /////////////////////////////////////////////////
 487  // CONSTANTS
 488  /////////////////////////////////////////////////
 489
 490  const STOP_MESSAGE  = 0; // message only, continue processing
 491  const STOP_CONTINUE = 1; // message?, likely ok to continue processing
 492  const STOP_CRITICAL = 2; // message, plus full stop, critical error reached
 493  const CRLF = "\r\n";     // SMTP RFC specified EOL
 494  
 495  /////////////////////////////////////////////////
 496  // METHODS, VARIABLES
 497  /////////////////////////////////////////////////
 498
 499  /**
 500   * Calls actual mail() function, but in a safe_mode aware fashion
 501   * Also, unless sendmail_path points to sendmail (or something that
 502   * claims to be sendmail), don't pass params (not a perfect fix,
 503   * but it will do)
 504   * @param string $to To
 505   * @param string $subject Subject
 506   * @param string $body Message Body
 507   * @param string $header Additional Header(s)
 508   * @param string $params Params
 509   * @access private
 510   * @return bool
 511   */
 512  private function mail_passthru($to, $subject, $body, $header, $params) {
 513    if ( ini_get('safe_mode') || !($this->UseSendmailOptions) ) {
 514        $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($subject)), $body, $header);
 515    } else {
 516        $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($subject)), $body, $header, $params);
 517    }
 518    return $rt;
 519  }
 520
 521  /**
 522   * Outputs debugging info via user-defined method
 523   * @param string $str
 524   */
 525  private function edebug($str) {
 526    if ($this->Debugoutput == "error_log") {
 527        error_log($str);
 528    } else {
 529        echo $str;
 530    }
 531  }
 532
 533  /**
 534   * Constructor
 535   * @param boolean $exceptions Should we throw external exceptions?
 536   */
 537  public function __construct($exceptions = false) {
 538    $this->exceptions = ($exceptions == true);
 539  }
 540
 541  /**
 542   * Sets message type to HTML.
 543   * @param bool $ishtml
 544   * @return void
 545   */
 546  public function IsHTML($ishtml = true) {
 547    if ($ishtml) {
 548      $this->ContentType = 'text/html';
 549    } else {
 550      $this->ContentType = 'text/plain';
 551    }
 552  }
 553
 554  /**
 555   * Sets Mailer to send message using SMTP.
 556   * @return void
 557   * @deprecated
 558   */
 559  public function IsSMTP() {
 560    $this->Mailer = 'smtp';
 561  }
 562
 563  /**
 564   * Sets Mailer to send message using PHP mail() function.
 565   * @return void
 566   * @deprecated
 567   */
 568  public function IsMail() {
 569    $this->Mailer = 'mail';
 570  }
 571
 572  /**
 573   * Sets Mailer to send message using the $Sendmail program.
 574   * @return void
 575   * @deprecated
 576   */
 577  public function IsSendmail() {
 578    if (!stristr(ini_get('sendmail_path'), 'sendmail')) {
 579      $this->Sendmail = '/var/qmail/bin/sendmail';
 580    }
 581    $this->Mailer = 'sendmail';
 582  }
 583
 584  /**
 585   * Sets Mailer to send message using the qmail MTA.
 586   * @return void
 587   * @deprecated
 588   */
 589  public function IsQmail() {
 590    if (stristr(ini_get('sendmail_path'), 'qmail')) {
 591      $this->Sendmail = '/var/qmail/bin/sendmail';
 592    }
 593    $this->Mailer = 'sendmail';
 594  }
 595
 596  /////////////////////////////////////////////////
 597  // METHODS, RECIPIENTS
 598  /////////////////////////////////////////////////
 599
 600  /**
 601   * Adds a "To" address.
 602   * @param string $address
 603   * @param string $name
 604   * @return boolean true on success, false if address already used
 605   */
 606  public function AddAddress($address, $name = '') {
 607    return $this->AddAnAddress('to', $address, $name);
 608  }
 609
 610  /**
 611   * Adds a "Cc" address.
 612   * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
 613   * @param string $address
 614   * @param string $name
 615   * @return boolean true on success, false if address already used
 616   */
 617  public function AddCC($address, $name = '') {
 618    return $this->AddAnAddress('cc', $address, $name);
 619  }
 620
 621  /**
 622   * Adds a "Bcc" address.
 623   * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
 624   * @param string $address
 625   * @param string $name
 626   * @return boolean true on success, false if address already used
 627   */
 628  public function AddBCC($address, $name = '') {
 629    return $this->AddAnAddress('bcc', $address, $name);
 630  }
 631
 632  /**
 633   * Adds a "Reply-to" address.
 634   * @param string $address
 635   * @param string $name
 636   * @return boolean
 637   */
 638  public function AddReplyTo($address, $name = '') {
 639    return $this->AddAnAddress('Reply-To', $address, $name);
 640  }
 641
 642  /**
 643   * Adds an address to one of the recipient arrays
 644   * Addresses that have been added already return false, but do not throw exceptions
 645   * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
 646   * @param string $address The email address to send to
 647   * @param string $name
 648   * @throws phpmailerException
 649   * @return boolean true on success, false if address already used or invalid in some way
 650   * @access protected
 651   */
 652  protected function AddAnAddress($kind, $address, $name = '') {
 653    if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) {
 654      $this->SetError($this->Lang('Invalid recipient array').': '.$kind);
 655      if ($this->exceptions) {
 656        throw new phpmailerException('Invalid recipient array: ' . $kind);
 657      }
 658      if ($this->SMTPDebug) {
 659        $this->edebug($this->Lang('Invalid recipient array').': '.$kind);
 660      }
 661      return false;
 662    }
 663    $address = trim($address);
 664    $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
 665    if (!$this->ValidateAddress($address)) {
 666      $this->SetError($this->Lang('invalid_address').': '. $address);
 667      if ($this->exceptions) {
 668        throw new phpmailerException($this->Lang('invalid_address').': '.$address);
 669      }
 670      if ($this->SMTPDebug) {
 671        $this->edebug($this->Lang('invalid_address').': '.$address);
 672      }
 673      return false;
 674    }
 675    if ($kind != 'Reply-To') {
 676      if (!isset($this->all_recipients[strtolower($address)])) {
 677        array_push($this->$kind, array($address, $name));
 678        $this->all_recipients[strtolower($address)] = true;
 679        return true;
 680      }
 681    } else {
 682      if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
 683        $this->ReplyTo[strtolower($address)] = array($address, $name);
 684      return true;
 685    }
 686  }
 687  return false;
 688}
 689
 690/**
 691 * Set the From and FromName properties
 692 * @param string $address
 693 * @param string $name
 694 * @param int $auto Also set Reply-To and Sender
 695   * @throws phpmailerException
 696 * @return boolean
 697 */
 698  public function SetFrom($address, $name = '', $auto = 1) {
 699    $address = trim($address);
 700    $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
 701    if (!$this->ValidateAddress($address)) {
 702      $this->SetError($this->Lang('invalid_address').': '. $address);
 703      if ($this->exceptions) {
 704        throw new phpmailerException($this->Lang('invalid_address').': '.$address);
 705      }
 706      if ($this->SMTPDebug) {
 707        $this->edebug($this->Lang('invalid_address').': '.$address);
 708      }
 709      return false;
 710    }
 711    $this->From = $address;
 712    $this->FromName = $name;
 713    if ($auto) {
 714      if (empty($this->ReplyTo)) {
 715        $this->AddAnAddress('Reply-To', $address, $name);
 716      }
 717      if (empty($this->Sender)) {
 718        $this->Sender = $address;
 719      }
 720    }
 721    return true;
 722  }
 723
 724  /**
 725   * Check that a string looks roughly like an email address should
 726   * Static so it can be used without instantiation, public so people can overload
 727   * Conforms to RFC5322: Uses *correct* regex on which FILTER_VALIDATE_EMAIL is
 728   * based; So why not use FILTER_VALIDATE_EMAIL? Because it was broken to
 729   * not allow a@b type valid addresses :(
 730   * Some Versions of PHP break on the regex though, likely due to PCRE, so use
 731   * the older validation method for those users. (http://php.net/manual/en/pcre.installation.php)
 732   * @link http://squiloople.com/2009/12/20/email-address-validation/
 733   * @copyright regex Copyright Michael Rushton 2009-10 | http://squiloople.com/ | Feel free to use and redistribute this code. But please keep this copyright notice.
 734   * @param string $address The email address to check
 735   * @return boolean
 736   * @static
 737   * @access public
 738   */
 739  public static function ValidateAddress($address) {
 740	if ((defined('PCRE_VERSION')) && (version_compare(PCRE_VERSION, '8.0') >= 0)) {
 741	  return preg_match('/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)((?>(?>(?>((?>(?>(?>\x0D\x0A)?[	 ])+|(?>[	 ]*\x0D\x0A)?[	 ]+)?)(\((?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}|(?!(?:.*[a-f0-9][:\]]){7,})((?6)(?>:(?6)){0,5})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:|(?!(?:.*[a-f0-9]:){5,})(?8)?::(?>((?6)(?>:(?6)){0,3}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD', $address);
 742	} elseif (function_exists('filter_var')) { //Introduced in PHP 5.2
 743        if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) {
 744          return false;
 745        } else {
 746          return true;
 747        }
 748    } else {
 749        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);
 750	}
 751  }
 752
 753  /////////////////////////////////////////////////
 754  // METHODS, MAIL SENDING
 755  /////////////////////////////////////////////////
 756
 757  /**
 758   * Creates message and assigns Mailer. If the message is
 759   * not sent successfully then it returns false.  Use the ErrorInfo
 760   * variable to view description of the error.
 761   * @throws phpmailerException
 762   * @return bool
 763   */
 764  public function Send() {
 765    try {
 766      if(!$this->PreSend()) return false;
 767      return $this->PostSend();
 768    } catch (phpmailerException $e) {
 769      $this->mailHeader = '';
 770      $this->SetError($e->getMessage());
 771      if ($this->exceptions) {
 772        throw $e;
 773      }
 774      return false;
 775    }
 776  }
 777
 778  /**
 779   * Prep mail by constructing all message entities
 780   * @throws phpmailerException
 781   * @return bool
 782   */
 783  public function PreSend() {
 784    try {
 785      $this->mailHeader = "";
 786      if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
 787        throw new phpmailerException($this->Lang('provide_address'), self::STOP_CRITICAL);
 788      }
 789
 790      // Set whether the message is multipart/alternative
 791      if(!empty($this->AltBody)) {
 792        $this->ContentType = 'multipart/alternative';
 793      }
 794
 795      $this->error_count = 0; // reset errors
 796      $this->SetMessageType();
 797      //Refuse to send an empty message
 798      if (empty($this->Body)) {
 799        throw new phpmailerException($this->Lang('empty_message'), self::STOP_CRITICAL);
 800      }
 801
 802      $this->MIMEHeader = $this->CreateHeader();
 803      $this->MIMEBody = $this->CreateBody();
 804
 805      // To capture the complete message when using mail(), create
 806      // an extra header list which CreateHeader() doesn't fold in
 807      if ($this->Mailer == 'mail') {
 808        if (count($this->to) > 0) {
 809          $this->mailHeader .= $this->AddrAppend("To", $this->to);
 810        } else {
 811          $this->mailHeader .= $this->HeaderLine("To", "undisclosed-recipients:;");
 812        }
 813        $this->mailHeader .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader(trim($this->Subject))));
 814        // if(count($this->cc) > 0) {
 815            // $this->mailHeader .= $this->AddrAppend("Cc", $this->cc);
 816        // }
 817      }
 818
 819      // digitally sign with DKIM if enabled
 820      if (!empty($this->DKIM_domain) && !empty($this->DKIM_private) && !empty($this->DKIM_selector) && !empty($this->DKIM_domain) && file_exists($this->DKIM_private)) {
 821        $header_dkim = $this->DKIM_Add($this->MIMEHeader, $this->EncodeHeader($this->SecureHeader($this->Subject)), $this->MIMEBody);
 822        $this->MIMEHeader = str_replace("\r\n", "\n", $header_dkim) . $this->MIMEHeader;
 823      }
 824
 825      return true;
 826
 827    } catch (phpmailerException $e) {
 828      $this->SetError($e->getMessage());
 829      if ($this->exceptions) {
 830        throw $e;
 831      }
 832      return false;
 833    }
 834  }
 835
 836  /**
 837   * Actual Email transport function
 838   * Send the email via the selected mechanism
 839   * @throws phpmailerException
 840   * @return bool
 841   */
 842  public function PostSend() {
 843    try {
 844      // Choose the mailer and send through it
 845      switch($this->Mailer) {
 846        case 'sendmail':
 847          return $this->SendmailSend($this->MIMEHeader, $this->MIMEBody);
 848        case 'smtp':
 849          return $this->SmtpSend($this->MIMEHeader, $this->MIMEBody);
 850        case 'mail':
 851          return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
 852        default:
 853          return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
 854      }
 855    } catch (phpmailerException $e) {
 856      $this->SetError($e->getMessage());
 857      if ($this->exceptions) {
 858        throw $e;
 859      }
 860      if ($this->SMTPDebug) {
 861        $this->edebug($e->getMessage()."\n");
 862      }
 863    }
 864    return false;
 865  }
 866
 867  /**
 868   * Sends mail using the $Sendmail program.
 869   * @param string $header The message headers
 870   * @param string $body The message body
 871   * @throws phpmailerException
 872   * @access protected
 873   * @return bool
 874   */
 875  protected function SendmailSend($header, $body) {
 876    if ($this->Sender != '') {
 877      $sendmail = sprintf("%s -oi -f%s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
 878    } else {
 879      $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
 880    }
 881    if ($this->SingleTo === true) {
 882      foreach ($this->SingleToArray as $val) {
 883        if(!@$mail = popen($sendmail, 'w')) {
 884          throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
 885        }
 886        fputs($mail, "To: " . $val . "\n");
 887        fputs($mail, $header);
 888        fputs($mail, $body);
 889        $result = pclose($mail);
 890        // implement call back function if it exists
 891        $isSent = ($result == 0) ? 1 : 0;
 892        $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
 893        if($result != 0) {
 894          throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
 895        }
 896      }
 897    } else {
 898      if(!@$mail = popen($sendmail, 'w')) {
 899        throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
 900      }
 901      fputs($mail, $header);
 902      fputs($mail, $body);
 903      $result = pclose($mail);
 904      // implement call back function if it exists
 905      $isSent = ($result == 0) ? 1 : 0;
 906      $this->doCallback($isSent, $this->to, $this->cc, $this->bcc, $this->Subject, $body);
 907      if($result != 0) {
 908        throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
 909      }
 910    }
 911    return true;
 912  }
 913
 914  /**
 915   * Sends mail using the PHP mail() function.
 916   * @param string $header The message headers
 917   * @param string $body The message body
 918     * @throws phpmailerException
 919   * @access protected
 920   * @return bool
 921   */
 922  protected function MailSend($header, $body) {
 923    $toArr = array();
 924    foreach($this->to as $t) {
 925      $toArr[] = $this->AddrFormat($t);
 926    }
 927    $to = implode(', ', $toArr);
 928
 929    if (empty($this->Sender)) {
 930      $params = "-oi ";
 931    } else {
 932      $params = sprintf("-oi -f%s", $this->Sender);
 933    }
 934    if ($this->Sender != '' and !ini_get('safe_mode')) {
 935      $old_from = ini_get('sendmail_from');
 936      ini_set('sendmail_from', $this->Sender);
 937    }
 938      $rt = false;
 939    if ($this->SingleTo === true && count($toArr) > 1) {
 940      foreach ($toArr as $val) {
 941        $rt = $this->mail_passthru($val, $this->Subject, $body, $header, $params);
 942        // implement call back function if it exists
 943        $isSent = ($rt == 1) ? 1 : 0;
 944        $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
 945      }
 946    } else {
 947      $rt = $this->mail_passthru($to, $this->Subject, $body, $header, $params);
 948      // implement call back function if it exists
 949      $isSent = ($rt == 1) ? 1 : 0;
 950      $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body);
 951    }
 952    if (isset($old_from)) {
 953      ini_set('sendmail_from', $old_from);
 954    }
 955    if(!$rt) {
 956      throw new phpmailerException($this->Lang('instantiate'), self::STOP_CRITICAL);
 957    }
 958    return true;
 959  }
 960
 961  /**
 962   * Sends mail via SMTP using PhpSMTP
 963   * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
 964   * @param string $header The message headers
 965   * @param string $body The message body
 966   * @throws phpmailerException
 967   * @uses SMTP
 968   * @access protected
 969   * @return bool
 970   */
 971  protected function SmtpSend($header, $body) {
 972    require_once $this->PluginDir . 'smtp.php';
 973    $bad_rcpt = array();
 974
 975    if(!$this->SmtpConnect()) {
 976      throw new phpmailerException($this->Lang('smtp_connect_failed'), self::STOP_CRITICAL);
 977    }
 978    $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
 979    if(!$this->smtp->Mail($smtp_from)) {
 980      $this->SetError($this->Lang('from_failed') . $smtp_from . " : " . implode(",",$this->smtp->getError())) ;
 981      throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL);
 982    }
 983
 984    // Attempt to send attach all recipients
 985    foreach($this->to as $to) {
 986      if (!$this->smtp->Recipient($to[0])) {
 987        $bad_rcpt[] = $to[0];
 988        // implement call back function if it exists
 989        $isSent = 0;
 990        $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
 991      } else {
 992        // implement call back function if it exists
 993        $isSent = 1;
 994        $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
 995      }
 996    }
 997    foreach($this->cc as $cc) {
 998      if (!$this->smtp->Recipient($cc[0])) {
 999        $bad_rcpt[] = $cc[0];
1000        // implement call back function if it exists
1001        $isSent = 0;
1002        $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
1003      } else {
1004        // implement call back function if it exists
1005        $isSent = 1;
1006        $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
1007      }
1008    }
1009    foreach($this->bcc as $bcc) {
1010      if (!$this->smtp->Recipient($bcc[0])) {
1011        $bad_rcpt[] = $bcc[0];
1012        // implement call back function if it exists
1013        $isSent = 0;
1014        $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
1015      } else {
1016        // implement call back function if it exists
1017        $isSent = 1;
1018        $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
1019      }
1020    }
1021
1022
1023    if (count($bad_rcpt) > 0 ) { //Create error message for any bad addresses
1024      $badaddresses = implode(', ', $bad_rcpt);
1025      throw new phpmailerException($this->Lang('recipients_failed') . $badaddresses);
1026    }
1027    if(!$this->smtp->Data($header . $body)) {
1028      throw new phpmailerException($this->Lang('data_not_accepted'), self::STOP_CRITICAL);
1029    }
1030    if($this->SMTPKeepAlive == true) {
1031      $this->smtp->Reset();
1032    } else {
1033        $this->smtp->Quit();
1034        $this->smtp->Close();
1035    }
1036    return true;
1037  }
1038
1039  /**
1040   * Initiates a connection to an SMTP server.
1041   * Returns false if the operation failed.
1042   * @uses SMTP
1043   * @access public
1044   * @throws phpmailerException
1045   * @return bool
1046   */
1047  public function SmtpConnect() {
1048    if(is_null($this->smtp)) {
1049      $this->smtp = new SMTP;
1050    }
1051
1052    $this->smtp->Timeout = $this->Timeout;
1053    $this->smtp->do_debug = $this->SMTPDebug;
1054    $hosts = explode(';', $this->Host);
1055    $index = 0;
1056    $connection = $this->smtp->Connected();
1057
1058    // Retry while there is no connection
1059    try {
1060      while($index < count($hosts) && !$connection) {
1061        $hostinfo = array();
1062        if (preg_match('/^(.+):([0-9]+)$/', $hosts[$index], $hostinfo)) {
1063          $host = $hostinfo[1];
1064          $port = $hostinfo[2];
1065        } else {
1066          $host = $hosts[$index];
1067          $port = $this->Port;
1068        }
1069
1070        $tls = ($this->SMTPSecure == 'tls');
1071        $ssl = ($this->SMTPSecure == 'ssl');
1072
1073        if ($this->smtp->Connect(($ssl ? 'ssl://':'').$host, $port, $this->Timeout)) {
1074
1075          $hello = ($this->Helo != '' ? $this->Helo : $this->ServerHostname());
1076          $this->smtp->Hello($hello);
1077
1078          if ($tls) {
1079            if (!$this->smtp->StartTLS()) {
1080              throw new phpmailerException($this->Lang('connect_host'));
1081            }
1082
1083            //We must resend HELO after tls negotiation
1084            $this->smtp->Hello($hello);
1085          }
1086
1087          $connection = true;
1088          if ($this->SMTPAuth) {
1089            if (!$this->smtp->Authenticate($this->Username, $this->Password, $this->AuthType,
1090                                           $this->Realm, $this->Workstation)) {
1091              throw new phpmailerException($this->Lang('authenticate'));
1092            }
1093          }
1094        }
1095        $index++;
1096      if (!$connection) {
1097        throw new phpmailerException($this->Lang('connect_host'));
1098      }
1099      }
1100    } catch (phpmailerException $e) {
1101      $this->smtp->Reset();
1102      if ($this->exceptions) {
1103        throw $e;
1104      }
1105    }
1106    return true;
1107  }
1108
1109  /**
1110   * Closes the active SMTP session if one exists.
1111   * @return void
1112   */
1113  public function SmtpClose() {
1114    if ($this->smtp !== null) {
1115      if($this->smtp->Connected()) {
1116        $this->smtp->Quit();
1117        $this->smtp->Close();
1118      }
1119    }
1120  }
1121
1122  /**
1123  * Sets the language for all class error messages.
1124  * Returns false if it cannot load the language file.  The default language is English.
1125  * @param string $langcode ISO 639-1 2-character language code (e.g. Portuguese: "br")
1126  * @param string $lang_path Path to the language file directory
1127   * @return bool
1128  * @access public
1129  */
1130  function SetLanguage($langcode = 'en', $lang_path = 'language/') {
1131    //Define full set of translatable strings
1132    $PHPMAILER_LANG = array(
1133      'authenticate'         => 'SMTP Error: Could not authenticate.',
1134      'connect_host'         => 'SMTP Error: Could not connect to SMTP host.',
1135      'data_not_accepted'    => 'SMTP Error: Data not accepted.',
1136      'empty_message'        => 'Message body empty',
1137      'encoding'             => 'Unknown encoding: ',
1138      'execute'              => 'Could not execute: ',
1139      'file_access'          => 'Could not access file: ',
1140      'file_open'            => 'File Error: Could not open file: ',
1141      'from_failed'          => 'The following From address failed: ',
1142      'instantiate'          => 'Could not instantiate mail function.',
1143      'invalid_address'      => 'Invalid address',
1144      'mailer_not_supported' => ' mailer is not supported.',
1145      'provide_address'      => 'You must provide at least one recipient email address.',
1146      'recipients_failed'    => 'SMTP Error: The following recipients failed: ',
1147      'signing'              => 'Signing Error: ',
1148      'smtp_connect_failed'  => 'SMTP Connect() failed.',
1149      'smtp_error'           => 'SMTP server error: ',
1150      'variable_set'         => 'Cannot set or reset variable: '
1151    );
1152    //Overwrite language-specific strings. This way we'll never have missing translations - no more "language string failed to load"!
1153    $l = true;
1154    if ($langcode != 'en') { //There is no English translation file
1155      $l = @include $lang_path.'phpmailer.lang-'.$langcode.'.php';
1156    }
1157    $this->language = $PHPMAILER_LANG;
1158    return ($l == true); //Returns false if language not found
1159  }
1160
1161  /**
1162  * Return the current array of language strings
1163  * @return array
1164  */
1165  public function GetTranslations() {
1166    return $this->language;
1167  }
1168
1169  /////////////////////////////////////////////////
1170  // METHODS, MESSAGE CREATION
1171  /////////////////////////////////////////////////
1172
1173  /**
1174   * Creates recipient headers.
1175   * @access public
1176   * @param string $type
1177   * @param array $addr
1178   * @return string
1179   */
1180  public function AddrAppend($type, $addr) {
1181    $addr_str = $type . ': ';
1182    $addresses = array();
1183    foreach ($addr as $a) {
1184      $addresses[] = $this->AddrFormat($a);
1185    }
1186    $addr_str .= implode(', ', $addresses);
1187    $addr_str .= $this->LE;
1188
1189    return $addr_str;
1190  }
1191
1192  /**
1193   * Formats an address correctly.
1194   * @access public
1195   * @param string $addr
1196   * @return string
1197   */
1198  public function AddrFormat($addr) {
1199    if (empty($addr[1])) {
1200      return $this->SecureHeader($addr[0]);
1201    } else {
1202      return $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
1203    }
1204  }
1205
1206  /**
1207   * Wraps message for use with mailers that do not
1208   * automatically perform wrapping and for quoted-printable.
1209   * Original written by philippe.
1210   * @param string $message The message to wrap
1211   * @param integer $length The line length to wrap to
1212   * @param boolean $qp_mode Whether to run in Quoted-Printable mode
1213   * @access public
1214   * @return string
1215   */
1216  public function WrapText($message, $length, $qp_mode = false) {
1217    $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
1218    // If utf-8 encoding is used, we will need to make sure we don't
1219    // split multibyte characters when we wrap
1220    $is_utf8 = (strtolower($this->CharSet) == "utf-8");
1221    $lelen = strlen($this->LE);
1222    $crlflen = strlen(self::CRLF);
1223
1224    $message = $this->FixEOL($message);
1225    if (substr($message, -$lelen) == $this->LE) {
1226      $message = substr($message, 0, -$lelen);
1227    }
1228
1229    $line = explode($this->LE, $message);   // Magic. We know FixEOL uses $LE
1230    $message = '';
1231    for ($i = 0 ;$i < count($line); $i++) {
1232      $line_part = explode(' ', $line[$i]);
1233      $buf = '';
1234      for ($e = 0; $e<count($line_part); $e++) {
1235        $word = $line_part[$e];
1236        if ($qp_mode and (strlen($word) > $length)) {
1237          $space_left = $length - strlen($buf) - $crlflen;
1238          if ($e != 0) {
1239            if ($space_left > 20) {
1240              $len = $space_left;
1241              if ($is_utf8) {
1242                $len = $this->UTF8CharBoundary($word, $len);
1243              } elseif (substr($word, $len - 1, 1) == "=") {
1244                $len--;
1245              } elseif (substr($word, $len - 2, 1) == "=") {
1246                $len -= 2;
1247              }
1248              $part = substr($word, 0, $len);
1249              $word = substr($word, $len);
1250              $buf .= ' ' . $part;
1251              $message .= $buf . sprintf("=%s", self::CRLF);
1252            } else {
1253              $message .= $buf . $soft_break;
1254            }
1255            $buf = '';
1256          }
1257          while (strlen($word) > 0) {
1258            $len = $length;
1259            if ($is_utf8) {
1260              $len = $this->UTF8CharBoundary($word, $len);
1261            } elseif (substr($word, $len - 1, 1) == "=") {
1262              $len--;
1263            } elseif (substr($word, $len - 2, 1) == "=") {
1264              $len -= 2;
1265            }
1266            $part = substr($word, 0, $len);
1267            $word = substr($word, $len);
1268
1269            if (strlen($word) > 0) {
1270              $message .= $part . sprintf("=%s", self::CRLF);
1271            } else {
1272              $buf = $part;
1273            }
1274          }
1275        } else {
1276          $buf_o = $buf;
1277          $buf .= ($e == 0) ? $word : (' ' . $word);
1278
1279          if (strlen($buf) > $length and $buf_o != '') {
1280            $message .= $buf_o . $soft_break;
1281            $buf = $word;
1282          }
1283        }
1284      }
1285      $message .= $buf . self::CRLF;
1286    }
1287
1288    return $message;
1289  }
1290
1291  /**
1292   * Finds last character boundary prior to maxLength in a utf-8
1293   * quoted (printable) encoded string.
1294   * Original written by Colin Brown.
1295   * @access public
1296   * @param string $encodedText utf-8 QP text
1297   * @param int    $maxLength   find last character boundary prior to this length
1298   * @return int
1299   */
1300  public function UTF8CharBoundary($encodedText, $maxLength) {
1301    $foundSplitPos = false;
1302    $lookBack = 3;
1303    while (!$foundSplitPos) {
1304      $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
1305      $encodedCharPos = strpos($lastChunk, "=");
1306      if ($encodedCharPos !== false) {
1307        // Found start of encoded character byte within $lookBack block.
1308        // Check the encoded byte value (the 2 chars after the '=')
1309        $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
1310        $dec = hexdec($hex);
1311        if ($dec < 128) { // Single byte character.
1312          // If the encoded char was found at pos 0, it will fit
1313          // otherwise reduce maxLength to start of the encoded char
1314          $maxLength = ($encodedCharPos == 0) ? $maxLength :
1315          $maxLength - ($lookBack - $encodedCharPos);
1316          $foundSplitPos = true;
1317        } elseif ($dec >= 192) { // First byte of a multi byte character
1318          // Reduce maxLength to split at start of character
1319          $maxLength = $maxLength - ($lookBack - $encodedCharPos);
1320          $foundSplitPos = true;
1321        } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
1322          $lookBack += 3;
1323        }
1324      } else {
1325        // No encoded character found
1326        $foundSplitPos = true;
1327      }
1328    }
1329    return $maxLength;
1330  }
1331
1332
1333  /**
1334   * Set the body wrapping.
1335   * @access public
1336   * @return void
1337   */
1338  public function SetWordWrap() {
1339    if($this->WordWrap < 1) {
1340      return;
1341    }
1342
1343    switch($this->message_type) {
1344      case 'alt':
1345      case 'alt_inline':
1346      case 'alt_attach':
1347      case 'alt_inline_attach':
1348        $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
1349        break;
1350      default:
1351        $this->Body = $this->WrapText($this->Body, $this->WordWrap);
1352        break;
1353    }
1354  }
1355
1356  /**
1357   * Assembles message header.
1358   * @access public
1359   * @return string The assembled header
1360   */
1361  public function CreateHeader() {
1362    $result = '';
1363
1364    // Set the boundaries
1365    $uniq_id = md5(uniqid(time()));
1366    $this->boundary[1] = 'b1_' . $uniq_id;
1367    $this->boundary[2] = 'b2_' . $uniq_id;
1368    $this->boundary[3] = 'b3_' . $uniq_id;
1369
1370    if ($this->MessageDate == '') {
1371      $result .= $this->HeaderLine('Date', self::RFCDate());
1372    } else {
1373      $result .= $this->HeaderLine('Date', $this->MessageDate);
1374    }
1375
1376    if ($this->ReturnPath) {
1377      $result .= $this->HeaderLine('Return-Path', trim($this->ReturnPath));
1378    } elseif ($this->Sender == '') {
1379      $result .= $this->HeaderLine('Return-Path', trim($this->From));
1380    } else {
1381      $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
1382    }
1383
1384    // To be created automatically by mail()
1385    if($this->Mailer != 'mail') {
1386      if ($this->SingleTo === true) {
1387        foreach($this->to as $t) {
1388          $this->SingleToArray[] = $this->AddrFormat($t);
1389        }
1390      } else {
1391        if(count($this->to) > 0) {
1392          $result .= $this->AddrAppend('To', $this->to);
1393        } elseif (count($this->cc) == 0) {
1394          $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
1395        }
1396      }
1397    }
1398
1399    $from = array();
1400    $from[0][0] = trim($this->From);
1401    $from[0][1] = $this->FromName;
1402    $result .= $this->AddrAppend('From', $from);
1403
1404    // sendmail and mail() extract Cc from the header before sending
1405    if(count($this->cc) > 0) {
1406      $result .= $this->AddrAppend('Cc', $this->cc);
1407    }
1408
1409    // sendmail and mail() extract Bcc from the header before sending
1410    if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
1411      $result .= $this->AddrAppend('Bcc', $this->bcc);
1412    }
1413
1414    if(count($this->ReplyTo) > 0) {
1415      $result .= $this->AddrAppend('Reply-To', $this->ReplyTo);
1416    }
1417
1418    // mail() sets the subject itself
1419    if($this->Mailer != 'mail') {
1420      $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
1421    }
1422
1423    if($this->MessageID != '') {
1424      $result .= $this->HeaderLine('Message-ID', $this->MessageID);
1425    } else {
1426      $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
1427    }
1428    $result .= $this->HeaderLine('X-Priority', $this->Priority);
1429    if ($this->XMailer == '') {
1430        $result .= $this->HeaderLine('X-Mailer', 'PHPMailer '.$this->Version.' (http://code.google.com/a/apache-extras.org/p/phpmailer/)');
1431    } else {
1432      $myXmailer = trim($this->XMailer);
1433      if ($myXmailer) {
1434        $result .= $this->HeaderLine('X-Mailer', $myXmailer);
1435      }
1436    }
1437
1438    if($this->ConfirmReadingTo != '') {
1439      $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
1440    }
1441
1442    // Add custom headers
1443    for($index = 0; $index < count($this->CustomHeader); $index++) {
1444      $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
1445    }
1446    if (!$this->sign_key_file) {
1447      $result .= $this->HeaderLine('MIME-Version', '1.0');
1448      $result .= $this->GetMailMIME();
1449    }
1450
1451    return $result;
1452  }
1453
1454  /**
1455   * Returns the message MIME.
1456   * @access public
1457   * @return string
1458   */
1459  public function GetMailMIME() {
1460    $result = '';
1461    switch($this->message_type) {
1462      case 'inline':
1463        $result .= $this->HeaderLine('Content-Type', 'multipart/related;');
1464        $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
1465        break;
1466      case 'attach':
1467      case 'inline_attach':
1468      case 'alt_attach':
1469      case 'alt_inline_attach':
1470        $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
1471        $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
1472        break;
1473      case 'alt':
1474      case 'alt_inline':
1475        $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
1476        $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
1477        break;
1478      default:
1479        // Catches case 'plain': and case '':
1480        $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
1481        $result .= $this->TextLine('Content-Type: '.$this->ContentType.'; charset='.$this->CharSet);
1482        break;
1483    }
1484
1485    if($this->Mailer != 'mail') {
1486      $result .= $this->LE;
1487    }
1488
1489    return $result;
1490  }
1491
1492  /**
1493   * Returns the MIME message (headers and body). Only really valid post PreSend().
1494   * @access public
1495   * @return string
1496   */
1497  public function GetSentMIMEMessage() {
1498    return $this->MIMEHeader . $this->mailHeader . self::CRLF . $this->MIMEBody;
1499  }
1500
1501
1502  /**
1503   * Assembles the message body.  Returns an empty string on failure.
1504   * @access public
1505   * @throws phpmailerException
1506   * @return string The assembled message body
1507   */
1508  public function CreateBody() {
1509    $body = '';
1510
1511    if ($this->sign_key_file) {
1512      $body .= $this->GetMailMIME().$this->LE;
1513    }
1514
1515    $this->SetWordWrap();
1516
1517    switch($this->message_type) {
1518      case 'inline':
1519        $body .= $this->GetBoundary($this->boundary[1], '', '', '');
1520        $body .= $this->EncodeString($this->Body, $this->Encoding);
1521        $body .= $this->LE.$this->LE;
1522        $body .= $this->AttachAll("inline", $this->boundary[1]);
1523        break;
1524      case 'attach':
1525        $body .= $this->GetBoundary($this->boundary[1], '', '', '');
1526        $body .= $this->EncodeString($this->Body, $this->Encoding);
1527        $body .= $this->LE.$this->LE;
1528        $body .= $this->AttachAll("attachment", $this->boundary[1]);
1529        break;
1530      case 'inline_attach':
1531        $body .= $this->TextLine("--" . $this->boundary[1]);
1532        $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
1533        $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
1534        $body .= $this->LE;
1535        $body .= $this->GetBoundary($this->boundary[2], '', '', '');
1536        $body .= $this->EncodeString($this->Body, $this->Encoding);
1537        $body .= $this->LE.$this->LE;
1538        $body .= $this->AttachAll("inline", $this->boundary[2]);
1539        $body .= $this->LE;
1540        $body .= $this->AttachAll("attachment", $this->boundary[1]);
1541        break;
1542      case 'alt':
1543        $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
1544        $body .= $this->EncodeString($this->AltBody, $this->Encoding);
1545        $body .= $this->LE.$this->LE;
1546        $body .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
1547        $body .= $this->EncodeString($this->Body, $this->Encoding);
1548        $body .= $this->LE.$this->LE;
1549        $body .= $this->EndBoundary($this->boundary[1]);
1550        break;
1551      case 'alt_inline':
1552        $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
1553        $body .= $this->EncodeString($this->AltBody, $this->Encoding);
1554        $body .= $this->LE.$this->LE;
1555        $body .= $this->TextLine("--" . $this->boundary[1]);
1556        $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
1557        $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
1558        $body .= $this->LE;
1559        $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '');
1560        $body…

Large files files are truncated, but you can click here to view the full file