2 /*~ class.phpmailer.php
 
   3 .---------------------------------------------------------------------------.
 
   4 |  Software: PHPMailer - PHP email class                                    |
 
   6 |      Site: https://github.com/PHPMailer/PHPMailer/                        |
 
   7 | ------------------------------------------------------------------------- |
 
   8 |    Admins: Marcus Bointon                                                 |
 
   9 |    Admins: Jim Jagielski                                                  |
 
  10 |   Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
 
  11 |          : Marcus Bointon (coolbru) phpmailer@synchromedia.co.uk          |
 
  12 |          : Jim Jagielski (jimjag) jimjag@gmail.com                        |
 
  13 |   Founder: Brent R. Matzelle (original founder)                           |
 
  14 | Copyright (c) 2010-2012, Jim Jagielski. All Rights Reserved.              |
 
  15 | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved.               |
 
  16 | Copyright (c) 2001-2003, Brent R. Matzelle                                |
 
  17 | ------------------------------------------------------------------------- |
 
  18 |   License: Distributed under the Lesser General Public License (LGPL)     |
 
  19 |            http://www.gnu.org/copyleft/lesser.html                        |
 
  20 | This program is distributed in the hope that it will be useful - WITHOUT  |
 
  21 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     |
 
  22 | FITNESS FOR A PARTICULAR PURPOSE.                                         |
 
  23 '---------------------------------------------------------------------------'
 
  27  * PHPMailer - PHP email creation and transport class
 
  28  * NOTE: Requires PHP version 5 or later
 
  30  * @author Andy Prevost
 
  31  * @author Marcus Bointon
 
  32  * @author Jim Jagielski
 
  33  * @copyright 2010 - 2012 Jim Jagielski
 
  34  * @copyright 2004 - 2009 Andy Prevost
 
  35  * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 
  38 if (version_compare(PHP_VERSION, '5.0.0', '<') ) exit("Sorry, this version of PHPMailer will only run on PHP version 5 or greater!\n");
 
  41  * PHP email creation and transport class
 
  46   /////////////////////////////////////////////////
 
  48   /////////////////////////////////////////////////
 
  51    * Email priority (1 = High, 3 = Normal, 5 = low).
 
  57    * Sets the CharSet of the message.
 
  60   public $CharSet           = 'iso-8859-1';
 
  63    * Sets the Content-type of the message.
 
  66   public $ContentType       = 'text/plain';
 
  69    * Sets the Encoding of the message. Options for this are
 
  70    *  "8bit", "7bit", "binary", "base64", and "quoted-printable".
 
  73   public $Encoding          = '8bit';
 
  76    * Holds the most recent mailer error message.
 
  79   public $ErrorInfo         = '';
 
  82    * Sets the From email address for the message.
 
  85   public $From              = 'root@localhost';
 
  88    * Sets the From name of the message.
 
  91   public $FromName          = 'Root User';
 
  94    * Sets the Sender email (Return-Path) of the message.  If not empty,
 
  95    * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
 
 101    * Sets the Return-Path of the message.  If empty, it will
 
 102    * be set to either From or Sender.
 
 105   public $ReturnPath        = '';
 
 108    * Sets the Subject of the message.
 
 111   public $Subject           = '';
 
 114    * Sets the Body of the message.  This can be either an HTML or text body.
 
 115    * If HTML then run IsHTML(true).
 
 121    * Sets the text-only body of the message.  This automatically sets the
 
 122    * email to multipart/alternative.  This body can be read by mail
 
 123    * clients that do not have HTML email capability such as mutt. Clients
 
 124    * that can read HTML will view the normal Body.
 
 127   public $AltBody           = '';
 
 130    * Stores the complete compiled MIME message body.
 
 134   protected $MIMEBody       = '';
 
 137    * Stores the complete compiled MIME message headers.
 
 141   protected $MIMEHeader     = '';
 
 144    * Stores the extra header list which CreateHeader() doesn't fold in
 
 148   protected $mailHeader     = '';
 
 151    * Sets word wrapping on the body of the message to a given number of
 
 155   public $WordWrap          = 0;
 
 158    * Method to send mail: ("mail", "sendmail", or "smtp").
 
 161   public $Mailer            = 'mail';
 
 164    * Sets the path of the sendmail program.
 
 167   public $Sendmail          = '/usr/sbin/sendmail';
 
 170    * Determine if mail() uses a fully sendmail compatible MTA that
 
 171    * supports sendmail's "-oi -f" options
 
 174   public $UseSendmailOptions    = true;
 
 177    * Path to PHPMailer plugins.  Useful if the SMTP class
 
 178    * is in a different directory than the PHP include path.
 
 181   public $PluginDir         = '';
 
 184    * Sets the email address that a reading confirmation will be sent.
 
 187   public $ConfirmReadingTo  = '';
 
 190    * Sets the hostname to use in Message-Id and Received headers
 
 191    * and as default HELO string. If empty, the value returned
 
 192    * by SERVER_NAME is used or 'localhost.localdomain'.
 
 195   public $Hostname          = '';
 
 198    * Sets the message ID to be used in the Message-Id header.
 
 199    * If empty, a unique id will be generated.
 
 202   public $MessageID         = '';
 
 205    * Sets the message Date to be used in the Date header.
 
 206    * If empty, the current date will be added.
 
 209   public $MessageDate       = '';
 
 211   /////////////////////////////////////////////////
 
 212   // PROPERTIES FOR SMTP
 
 213   /////////////////////////////////////////////////
 
 216    * Sets the SMTP hosts.
 
 218    * All hosts must be separated by a
 
 219    * semicolon.  You can also specify a different port
 
 220    * for each host by using this format: [hostname:port]
 
 221    * (e.g. "smtp1.example.com:25;smtp2.example.com").
 
 222    * Hosts will be tried in order.
 
 225   public $Host          = 'localhost';
 
 228    * Sets the default SMTP server port.
 
 234    * Sets the SMTP HELO of the message (Default is $Hostname).
 
 240    * Sets connection prefix. Options are "", "ssl" or "tls"
 
 243   public $SMTPSecure    = '';
 
 246    * Sets SMTP authentication. Utilizes the Username and Password variables.
 
 249   public $SMTPAuth      = false;
 
 252    * Sets SMTP username.
 
 255   public $Username      = '';
 
 258    * Sets SMTP password.
 
 261   public $Password      = '';
 
 264    *  Sets SMTP auth type. Options are LOGIN | PLAIN | NTLM | CRAM-MD5 (default LOGIN)
 
 267   public $AuthType      = '';
 
 276    *  Sets SMTP workstation.
 
 279   public $Workstation   = '';
 
 282    * Sets the SMTP server timeout in seconds.
 
 283    * This function will not work with the win32 version.
 
 286   public $Timeout       = 10;
 
 289    * Sets SMTP class debugging on or off.
 
 292   public $SMTPDebug     = false;
 
 295    * Sets the function/method to use for debugging output.
 
 296    * Right now we only honor "echo" or "error_log"
 
 299   public $Debugoutput     = "echo";
 
 302    * Prevents the SMTP connection from being closed after each mail
 
 303    * sending.  If this is set to true then to close the connection
 
 304    * requires an explicit call to SmtpClose().
 
 307   public $SMTPKeepAlive = false;
 
 310    * Provides the ability to have the TO field process individual
 
 311    * emails, instead of sending to entire TO addresses
 
 314   public $SingleTo      = false;
 
 317    * If SingleTo is true, this provides the array to hold the email addresses
 
 320   public $SingleToArray = array();
 
 323    * Should we allow sending messages with empty body?
 
 326   public $AllowEmpty = false;
 
 329    * Provides the ability to change the generic line ending
 
 330    * NOTE: The default remains '\n'. We force CRLF where we KNOW
 
 331    *        it must be used via self::CRLF
 
 337    * Used with DKIM Signing
 
 338    * required parameter if DKIM is enabled
 
 340    * domain selector example domainkey
 
 343   public $DKIM_selector   = '';
 
 346    * Used with DKIM Signing
 
 347    * required if DKIM is enabled, in format of email address 'you@yourdomain.com' typically used as the source of the email
 
 350   public $DKIM_identity   = '';
 
 353    * Used with DKIM Signing
 
 354    * optional parameter if your private key requires a passphras
 
 357   public $DKIM_passphrase   = '';
 
 360    * Used with DKIM Singing
 
 361    * required if DKIM is enabled, in format of email address 'domain.com'
 
 364   public $DKIM_domain     = '';
 
 367    * Used with DKIM Signing
 
 368    * required if DKIM is enabled, path to private key file
 
 371   public $DKIM_private    = '';
 
 374    * Callback Action function name.
 
 375    * The function that handles the result of the send email action.
 
 376    * It is called out by Send() for each email sent.
 
 379    * - 'function_name' for function names
 
 380    * - 'Class::Method' for static method calls
 
 381    * - array($object, 'Method') for calling methods on $object
 
 382    * See http://php.net/is_callable manual page for more details.
 
 385    *   bool    $result        result of the send action
 
 386    *   string  $to            email address of the recipient
 
 387    *   string  $cc            cc email addresses
 
 388    *   string  $bcc           bcc email addresses
 
 389    *   string  $subject       the subject
 
 390    *   string  $body          the email body
 
 391    *   string  $from          email address of sender
 
 394   public $action_function = ''; //'callbackAction';
 
 397    * Sets the PHPMailer Version number
 
 400   public $Version         = '5.2.6';
 
 403    * What to use in the X-Mailer header
 
 404    * @var string NULL for default, whitespace for None, or actual string to use
 
 406   public $XMailer         = '';
 
 408   /////////////////////////////////////////////////
 
 409   // PROPERTIES, PRIVATE AND PROTECTED
 
 410   /////////////////////////////////////////////////
 
 413    * @var SMTP An instance of the SMTP sender class
 
 416   protected   $smtp           = null;
 
 418    * @var array An array of 'to' addresses
 
 421   protected   $to             = array();
 
 423    * @var array An array of 'cc' addresses
 
 426   protected   $cc             = array();
 
 428    * @var array An array of 'bcc' addresses
 
 431   protected   $bcc            = array();
 
 433    * @var array An array of reply-to name and address
 
 436   protected   $ReplyTo        = array();
 
 438    * @var array An array of all kinds of addresses: to, cc, bcc, replyto
 
 441   protected   $all_recipients = array();
 
 443    * @var array An array of attachments
 
 446   protected   $attachment     = array();
 
 448    * @var array An array of custom headers
 
 451   protected   $CustomHeader   = array();
 
 453    * @var string The message's MIME type
 
 456   protected   $message_type   = '';
 
 458    * @var array An array of MIME boundary strings
 
 461   protected   $boundary       = array();
 
 463    * @var array An array of available languages
 
 466   protected   $language       = array();
 
 468    * @var integer The number of errors encountered
 
 471   protected   $error_count    = 0;
 
 473    * @var string The filename of a DKIM certificate file
 
 476   protected   $sign_cert_file = '';
 
 478    * @var string The filename of a DKIM key file
 
 481   protected   $sign_key_file  = '';
 
 483    * @var string The password of a DKIM key
 
 486   protected   $sign_key_pass  = '';
 
 488    * @var boolean Whether to throw exceptions for errors
 
 491   protected   $exceptions     = false;
 
 493   /////////////////////////////////////////////////
 
 495   /////////////////////////////////////////////////
 
 497   const STOP_MESSAGE  = 0; // message only, continue processing
 
 498   const STOP_CONTINUE = 1; // message?, likely ok to continue processing
 
 499   const STOP_CRITICAL = 2; // message, plus full stop, critical error reached
 
 500   const CRLF = "\r\n";     // SMTP RFC specified EOL
 
 502   /////////////////////////////////////////////////
 
 503   // METHODS, VARIABLES
 
 504   /////////////////////////////////////////////////
 
 507    * Calls actual mail() function, but in a safe_mode aware fashion
 
 508    * Also, unless sendmail_path points to sendmail (or something that
 
 509    * claims to be sendmail), don't pass params (not a perfect fix,
 
 511    * @param string $to To
 
 512    * @param string $subject Subject
 
 513    * @param string $body Message Body
 
 514    * @param string $header Additional Header(s)
 
 515    * @param string $params Params
 
 519   private function mail_passthru($to, $subject, $body, $header, $params) {
 
 520     if ( ini_get('safe_mode') || !($this->UseSendmailOptions) ) {
 
 521         $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($subject)), $body, $header);
 
 523         $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($subject)), $body, $header, $params);
 
 529    * Outputs debugging info via user-defined method
 
 532   private function edebug($str) {
 
 533     if ($this->Debugoutput == "error_log") {
 
 542    * @param boolean $exceptions Should we throw external exceptions?
 
 544   public function __construct($exceptions = false) {
 
 545     $this->exceptions = ($exceptions == true);
 
 551   public function __destruct() {
 
 552       if ($this->Mailer == 'smtp') { //Close any open SMTP connection nicely
 
 558    * Sets message type to HTML.
 
 559    * @param bool $ishtml
 
 562   public function IsHTML($ishtml = true) {
 
 564       $this->ContentType = 'text/html';
 
 566       $this->ContentType = 'text/plain';
 
 571    * Sets Mailer to send message using SMTP.
 
 574   public function IsSMTP() {
 
 575     $this->Mailer = 'smtp';
 
 579    * Sets Mailer to send message using PHP mail() function.
 
 582   public function IsMail() {
 
 583     $this->Mailer = 'mail';
 
 587    * Sets Mailer to send message using the $Sendmail program.
 
 590   public function IsSendmail() {
 
 591     if (!stristr(ini_get('sendmail_path'), 'sendmail')) {
 
 592       $this->Sendmail = '/var/qmail/bin/sendmail';
 
 594     $this->Mailer = 'sendmail';
 
 598    * Sets Mailer to send message using the qmail MTA.
 
 601   public function IsQmail() {
 
 602     if (stristr(ini_get('sendmail_path'), 'qmail')) {
 
 603       $this->Sendmail = '/var/qmail/bin/sendmail';
 
 605     $this->Mailer = 'sendmail';
 
 608   /////////////////////////////////////////////////
 
 609   // METHODS, RECIPIENTS
 
 610   /////////////////////////////////////////////////
 
 613    * Adds a "To" address.
 
 614    * @param string $address
 
 615    * @param string $name
 
 616    * @return boolean true on success, false if address already used
 
 618   public function AddAddress($address, $name = '') {
 
 619     return $this->AddAnAddress('to', $address, $name);
 
 623    * Adds a "Cc" address.
 
 624    * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
 
 625    * @param string $address
 
 626    * @param string $name
 
 627    * @return boolean true on success, false if address already used
 
 629   public function AddCC($address, $name = '') {
 
 630     return $this->AddAnAddress('cc', $address, $name);
 
 634    * Adds a "Bcc" address.
 
 635    * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
 
 636    * @param string $address
 
 637    * @param string $name
 
 638    * @return boolean true on success, false if address already used
 
 640   public function AddBCC($address, $name = '') {
 
 641     return $this->AddAnAddress('bcc', $address, $name);
 
 645    * Adds a "Reply-to" address.
 
 646    * @param string $address
 
 647    * @param string $name
 
 650   public function AddReplyTo($address, $name = '') {
 
 651     return $this->AddAnAddress('Reply-To', $address, $name);
 
 655    * Adds an address to one of the recipient arrays
 
 656    * Addresses that have been added already return false, but do not throw exceptions
 
 657    * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
 
 658    * @param string $address The email address to send to
 
 659    * @param string $name
 
 660    * @throws phpmailerException
 
 661    * @return boolean true on success, false if address already used or invalid in some way
 
 664   protected function AddAnAddress($kind, $address, $name = '') {
 
 665     if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) {
 
 666       $this->SetError($this->Lang('Invalid recipient array').': '.$kind);
 
 667       if ($this->exceptions) {
 
 668         throw new phpmailerException('Invalid recipient array: ' . $kind);
 
 670       if ($this->SMTPDebug) {
 
 671         $this->edebug($this->Lang('Invalid recipient array').': '.$kind);
 
 675     $address = trim($address);
 
 676     $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
 
 677     if (!$this->ValidateAddress($address)) {
 
 678       $this->SetError($this->Lang('invalid_address').': '. $address);
 
 679       if ($this->exceptions) {
 
 680         throw new phpmailerException($this->Lang('invalid_address').': '.$address);
 
 682       if ($this->SMTPDebug) {
 
 683         $this->edebug($this->Lang('invalid_address').': '.$address);
 
 687     if ($kind != 'Reply-To') {
 
 688       if (!isset($this->all_recipients[strtolower($address)])) {
 
 689         array_push($this->$kind, array($address, $name));
 
 690         $this->all_recipients[strtolower($address)] = true;
 
 694       if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
 
 695         $this->ReplyTo[strtolower($address)] = array($address, $name);
 
 703    * Set the From and FromName properties
 
 704    * @param string $address
 
 705    * @param string $name
 
 706    * @param int $auto Also set Reply-To and Sender
 
 707    * @throws phpmailerException
 
 710   public function SetFrom($address, $name = '', $auto = 1) {
 
 711     $address = trim($address);
 
 712     $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
 
 713     if (!$this->ValidateAddress($address)) {
 
 714       $this->SetError($this->Lang('invalid_address').': '. $address);
 
 715       if ($this->exceptions) {
 
 716         throw new phpmailerException($this->Lang('invalid_address').': '.$address);
 
 718       if ($this->SMTPDebug) {
 
 719         $this->edebug($this->Lang('invalid_address').': '.$address);
 
 723     $this->From = $address;
 
 724     $this->FromName = $name;
 
 726       if (empty($this->ReplyTo)) {
 
 727         $this->AddAnAddress('Reply-To', $address, $name);
 
 729       if (empty($this->Sender)) {
 
 730         $this->Sender = $address;
 
 737    * Check that a string looks roughly like an email address should
 
 738    * Static so it can be used without instantiation, public so people can overload
 
 739    * Conforms to RFC5322: Uses *correct* regex on which FILTER_VALIDATE_EMAIL is
 
 740    * based; So why not use FILTER_VALIDATE_EMAIL? Because it was broken to
 
 741    * not allow a@b type valid addresses :(
 
 742    * @link http://squiloople.com/2009/12/20/email-address-validation/
 
 743    * @copyright regex Copyright Michael Rushton 2009-10 | http://squiloople.com/ | Feel free to use and redistribute this code. But please keep this copyright notice.
 
 744    * @param string $address The email address to check
 
 749   public static function ValidateAddress($address) {
 
 750       if (defined('PCRE_VERSION')) { //Check this instead of extension_loaded so it works when that function is disabled
 
 751           if (version_compare(PCRE_VERSION, '8.0') >= 0) {
 
 752               return (boolean)preg_match('/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?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][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD', $address);
 
 754               //Fall back to an older regex that doesn't need a recent PCRE
 
 755               return (boolean)preg_match('/^(?!(?>"?(?>\\\[ -~]|[^"])"?){255,})(?!(?>"?(?>\\\[ -~]|[^"])"?){65,}@)(?>[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*")(?>\.(?>[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*"))*@(?>(?![a-z0-9-]{64,})(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>\.(?![a-z0-9-]{64,})(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)){0,126}|\[(?:(?>IPv6:(?>(?>[a-f0-9]{1,4})(?>:[a-f0-9]{1,4}){7}|(?!(?:.*[a-f0-9][:\]]){8,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?::(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?))|(?>(?>IPv6:(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){5}:|(?!(?:.*[a-f0-9]:){6,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4})?::(?>(?:[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4}):)?))?(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}))\])$/isD', $address);
 
 758           //No PCRE! Do something _very_ approximate!
 
 759           //Check the address is 3 chars or longer and contains an @ that's not the first or last char
 
 760           return (strlen($address) >= 3 and strpos($address, '@') >= 1 and strpos($address, '@') != strlen($address) - 1);
 
 764   /////////////////////////////////////////////////
 
 765   // METHODS, MAIL SENDING
 
 766   /////////////////////////////////////////////////
 
 769    * Creates message and assigns Mailer. If the message is
 
 770    * not sent successfully then it returns false.  Use the ErrorInfo
 
 771    * variable to view description of the error.
 
 772    * @throws phpmailerException
 
 775   public function Send() {
 
 777       if(!$this->PreSend()) return false;
 
 778       return $this->PostSend();
 
 779     } catch (phpmailerException $e) {
 
 780       $this->mailHeader = '';
 
 781       $this->SetError($e->getMessage());
 
 782       if ($this->exceptions) {
 
 790    * Prep mail by constructing all message entities
 
 791    * @throws phpmailerException
 
 794   public function PreSend() {
 
 796       $this->mailHeader = "";
 
 797       if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
 
 798         throw new phpmailerException($this->Lang('provide_address'), self::STOP_CRITICAL);
 
 801       // Set whether the message is multipart/alternative
 
 802       if(!empty($this->AltBody)) {
 
 803         $this->ContentType = 'multipart/alternative';
 
 806       $this->error_count = 0; // reset errors
 
 807       $this->SetMessageType();
 
 808       //Refuse to send an empty message unless we are specifically allowing it
 
 809       if (!$this->AllowEmpty and empty($this->Body)) {
 
 810         throw new phpmailerException($this->Lang('empty_message'), self::STOP_CRITICAL);
 
 813       $this->MIMEHeader = $this->CreateHeader();
 
 814       $this->MIMEBody = $this->CreateBody();
 
 816       // To capture the complete message when using mail(), create
 
 817       // an extra header list which CreateHeader() doesn't fold in
 
 818       if ($this->Mailer == 'mail') {
 
 819         if (count($this->to) > 0) {
 
 820           $this->mailHeader .= $this->AddrAppend("To", $this->to);
 
 822           $this->mailHeader .= $this->HeaderLine("To", "undisclosed-recipients:;");
 
 824         $this->mailHeader .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader(trim($this->Subject))));
 
 827       // digitally sign with DKIM if enabled
 
 828       if (!empty($this->DKIM_domain) && !empty($this->DKIM_private) && !empty($this->DKIM_selector) && !empty($this->DKIM_domain) && file_exists($this->DKIM_private)) {
 
 829         $header_dkim = $this->DKIM_Add($this->MIMEHeader . $this->mailHeader, $this->EncodeHeader($this->SecureHeader($this->Subject)), $this->MIMEBody);
 
 830         $this->MIMEHeader = str_replace("\r\n", "\n", $header_dkim) . $this->MIMEHeader;
 
 835     } catch (phpmailerException $e) {
 
 836       $this->SetError($e->getMessage());
 
 837       if ($this->exceptions) {
 
 845    * Actual Email transport function
 
 846    * Send the email via the selected mechanism
 
 847    * @throws phpmailerException
 
 850   public function PostSend() {
 
 852       // Choose the mailer and send through it
 
 853       switch($this->Mailer) {
 
 855           return $this->SendmailSend($this->MIMEHeader, $this->MIMEBody);
 
 857           return $this->SmtpSend($this->MIMEHeader, $this->MIMEBody);
 
 859           return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
 
 861           return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
 
 863     } catch (phpmailerException $e) {
 
 864       $this->SetError($e->getMessage());
 
 865       if ($this->exceptions) {
 
 868       if ($this->SMTPDebug) {
 
 869         $this->edebug($e->getMessage()."\n");
 
 876    * Sends mail using the $Sendmail program.
 
 877    * @param string $header The message headers
 
 878    * @param string $body The message body
 
 879    * @throws phpmailerException
 
 883   protected function SendmailSend($header, $body) {
 
 884     if ($this->Sender != '') {
 
 885       $sendmail = sprintf("%s -oi -f%s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
 
 887       $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
 
 889     if ($this->SingleTo === true) {
 
 890       foreach ($this->SingleToArray as $val) {
 
 891         if(!@$mail = popen($sendmail, 'w')) {
 
 892           throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
 
 894         fputs($mail, "To: " . $val . "\n");
 
 895         fputs($mail, $header);
 
 897         $result = pclose($mail);
 
 898         // implement call back function if it exists
 
 899         $isSent = ($result == 0) ? 1 : 0;
 
 900         $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
 
 902           throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
 
 906       if(!@$mail = popen($sendmail, 'w')) {
 
 907         throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
 
 909       fputs($mail, $header);
 
 911       $result = pclose($mail);
 
 912       // implement call back function if it exists
 
 913       $isSent = ($result == 0) ? 1 : 0;
 
 914       $this->doCallback($isSent, $this->to, $this->cc, $this->bcc, $this->Subject, $body);
 
 916         throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
 
 923    * Sends mail using the PHP mail() function.
 
 924    * @param string $header The message headers
 
 925    * @param string $body The message body
 
 926    * @throws phpmailerException
 
 930   protected function MailSend($header, $body) {
 
 932     foreach($this->to as $t) {
 
 933       $toArr[] = $this->AddrFormat($t);
 
 935     $to = implode(', ', $toArr);
 
 937     if (empty($this->Sender)) {
 
 940       $params = sprintf("-f%s", $this->Sender);
 
 942     if ($this->Sender != '' and !ini_get('safe_mode')) {
 
 943       $old_from = ini_get('sendmail_from');
 
 944       ini_set('sendmail_from', $this->Sender);
 
 947     if ($this->SingleTo === true && count($toArr) > 1) {
 
 948       foreach ($toArr as $val) {
 
 949         $rt = $this->mail_passthru($val, $this->Subject, $body, $header, $params);
 
 950         // implement call back function if it exists
 
 951         $isSent = ($rt == 1) ? 1 : 0;
 
 952         $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
 
 955       $rt = $this->mail_passthru($to, $this->Subject, $body, $header, $params);
 
 956       // implement call back function if it exists
 
 957       $isSent = ($rt == 1) ? 1 : 0;
 
 958       $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body);
 
 960     if (isset($old_from)) {
 
 961       ini_set('sendmail_from', $old_from);
 
 964       throw new phpmailerException($this->Lang('instantiate'), self::STOP_CRITICAL);
 
 970    * Sends mail via SMTP using PhpSMTP
 
 971    * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
 
 972    * @param string $header The message headers
 
 973    * @param string $body The message body
 
 974    * @throws phpmailerException
 
 979   protected function SmtpSend($header, $body) {
 
 980     require_once $this->PluginDir . 'class.smtp.php';
 
 983     if(!$this->SmtpConnect()) {
 
 984       throw new phpmailerException($this->Lang('smtp_connect_failed'), self::STOP_CRITICAL);
 
 986     $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
 
 987     if(!$this->smtp->Mail($smtp_from)) {
 
 988       $this->SetError($this->Lang('from_failed') . $smtp_from . ' : ' .implode(',', $this->smtp->getError()));
 
 989       throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL);
 
 992     // Attempt to send attach all recipients
 
 993     foreach($this->to as $to) {
 
 994       if (!$this->smtp->Recipient($to[0])) {
 
 995         $bad_rcpt[] = $to[0];
 
 996         // implement call back function if it exists
 
 998         $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
 
1000         // implement call back function if it exists
 
1002         $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
 
1005     foreach($this->cc as $cc) {
 
1006       if (!$this->smtp->Recipient($cc[0])) {
 
1007         $bad_rcpt[] = $cc[0];
 
1008         // implement call back function if it exists
 
1010         $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
 
1012         // implement call back function if it exists
 
1014         $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
 
1017     foreach($this->bcc as $bcc) {
 
1018       if (!$this->smtp->Recipient($bcc[0])) {
 
1019         $bad_rcpt[] = $bcc[0];
 
1020         // implement call back function if it exists
 
1022         $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
 
1024         // implement call back function if it exists
 
1026         $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
 
1031     if (count($bad_rcpt) > 0 ) { //Create error message for any bad addresses
 
1032       $badaddresses = implode(', ', $bad_rcpt);
 
1033       throw new phpmailerException($this->Lang('recipients_failed') . $badaddresses);
 
1035     if(!$this->smtp->Data($header . $body)) {
 
1036       throw new phpmailerException($this->Lang('data_not_accepted'), self::STOP_CRITICAL);
 
1038     if($this->SMTPKeepAlive == true) {
 
1039       $this->smtp->Reset();
 
1041         $this->smtp->Quit();
 
1042         $this->smtp->Close();
 
1048    * Initiates a connection to an SMTP server.
 
1049    * Returns false if the operation failed.
 
1052    * @throws phpmailerException
 
1055   public function SmtpConnect() {
 
1056     if(is_null($this->smtp)) {
 
1057       $this->smtp = new SMTP;
 
1060     $this->smtp->Timeout = $this->Timeout;
 
1061     $this->smtp->do_debug = $this->SMTPDebug;
 
1062     $hosts = explode(';', $this->Host);
 
1064     $connection = $this->smtp->Connected();
 
1066     // Retry while there is no connection
 
1068       while($index < count($hosts) && !$connection) {
 
1069         $hostinfo = array();
 
1070         if (preg_match('/^(.+):([0-9]+)$/', $hosts[$index], $hostinfo)) {
 
1071           $host = $hostinfo[1];
 
1072           $port = $hostinfo[2];
 
1074           $host = $hosts[$index];
 
1075           $port = $this->Port;
 
1078         $tls = ($this->SMTPSecure == 'tls');
 
1079         $ssl = ($this->SMTPSecure == 'ssl');
 
1081         if ($this->smtp->Connect(($ssl ? 'ssl://':'').$host, $port, $this->Timeout)) {
 
1083           $hello = ($this->Helo != '' ? $this->Helo : $this->ServerHostname());
 
1084           $this->smtp->Hello($hello);
 
1087             if (!$this->smtp->StartTLS()) {
 
1088               throw new phpmailerException($this->Lang('connect_host'));
 
1091             //We must resend HELO after tls negotiation
 
1092             $this->smtp->Hello($hello);
 
1096           if ($this->SMTPAuth) {
 
1097             if (!$this->smtp->Authenticate($this->Username, $this->Password, $this->AuthType, $this->Realm, $this->Workstation)) {
 
1098               throw new phpmailerException($this->Lang('authenticate'));
 
1104           throw new phpmailerException($this->Lang('connect_host'));
 
1107     } catch (phpmailerException $e) {
 
1108       $this->smtp->Reset();
 
1109       if ($this->exceptions) {
 
1117    * Closes the active SMTP session if one exists.
 
1120   public function SmtpClose() {
 
1121     if ($this->smtp !== null) {
 
1122       if($this->smtp->Connected()) {
 
1123         $this->smtp->Quit();
 
1124         $this->smtp->Close();
 
1130    * Sets the language for all class error messages.
 
1131    * Returns false if it cannot load the language file.  The default language is English.
 
1132    * @param string $langcode ISO 639-1 2-character language code (e.g. Portuguese: "br")
 
1133    * @param string $lang_path Path to the language file directory
 
1137   function SetLanguage($langcode = 'en', $lang_path = 'language/') {
 
1138     //Define full set of translatable strings
 
1139     $PHPMAILER_LANG = array(
 
1140       'authenticate'         => 'SMTP Error: Could not authenticate.',
 
1141       'connect_host'         => 'SMTP Error: Could not connect to SMTP host.',
 
1142       'data_not_accepted'    => 'SMTP Error: Data not accepted.',
 
1143       'empty_message'        => 'Message body empty',
 
1144       'encoding'             => 'Unknown encoding: ',
 
1145       'execute'              => 'Could not execute: ',
 
1146       'file_access'          => 'Could not access file: ',
 
1147       'file_open'            => 'File Error: Could not open file: ',
 
1148       'from_failed'          => 'The following From address failed: ',
 
1149       'instantiate'          => 'Could not instantiate mail function.',
 
1150       'invalid_address'      => 'Invalid address',
 
1151       'mailer_not_supported' => ' mailer is not supported.',
 
1152       'provide_address'      => 'You must provide at least one recipient email address.',
 
1153       'recipients_failed'    => 'SMTP Error: The following recipients failed: ',
 
1154       'signing'              => 'Signing Error: ',
 
1155       'smtp_connect_failed'  => 'SMTP Connect() failed.',
 
1156       'smtp_error'           => 'SMTP server error: ',
 
1157       'variable_set'         => 'Cannot set or reset variable: '
 
1159     //Overwrite language-specific strings. This way we'll never have missing translations - no more "language string failed to load"!
 
1161     if ($langcode != 'en') { //There is no English translation file
 
1162       $l = @include $lang_path.'phpmailer.lang-'.$langcode.'.php';
 
1164     $this->language = $PHPMAILER_LANG;
 
1165     return ($l == true); //Returns false if language not found
 
1169   * Return the current array of language strings
 
1172   public function GetTranslations() {
 
1173     return $this->language;
 
1176   /////////////////////////////////////////////////
 
1177   // METHODS, MESSAGE CREATION
 
1178   /////////////////////////////////////////////////
 
1181    * Creates recipient headers.
 
1183    * @param string $type
 
1184    * @param array $addr
 
1187   public function AddrAppend($type, $addr) {
 
1188     $addr_str = $type . ': ';
 
1189     $addresses = array();
 
1190     foreach ($addr as $a) {
 
1191       $addresses[] = $this->AddrFormat($a);
 
1193     $addr_str .= implode(', ', $addresses);
 
1194     $addr_str .= $this->LE;
 
1200    * Formats an address correctly.
 
1202    * @param string $addr
 
1205   public function AddrFormat($addr) {
 
1206     if (empty($addr[1])) {
 
1207       return $this->SecureHeader($addr[0]);
 
1209       return $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
 
1214    * Wraps message for use with mailers that do not
 
1215    * automatically perform wrapping and for quoted-printable.
 
1216    * Original written by philippe.
 
1217    * @param string $message The message to wrap
 
1218    * @param integer $length The line length to wrap to
 
1219    * @param boolean $qp_mode Whether to run in Quoted-Printable mode
 
1223   public function WrapText($message, $length, $qp_mode = false) {
 
1224     $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
 
1225     // If utf-8 encoding is used, we will need to make sure we don't
 
1226     // split multibyte characters when we wrap
 
1227     $is_utf8 = (strtolower($this->CharSet) == "utf-8");
 
1228     $lelen = strlen($this->LE);
 
1229     $crlflen = strlen(self::CRLF);
 
1231     $message = $this->FixEOL($message);
 
1232     if (substr($message, -$lelen) == $this->LE) {
 
1233       $message = substr($message, 0, -$lelen);
 
1236     $line = explode($this->LE, $message);   // Magic. We know FixEOL uses $LE
 
1238     for ($i = 0 ;$i < count($line); $i++) {
 
1239       $line_part = explode(' ', $line[$i]);
 
1241       for ($e = 0; $e<count($line_part); $e++) {
 
1242         $word = $line_part[$e];
 
1243         if ($qp_mode and (strlen($word) > $length)) {
 
1244           $space_left = $length - strlen($buf) - $crlflen;
 
1246             if ($space_left > 20) {
 
1249                 $len = $this->UTF8CharBoundary($word, $len);
 
1250               } elseif (substr($word, $len - 1, 1) == "=") {
 
1252               } elseif (substr($word, $len - 2, 1) == "=") {
 
1255               $part = substr($word, 0, $len);
 
1256               $word = substr($word, $len);
 
1257               $buf .= ' ' . $part;
 
1258               $message .= $buf . sprintf("=%s", self::CRLF);
 
1260               $message .= $buf . $soft_break;
 
1264           while (strlen($word) > 0) {
 
1270               $len = $this->UTF8CharBoundary($word, $len);
 
1271             } elseif (substr($word, $len - 1, 1) == "=") {
 
1273             } elseif (substr($word, $len - 2, 1) == "=") {
 
1276             $part = substr($word, 0, $len);
 
1277             $word = substr($word, $len);
 
1279             if (strlen($word) > 0) {
 
1280               $message .= $part . sprintf("=%s", self::CRLF);
 
1287           $buf .= ($e == 0) ? $word : (' ' . $word);
 
1289           if (strlen($buf) > $length and $buf_o != '') {
 
1290             $message .= $buf_o . $soft_break;
 
1295       $message .= $buf . self::CRLF;
 
1302    * Finds last character boundary prior to maxLength in a utf-8
 
1303    * quoted (printable) encoded string.
 
1304    * Original written by Colin Brown.
 
1306    * @param string $encodedText utf-8 QP text
 
1307    * @param int    $maxLength   find last character boundary prior to this length
 
1310   public function UTF8CharBoundary($encodedText, $maxLength) {
 
1311     $foundSplitPos = false;
 
1313     while (!$foundSplitPos) {
 
1314       $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
 
1315       $encodedCharPos = strpos($lastChunk, "=");
 
1316       if ($encodedCharPos !== false) {
 
1317         // Found start of encoded character byte within $lookBack block.
 
1318         // Check the encoded byte value (the 2 chars after the '=')
 
1319         $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
 
1320         $dec = hexdec($hex);
 
1321         if ($dec < 128) { // Single byte character.
 
1322           // If the encoded char was found at pos 0, it will fit
 
1323           // otherwise reduce maxLength to start of the encoded char
 
1324           $maxLength = ($encodedCharPos == 0) ? $maxLength :
 
1325           $maxLength - ($lookBack - $encodedCharPos);
 
1326           $foundSplitPos = true;
 
1327         } elseif ($dec >= 192) { // First byte of a multi byte character
 
1328           // Reduce maxLength to split at start of character
 
1329           $maxLength = $maxLength - ($lookBack - $encodedCharPos);
 
1330           $foundSplitPos = true;
 
1331         } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
 
1335         // No encoded character found
 
1336         $foundSplitPos = true;
 
1344    * Set the body wrapping.
 
1348   public function SetWordWrap() {
 
1349     if($this->WordWrap < 1) {
 
1353     switch($this->message_type) {
 
1357       case 'alt_inline_attach':
 
1358         $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
 
1361         $this->Body = $this->WrapText($this->Body, $this->WordWrap);
 
1367    * Assembles message header.
 
1369    * @return string The assembled header
 
1371   public function CreateHeader() {
 
1374     // Set the boundaries
 
1375     $uniq_id = md5(uniqid(time()));
 
1376     $this->boundary[1] = 'b1_' . $uniq_id;
 
1377     $this->boundary[2] = 'b2_' . $uniq_id;
 
1378     $this->boundary[3] = 'b3_' . $uniq_id;
 
1380     if ($this->MessageDate == '') {
 
1381       $result .= $this->HeaderLine('Date', self::RFCDate());
 
1383       $result .= $this->HeaderLine('Date', $this->MessageDate);
 
1386     if ($this->ReturnPath) {
 
1387       $result .= $this->HeaderLine('Return-Path', '<'.trim($this->ReturnPath).'>');
 
1388     } elseif ($this->Sender == '') {
 
1389       $result .= $this->HeaderLine('Return-Path', '<'.trim($this->From).'>');
 
1391       $result .= $this->HeaderLine('Return-Path', '<'.trim($this->Sender).'>');
 
1394     // To be created automatically by mail()
 
1395     if($this->Mailer != 'mail') {
 
1396       if ($this->SingleTo === true) {
 
1397         foreach($this->to as $t) {
 
1398           $this->SingleToArray[] = $this->AddrFormat($t);
 
1401         if(count($this->to) > 0) {
 
1402           $result .= $this->AddrAppend('To', $this->to);
 
1403         } elseif (count($this->cc) == 0) {
 
1404           $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
 
1410     $from[0][0] = trim($this->From);
 
1411     $from[0][1] = $this->FromName;
 
1412     $result .= $this->AddrAppend('From', $from);
 
1414     // sendmail and mail() extract Cc from the header before sending
 
1415     if(count($this->cc) > 0) {
 
1416       $result .= $this->AddrAppend('Cc', $this->cc);
 
1419     // sendmail and mail() extract Bcc from the header before sending
 
1420     if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
 
1421       $result .= $this->AddrAppend('Bcc', $this->bcc);
 
1424     if(count($this->ReplyTo) > 0) {
 
1425       $result .= $this->AddrAppend('Reply-To', $this->ReplyTo);
 
1428     // mail() sets the subject itself
 
1429     if($this->Mailer != 'mail') {
 
1430       $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
 
1433     if($this->MessageID != '') {
 
1434       $result .= $this->HeaderLine('Message-ID', $this->MessageID);
 
1436       $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
 
1438     $result .= $this->HeaderLine('X-Priority', $this->Priority);
 
1439     if ($this->XMailer == '') {
 
1440         $result .= $this->HeaderLine('X-Mailer', 'PHPMailer '.$this->Version.' (https://github.com/PHPMailer/PHPMailer/)');
 
1442       $myXmailer = trim($this->XMailer);
 
1444         $result .= $this->HeaderLine('X-Mailer', $myXmailer);
 
1448     if($this->ConfirmReadingTo != '') {
 
1449       $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
 
1452     // Add custom headers
 
1453     for($index = 0; $index < count($this->CustomHeader); $index++) {
 
1454       $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
 
1456     if (!$this->sign_key_file) {
 
1457       $result .= $this->HeaderLine('MIME-Version', '1.0');
 
1458       $result .= $this->GetMailMIME();
 
1465    * Returns the message MIME.
 
1469   public function GetMailMIME() {
 
1471     switch($this->message_type) {
 
1473         $result .= $this->HeaderLine('Content-Type', 'multipart/related;');
 
1474         $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
 
1477       case 'inline_attach':
 
1479       case 'alt_inline_attach':
 
1480         $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
 
1481         $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
 
1485         $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
 
1486         $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
 
1489         // Catches case 'plain': and case '':
 
1490         $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
 
1491         $result .= $this->TextLine('Content-Type: '.$this->ContentType.'; charset='.$this->CharSet);
 
1495     if($this->Mailer != 'mail') {
 
1496       $result .= $this->LE;
 
1503    * Returns the MIME message (headers and body). Only really valid post PreSend().
 
1507   public function GetSentMIMEMessage() {
 
1508     return $this->MIMEHeader . $this->mailHeader . self::CRLF . $this->MIMEBody;
 
1513    * Assembles the message body.  Returns an empty string on failure.
 
1515    * @throws phpmailerException
 
1516    * @return string The assembled message body
 
1518   public function CreateBody() {
 
1521     if ($this->sign_key_file) {
 
1522       $body .= $this->GetMailMIME().$this->LE;
 
1525     $this->SetWordWrap();
 
1527     switch($this->message_type) {
 
1529         $body .= $this->GetBoundary($this->boundary[1], '', '', '');
 
1530         $body .= $this->EncodeString($this->Body, $this->Encoding);
 
1531         $body .= $this->LE.$this->LE;
 
1532         $body .= $this->AttachAll('inline', $this->boundary[1]);
 
1535         $body .= $this->GetBoundary($this->boundary[1], '', '', '');
 
1536         $body .= $this->EncodeString($this->Body, $this->Encoding);
 
1537         $body .= $this->LE.$this->LE;
 
1538         $body .= $this->AttachAll('attachment', $this->boundary[1]);
 
1540       case 'inline_attach':
 
1541         $body .= $this->TextLine('--' . $this->boundary[1]);
 
1542         $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
 
1543         $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
 
1545         $body .= $this->GetBoundary($this->boundary[2], '', '', '');
 
1546         $body .= $this->EncodeString($this->Body, $this->Encoding);
 
1547         $body .= $this->LE.$this->LE;
 
1548         $body .= $this->AttachAll('inline', $this->boundary[2]);
 
1550         $body .= $this->AttachAll('attachment', $this->boundary[1]);
 
1553         $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
 
1554         $body .= $this->EncodeString($this->AltBody, $this->Encoding);
 
1555         $body .= $this->LE.$this->LE;
 
1556         $body .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
 
1557         $body .= $this->EncodeString($this->Body, $this->Encoding);
 
1558         $body .= $this->LE.$this->LE;
 
1559         $body .= $this->EndBoundary($this->boundary[1]);
 
1562         $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
 
1563         $body .= $this->EncodeString($this->AltBody, $this->Encoding);
 
1564         $body .= $this->LE.$this->LE;
 
1565         $body .= $this->TextLine('--' . $this->boundary[1]);
 
1566         $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
 
1567         $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
 
1569         $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '');
 
1570         $body .= $this->EncodeString($this->Body, $this->Encoding);
 
1571         $body .= $this->LE.$this->LE;
 
1572         $body .= $this->AttachAll('inline', $this->boundary[2]);
 
1574         $body .= $this->EndBoundary($this->boundary[1]);
 
1577         $body .= $this->TextLine('--' . $this->boundary[1]);
 
1578         $body .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
 
1579         $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
 
1581         $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '');
 
1582         $body .= $this->EncodeString($this->AltBody, $this->Encoding);
 
1583         $body .= $this->LE.$this->LE;
 
1584         $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '');
 
1585         $body .= $this->EncodeString($this->Body, $this->Encoding);
 
1586         $body .= $this->LE.$this->LE;
 
1587         $body .= $this->EndBoundary($this->boundary[2]);
 
1589         $body .= $this->AttachAll('attachment', $this->boundary[1]);
 
1591       case 'alt_inline_attach':
 
1592         $body .= $this->TextLine('--' . $this->boundary[1]);
 
1593         $body .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
 
1594         $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
 
1596         $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '');
 
1597         $body .= $this->EncodeString($this->AltBody, $this->Encoding);
 
1598         $body .= $this->LE.$this->LE;
 
1599         $body .= $this->TextLine('--' . $this->boundary[2]);
 
1600         $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
 
1601         $body .= $this->TextLine("\tboundary=\"" . $this->boundary[3] . '"');
 
1603         $body .= $this->GetBoundary($this->boundary[3], '', 'text/html', '');
 
1604         $body .= $this->EncodeString($this->Body, $this->Encoding);
 
1605         $body .= $this->LE.$this->LE;
 
1606         $body .= $this->AttachAll('inline', $this->boundary[3]);
 
1608         $body .= $this->EndBoundary($this->boundary[2]);
 
1610         $body .= $this->AttachAll('attachment', $this->boundary[1]);
 
1613         // catch case 'plain' and case ''
 
1614         $body .= $this->EncodeString($this->Body, $this->Encoding);
 
1618     if ($this->IsError()) {
 
1620     } elseif ($this->sign_key_file) {
 
1622         if (!defined('PKCS7_TEXT')) {
 
1623             throw new phpmailerException($this->Lang('signing').' OpenSSL extension missing.');
 
1625         $file = tempnam(sys_get_temp_dir(), 'mail');
 
1626         file_put_contents($file, $body); //TODO check this worked
 
1627         $signed = tempnam(sys_get_temp_dir(), 'signed');
 
1628         if (@openssl_pkcs7_sign($file, $signed, 'file://'.realpath($this->sign_cert_file), array('file://'.realpath($this->sign_key_file), $this->sign_key_pass), null)) {
 
1630           $body = file_get_contents($signed);
 
1635           throw new phpmailerException($this->Lang('signing').openssl_error_string());
 
1637       } catch (phpmailerException $e) {
 
1639         if ($this->exceptions) {
 
1648    * Returns the start of a message boundary.
 
1650    * @param string $boundary
 
1651    * @param string $charSet
 
1652    * @param string $contentType
 
1653    * @param string $encoding
 
1656   protected function GetBoundary($boundary, $charSet, $contentType, $encoding) {
 
1658     if($charSet == '') {
 
1659       $charSet = $this->CharSet;
 
1661     if($contentType == '') {
 
1662       $contentType = $this->ContentType;
 
1664     if($encoding == '') {
 
1665       $encoding = $this->Encoding;
 
1667     $result .= $this->TextLine('--' . $boundary);
 
1668     $result .= sprintf("Content-Type: %s; charset=%s", $contentType, $charSet);
 
1669     $result .= $this->LE;
 
1670     $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
 
1671     $result .= $this->LE;
 
1677    * Returns the end of a message boundary.
 
1679    * @param string $boundary
 
1682   protected function EndBoundary($boundary) {
 
1683     return $this->LE . '--' . $boundary . '--' . $this->LE;
 
1687    * Sets the message type.
 
1691   protected function SetMessageType() {
 
1692     $this->message_type = array();
 
1693     if($this->AlternativeExists()) $this->message_type[] = "alt";
 
1694     if($this->InlineImageExists()) $this->message_type[] = "inline";
 
1695     if($this->AttachmentExists()) $this->message_type[] = "attach";
 
1696     $this->message_type = implode("_", $this->message_type);
 
1697     if($this->message_type == "") $this->message_type = "plain";
 
1701    * Returns a formatted header line.
 
1703    * @param string $name
 
1704    * @param string $value
 
1707   public function HeaderLine($name, $value) {
 
1708     return $name . ': ' . $value . $this->LE;
 
1712    * Returns a formatted mail line.
 
1714    * @param string $value
 
1717   public function TextLine($value) {
 
1718     return $value . $this->LE;
 
1721   /////////////////////////////////////////////////
 
1722   // CLASS METHODS, ATTACHMENTS
 
1723   /////////////////////////////////////////////////
 
1726    * Adds an attachment from a path on the filesystem.
 
1727    * Returns false if the file could not be found
 
1729    * @param string $path Path to the attachment.
 
1730    * @param string $name Overrides the attachment name.
 
1731    * @param string $encoding File encoding (see $Encoding).
 
1732    * @param string $type File extension (MIME) type.
 
1733    * @throws phpmailerException
 
1736   public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
 
1738       if ( !@is_file($path) ) {
 
1739         throw new phpmailerException($this->Lang('file_access') . $path, self::STOP_CONTINUE);
 
1741       $filename = basename($path);
 
1742       if ( $name == '' ) {
 
1746       $this->attachment[] = array(
 
1752         5 => false,  // isStringAttachment
 
1757     } catch (phpmailerException $e) {
 
1758       $this->SetError($e->getMessage());
 
1759       if ($this->exceptions) {
 
1762       if ($this->SMTPDebug) {
 
1763         $this->edebug($e->getMessage()."\n");
 
1765       if ( $e->getCode() == self::STOP_CRITICAL ) {
 
1773   * Return the current array of attachments
 
1776   public function GetAttachments() {
 
1777     return $this->attachment;
 
1781    * Attaches all fs, string, and binary attachments to the message.
 
1782    * Returns an empty string on failure.
 
1784    * @param string $disposition_type
 
1785    * @param string $boundary
 
1788   protected function AttachAll($disposition_type, $boundary) {
 
1789     // Return text of body
 
1794     // Add all attachments
 
1795     foreach ($this->attachment as $attachment) {
 
1796       // CHECK IF IT IS A VALID DISPOSITION_FILTER
 
1797       if($attachment[6] == $disposition_type) {
 
1798         // Check for string attachment
 
1801         $bString = $attachment[5];
 
1803           $string = $attachment[0];
 
1805           $path = $attachment[0];
 
1808         $inclhash = md5(serialize($attachment));
 
1809         if (in_array($inclhash, $incl)) { continue; }
 
1810         $incl[]      = $inclhash;
 
1811         $filename    = $attachment[1];
 
1812         $name        = $attachment[2];
 
1813         $encoding    = $attachment[3];
 
1814         $type        = $attachment[4];
 
1815         $disposition = $attachment[6];
 
1816         $cid         = $attachment[7];
 
1817         if ( $disposition == 'inline' && isset($cidUniq[$cid]) ) { continue; }
 
1818         $cidUniq[$cid] = true;
 
1820         $mime[] = sprintf("--%s%s", $boundary, $this->LE);
 
1821         $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);
 
1822         $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
 
1824         if($disposition == 'inline') {
 
1825           $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
 
1828         $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);
 
1830         // Encode as string attachment
 
1832           $mime[] = $this->EncodeString($string, $encoding);
 
1833           if($this->IsError()) {
 
1836           $mime[] = $this->LE.$this->LE;
 
1838           $mime[] = $this->EncodeFile($path, $encoding);
 
1839           if($this->IsError()) {
 
1842           $mime[] = $this->LE.$this->LE;
 
1847     $mime[] = sprintf("--%s--%s", $boundary, $this->LE);
 
1849     return implode("", $mime);
 
1853    * Encodes attachment in requested format.
 
1854    * Returns an empty string on failure.
 
1855    * @param string $path The full path to the file
 
1856    * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
 
1857    * @throws phpmailerException
 
1862   protected function EncodeFile($path, $encoding = 'base64') {
 
1864       if (!is_readable($path)) {
 
1865         throw new phpmailerException($this->Lang('file_open') . $path, self::STOP_CONTINUE);
 
1867       $magic_quotes = get_magic_quotes_runtime();
 
1868       if ($magic_quotes) {
 
1869         if (version_compare(PHP_VERSION, '5.3.0', '<')) {
 
1870           set_magic_quotes_runtime(0);
 
1872           ini_set('magic_quotes_runtime', 0);
 
1875       $file_buffer  = file_get_contents($path);
 
1876       $file_buffer  = $this->EncodeString($file_buffer, $encoding);
 
1877       if ($magic_quotes) {
 
1878         if (version_compare(PHP_VERSION, '5.3.0', '<')) {
 
1879           set_magic_quotes_runtime($magic_quotes);
 
1881           ini_set('magic_quotes_runtime', $magic_quotes);
 
1884       return $file_buffer;
 
1885     } catch (Exception $e) {
 
1886       $this->SetError($e->getMessage());
 
1892    * Encodes string to requested format.
 
1893    * Returns an empty string on failure.
 
1894    * @param string $str The text to encode
 
1895    * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
 
1899   public function EncodeString($str, $encoding = 'base64') {
 
1901     switch(strtolower($encoding)) {
 
1903         $encoded = chunk_split(base64_encode($str), 76, $this->LE);
 
1907         $encoded = $this->FixEOL($str);
 
1908         //Make sure it ends with a line break
 
1909         if (substr($encoded, -(strlen($this->LE))) != $this->LE)
 
1910           $encoded .= $this->LE;
 
1915       case 'quoted-printable':
 
1916         $encoded = $this->EncodeQP($str);
 
1919         $this->SetError($this->Lang('encoding') . $encoding);
 
1926    * Encode a header string to best (shortest) of Q, B, quoted or none.
 
1928    * @param string $str
 
1929    * @param string $position
 
1932   public function EncodeHeader($str, $position = 'text') {
 
1935     switch (strtolower($position)) {
 
1937         if (!preg_match('/[\200-\377]/', $str)) {
 
1938           // Can't use addslashes as we don't know what value has magic_quotes_sybase
 
1939           $encoded = addcslashes($str, "\0..\37\177\\\"");
 
1940           if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
 
1943             return ("\"$encoded\"");
 
1946         $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
 
1949         $x = preg_match_all('/[()"]/', $str, $matches);
 
1953         $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
 
1957     if ($x == 0) { //There are no chars that need encoding
 
1961     $maxlen = 75 - 7 - strlen($this->CharSet);
 
1962     // Try to select the encoding which should produce the shortest output
 
1963     if ($x > strlen($str)/3) { //More than a third of the content will need encoding, so B encoding will be most efficient
 
1965       if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
 
1966         // Use a custom function which correctly encodes and wraps long
 
1967         // multibyte strings without breaking lines within a character
 
1968         $encoded = $this->Base64EncodeWrapMB($str, "\n");
 
1970         $encoded = base64_encode($str);
 
1971         $maxlen -= $maxlen % 4;
 
1972         $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
 
1976       $encoded = $this->EncodeQ($str, $position);
 
1977       $encoded = $this->WrapText($encoded, $maxlen, true);
 
1978       $encoded = str_replace('='.self::CRLF, "\n", trim($encoded));
 
1981     $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
 
1982     $encoded = trim(str_replace("\n", $this->LE, $encoded));
 
1988    * Checks if a string contains multibyte characters.
 
1990    * @param string $str multi-byte text to wrap encode
 
1993   public function HasMultiBytes($str) {
 
1994     if (function_exists('mb_strlen')) {
 
1995       return (strlen($str) > mb_strlen($str, $this->CharSet));
 
1996     } else { // Assume no multibytes (we can't handle without mbstring functions anyway)
 
2002    * Correctly encodes and wraps long multibyte strings for mail headers
 
2003    * without breaking lines within a character.
 
2004    * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
 
2006    * @param string $str multi-byte text to wrap encode
 
2007    * @param string $lf string to use as linefeed/end-of-line
 
2010   public function Base64EncodeWrapMB($str, $lf=null) {
 
2011     $start = "=?".$this->CharSet."?B?";
 
2018     $mb_length = mb_strlen($str, $this->CharSet);
 
2019     // Each line must have length <= 75, including $start and $end
 
2020     $length = 75 - strlen($start) - strlen($end);
 
2021     // Average multi-byte ratio
 
2022     $ratio = $mb_length / strlen($str);
 
2023     // Base64 has a 4:3 ratio
 
2024     $offset = $avgLength = floor($length * $ratio * .75);
 
2026     for ($i = 0; $i < $mb_length; $i += $offset) {
 
2030         $offset = $avgLength - $lookBack;
 
2031         $chunk = mb_substr($str, $i, $offset, $this->CharSet);
 
2032         $chunk = base64_encode($chunk);
 
2035       while (strlen($chunk) > $length);
 
2037       $encoded .= $chunk . $lf;
 
2040     // Chomp the last linefeed
 
2041     $encoded = substr($encoded, 0, -strlen($lf));
 
2046    * Encode string to RFC2045 (6.7) quoted-printable format
 
2048    * @param string $string The text to encode
 
2049    * @param integer $line_max Number of chars allowed on a line before wrapping
 
2051    * @link PHP version adapted from http://www.php.net/manual/en/function.quoted-printable-decode.php#89417
 
2053   public function EncodeQP($string, $line_max = 76) {
 
2054     if (function_exists('quoted_printable_encode')) { //Use native function if it's available (>= PHP5.3)
 
2055       return quoted_printable_encode($string);
 
2057     //Fall back to a pure PHP implementation
 
2058     $string = str_replace(array('%20', '%0D%0A.', '%0D%0A', '%'), array(' ', "\r\n=2E", "\r\n", '='), rawurlencode($string));
 
2059     $string = preg_replace('/[^\r\n]{'.($line_max - 3).'}[^=\r\n]{2}/', "$0=\r\n", $string);
 
2064    * Wrapper to preserve BC for old QP encoding function that was removed
 
2067    * @param string $string
 
2068    * @param integer $line_max
 
2069    * @param bool $space_conv
 
2072   public function EncodeQPphp($string, $line_max = 76, $space_conv = false) {
 
2073     return $this->EncodeQP($string, $line_max);
 
2077    * Encode string to q encoding.
 
2078    * @link http://tools.ietf.org/html/rfc2047
 
2079    * @param string $str the text to encode
 
2080    * @param string $position Where the text is going to be used, see the RFC for what that means
 
2084   public function EncodeQ($str, $position = 'text') {
 
2085     //There should not be any EOL in the string
 
2087     $encoded = str_replace(array("\r", "\n"), '', $str);
 
2088     switch (strtolower($position)) {
 
2090         $pattern = '^A-Za-z0-9!*+\/ -';
 
2095         //note that we don't break here!
 
2096         //for this reason we build the $pattern without including delimiters and []
 
2100         //Replace every high ascii, control =, ? and _ characters
 
2101         //We put \075 (=) as first value to make sure it's the first one in being converted, preventing double encode
 
2102         $pattern = '\075\000-\011\013\014\016-\037\077\137\177-\377' . $pattern;
 
2106     if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) {
 
2107       foreach (array_unique($matches[0]) as $char) {
 
2108         $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded);
 
2112     //Replace every spaces to _ (more readable than =20)
 
2113     return str_replace(' ', '_', $encoded);
 
2118    * Adds a string or binary attachment (non-filesystem) to the list.
 
2119    * This method can be used to attach ascii or binary data,
 
2120    * such as a BLOB record from a database.
 
2121    * @param string $string String attachment data.
 
2122    * @param string $filename Name of the attachment.
 
2123    * @param string $encoding File encoding (see $Encoding).
 
2124    * @param string $type File extension (MIME) type.
 
2127   public function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
 
2128     // Append to $attachment array
 
2129     $this->attachment[] = array(
 
2132       2 => basename($filename),
 
2135       5 => true,  // isStringAttachment
 
2142    * Add an embedded attachment from a file.
 
2143    * This can include images, sounds, and just about any other document type.
 
2144    * Be sure to set the $type to an image type for images:
 
2145    * JPEG images use 'image/jpeg', GIF uses 'image/gif', PNG uses 'image/png'.
 
2146    * @param string $path Path to the attachment.
 
2147    * @param string $cid Content ID of the attachment; Use this to reference
 
2148    *        the content when using an embedded image in HTML.
 
2149    * @param string $name Overrides the attachment name.
 
2150    * @param string $encoding File encoding (see $Encoding).
 
2151    * @param string $type File MIME type.
 
2152    * @return bool True on successfully adding an attachment
 
2154   public function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
 
2156     if ( !@is_file($path) ) {
 
2157       $this->SetError($this->Lang('file_access') . $path);
 
2161     $filename = basename($path);
 
2162     if ( $name == '' ) {
 
2166     // Append to $attachment array
 
2167     $this->attachment[] = array(
 
2173       5 => false,  // isStringAttachment
 
2182    * Add an embedded stringified attachment.
 
2183    * This can include images, sounds, and just about any other document type.
 
2184    * Be sure to set the $type to an image type for images:
 
2185    * JPEG images use 'image/jpeg', GIF uses 'image/gif', PNG uses 'image/png'.
 
2186    * @param string $string The attachment binary data.
 
2187    * @param string $cid Content ID of the attachment; Use this to reference
 
2188    *        the content when using an embedded image in HTML.
 
2189    * @param string $name
 
2190    * @param string $encoding File encoding (see $Encoding).
 
2191    * @param string $type MIME type.
 
2192    * @return bool True on successfully adding an attachment
 
2194   public function AddStringEmbeddedImage($string, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
 
2195     // Append to $attachment array
 
2196     $this->attachment[] = array(
 
2202       5 => true,  // isStringAttachment
 
2210    * Returns true if an inline attachment is present.
 
2214   public function InlineImageExists() {
 
2215     foreach($this->attachment as $attachment) {
 
2216       if ($attachment[6] == 'inline') {
 
2224    * Returns true if an attachment (non-inline) is present.
 
2227   public function AttachmentExists() {
 
2228     foreach($this->attachment as $attachment) {
 
2229       if ($attachment[6] == 'attachment') {
 
2237    * Does this message have an alternative body set?
 
2240   public function AlternativeExists() {
 
2241     return !empty($this->AltBody);
 
2244   /////////////////////////////////////////////////
 
2245   // CLASS METHODS, MESSAGE RESET
 
2246   /////////////////////////////////////////////////
 
2249    * Clears all recipients assigned in the TO array.  Returns void.
 
2252   public function ClearAddresses() {
 
2253     foreach($this->to as $to) {
 
2254       unset($this->all_recipients[strtolower($to[0])]);
 
2256     $this->to = array();
 
2260    * Clears all recipients assigned in the CC array.  Returns void.
 
2263   public function ClearCCs() {
 
2264     foreach($this->cc as $cc) {
 
2265       unset($this->all_recipients[strtolower($cc[0])]);
 
2267     $this->cc = array();
 
2271    * Clears all recipients assigned in the BCC array.  Returns void.
 
2274   public function ClearBCCs() {
 
2275     foreach($this->bcc as $bcc) {
 
2276       unset($this->all_recipients[strtolower($bcc[0])]);
 
2278     $this->bcc = array();
 
2282    * Clears all recipients assigned in the ReplyTo array.  Returns void.
 
2285   public function ClearReplyTos() {
 
2286     $this->ReplyTo = array();
 
2290    * Clears all recipients assigned in the TO, CC and BCC
 
2291    * array.  Returns void.
 
2294   public function ClearAllRecipients() {
 
2295     $this->to = array();
 
2296     $this->cc = array();
 
2297     $this->bcc = array();
 
2298     $this->all_recipients = array();
 
2302    * Clears all previously set filesystem, string, and binary
 
2303    * attachments.  Returns void.
 
2306   public function ClearAttachments() {
 
2307     $this->attachment = array();
 
2311    * Clears all custom headers.  Returns void.
 
2314   public function ClearCustomHeaders() {
 
2315     $this->CustomHeader = array();
 
2318   /////////////////////////////////////////////////
 
2319   // CLASS METHODS, MISCELLANEOUS
 
2320   /////////////////////////////////////////////////
 
2323    * Adds the error message to the error container.
 
2325    * @param string $msg
 
2328   protected function SetError($msg) {
 
2329     $this->error_count++;
 
2330     if ($this->Mailer == 'smtp' and !is_null($this->smtp)) {
 
2331       $lasterror = $this->smtp->getError();
 
2332       if (!empty($lasterror) and array_key_exists('smtp_msg', $lasterror)) {
 
2333         $msg .= '<p>' . $this->Lang('smtp_error') . $lasterror['smtp_msg'] . "</p>\n";
 
2336     $this->ErrorInfo = $msg;
 
2340    * Returns the proper RFC 822 formatted date.
 
2345   public static function RFCDate() {
 
2346     //Set the time zone to whatever the default is to avoid 500 errors
 
2347     //Will default to UTC if it's not set properly in php.ini
 
2348     date_default_timezone_set(@date_default_timezone_get());
 
2349     return date('D, j M Y H:i:s O');
 
2353    * Returns the server hostname or 'localhost.localdomain' if unknown.
 
2357   protected function ServerHostname() {
 
2358     if (!empty($this->Hostname)) {
 
2359       $result = $this->Hostname;
 
2360     } elseif (isset($_SERVER['SERVER_NAME'])) {
 
2361       $result = $_SERVER['SERVER_NAME'];
 
2363       $result = 'localhost.localdomain';
 
2370    * Returns a message in the appropriate language.
 
2372    * @param string $key
 
2375   protected function Lang($key) {
 
2376     if(count($this->language) < 1) {
 
2377       $this->SetLanguage('en'); // set the default language
 
2380     if(isset($this->language[$key])) {
 
2381       return $this->language[$key];
 
2383       return 'Language string failed to load: ' . $key;
 
2388    * Returns true if an error occurred.
 
2392   public function IsError() {
 
2393     return ($this->error_count > 0);
 
2397    * Changes every end of line from CRLF, CR or LF to $this->LE.
 
2399    * @param string $str String to FixEOL
 
2402   public function FixEOL($str) {
 
2403         // condense down to \n
 
2404         $nstr = str_replace(array("\r\n", "\r"), "\n", $str);
 
2405         // Now convert LE as needed
 
2406         if ($this->LE !== "\n") {
 
2407                 $nstr = str_replace("\n", $this->LE, $nstr);
 
2413    * Adds a custom header. $name value can be overloaded to contain
 
2414    * both header name and value (name:value)
 
2416    * @param string $name custom header name
 
2417    * @param string $value header value
 
2420   public function AddCustomHeader($name, $value=null) {
 
2421         if ($value === null) {
 
2422                 // Value passed in as name:value
 
2423                 $this->CustomHeader[] = explode(':', $name, 2);
 
2425                 $this->CustomHeader[] = array($name, $value);
 
2430    * Creates a message from an HTML string, making modifications for inline images and backgrounds
 
2431    * and creates a plain-text version by converting the HTML
 
2432    * Overwrites any existing values in $this->Body and $this->AltBody
 
2434    * @param string $message HTML message string
 
2435    * @param string $basedir baseline directory for path
 
2436    * @param bool $advanced Whether to use the advanced HTML to text converter
 
2437    * @return string $message
 
2439   public function MsgHTML($message, $basedir = '', $advanced = false) {
 
2440     preg_match_all("/(src|background)=[\"'](.*)[\"']/Ui", $message, $images);
 
2441     if(isset($images[2])) {
 
2442       foreach($images[2] as $i => $url) {
 
2443         // do not change urls for absolute images (thanks to corvuscorax)
 
2444         if (!preg_match('#^[A-z]+://#', $url)) {
 
2445           $filename = basename($url);
 
2446           $directory = dirname($url);
 
2447           if ($directory == '.') {
 
2450           $cid = 'cid:' . md5($url);
 
2451           $ext = pathinfo($filename, PATHINFO_EXTENSION);
 
2452           $mimeType  = self::_mime_types($ext);
 
2453           if ( strlen($basedir) > 1 && substr($basedir, -1) != '/') { $basedir .= '/'; }
 
2454           if ( strlen($directory) > 1 && substr($directory, -1) != '/') { $directory .= '/'; }
 
2455           if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($url), $filename, 'base64', $mimeType) ) {
 
2456             $message = preg_replace("/".$images[1][$i]."=[\"']".preg_quote($url, '/')."[\"']/Ui", $images[1][$i]."=\"".$cid."\"", $message);
 
2461     $this->IsHTML(true);
 
2462     $this->Body = $message;
 
2463     $this->AltBody = $this->html2text($message, $advanced);
 
2464     if (empty($this->AltBody)) {
 
2465       $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . "\n\n";
 
2471      * Convert an HTML string into a plain text version
 
2472      * @param string $html The HTML text to convert
 
2473      * @param bool $advanced Should this use the more complex html2text converter or just a simple one?
 
2476   public function html2text($html, $advanced = false) {
 
2478       require_once 'extras/class.html2text.php';
 
2479       $h = new html2text($html);
 
2480       return $h->get_text();
 
2482     return html_entity_decode(trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s', '', $html))), ENT_QUOTES, $this->CharSet);
 
2486    * Gets the MIME type of the embedded or inline image
 
2487    * @param string $ext File extension
 
2489    * @return string MIME type of ext
 
2492   public static function _mime_types($ext = '') {
 
2494       'xl'    =>  'application/excel',
 
2495       'hqx'   =>  'application/mac-binhex40',
 
2496       'cpt'   =>  'application/mac-compactpro',
 
2497       'bin'   =>  'application/macbinary',
 
2498       'doc'   =>  'application/msword',
 
2499       'word'  =>  'application/msword',
 
2500       'class' =>  'application/octet-stream',
 
2501       'dll'   =>  'application/octet-stream',
 
2502       'dms'   =>  'application/octet-stream',
 
2503       'exe'   =>  'application/octet-stream',
 
2504       'lha'   =>  'application/octet-stream',
 
2505       'lzh'   =>  'application/octet-stream',
 
2506       'psd'   =>  'application/octet-stream',
 
2507       'sea'   =>  'application/octet-stream',
 
2508       'so'    =>  'application/octet-stream',
 
2509       'oda'   =>  'application/oda',
 
2510       'pdf'   =>  'application/pdf',
 
2511       'ai'    =>  'application/postscript',
 
2512       'eps'   =>  'application/postscript',
 
2513       'ps'    =>  'application/postscript',
 
2514       'smi'   =>  'application/smil',
 
2515       'smil'  =>  'application/smil',
 
2516       'mif'   =>  'application/vnd.mif',
 
2517       'xls'   =>  'application/vnd.ms-excel',
 
2518       'ppt'   =>  'application/vnd.ms-powerpoint',
 
2519       'wbxml' =>  'application/vnd.wap.wbxml',
 
2520       'wmlc'  =>  'application/vnd.wap.wmlc',
 
2521       'dcr'   =>  'application/x-director',
 
2522       'dir'   =>  'application/x-director',
 
2523       'dxr'   =>  'application/x-director',
 
2524       'dvi'   =>  'application/x-dvi',
 
2525       'gtar'  =>  'application/x-gtar',
 
2526       'php3'  =>  'application/x-httpd-php',
 
2527       'php4'  =>  'application/x-httpd-php',
 
2528       'php'   =>  'application/x-httpd-php',
 
2529       'phtml' =>  'application/x-httpd-php',
 
2530       'phps'  =>  'application/x-httpd-php-source',
 
2531       'js'    =>  'application/x-javascript',
 
2532       'swf'   =>  'application/x-shockwave-flash',
 
2533       'sit'   =>  'application/x-stuffit',
 
2534       'tar'   =>  'application/x-tar',
 
2535       'tgz'   =>  'application/x-tar',
 
2536       'xht'   =>  'application/xhtml+xml',
 
2537       'xhtml' =>  'application/xhtml+xml',
 
2538       'zip'   =>  'application/zip',
 
2539       'mid'   =>  'audio/midi',
 
2540       'midi'  =>  'audio/midi',
 
2541       'mp2'   =>  'audio/mpeg',
 
2542       'mp3'   =>  'audio/mpeg',
 
2543       'mpga'  =>  'audio/mpeg',
 
2544       'aif'   =>  'audio/x-aiff',
 
2545       'aifc'  =>  'audio/x-aiff',
 
2546       'aiff'  =>  'audio/x-aiff',
 
2547       'ram'   =>  'audio/x-pn-realaudio',
 
2548       'rm'    =>  'audio/x-pn-realaudio',
 
2549       'rpm'   =>  'audio/x-pn-realaudio-plugin',
 
2550       'ra'    =>  'audio/x-realaudio',
 
2551       'wav'   =>  'audio/x-wav',
 
2552       'bmp'   =>  'image/bmp',
 
2553       'gif'   =>  'image/gif',
 
2554       'jpeg'  =>  'image/jpeg',
 
2555       'jpe'   =>  'image/jpeg',
 
2556       'jpg'   =>  'image/jpeg',
 
2557       'png'   =>  'image/png',
 
2558       'tiff'  =>  'image/tiff',
 
2559       'tif'   =>  'image/tiff',
 
2560       'eml'   =>  'message/rfc822',
 
2561       'css'   =>  'text/css',
 
2562       'html'  =>  'text/html',
 
2563       'htm'   =>  'text/html',
 
2564       'shtml' =>  'text/html',
 
2565       'log'   =>  'text/plain',
 
2566       'text'  =>  'text/plain',
 
2567       'txt'   =>  'text/plain',
 
2568       'rtx'   =>  'text/richtext',
 
2569       'rtf'   =>  'text/rtf',
 
2570       'xml'   =>  'text/xml',
 
2571       'xsl'   =>  'text/xml',
 
2572       'mpeg'  =>  'video/mpeg',
 
2573       'mpe'   =>  'video/mpeg',
 
2574       'mpg'   =>  'video/mpeg',
 
2575       'mov'   =>  'video/quicktime',
 
2576       'qt'    =>  'video/quicktime',
 
2577       'rv'    =>  'video/vnd.rn-realvideo',
 
2578       'avi'   =>  'video/x-msvideo',
 
2579       'movie' =>  'video/x-sgi-movie'
 
2581     return (!isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)];
 
2585    * Set (or reset) Class Objects (variables)
 
2588    * $page->set('X-Priority', '3');
 
2591    * @param string $name
 
2592    * @param mixed $value
 
2593    * NOTE: will not work with arrays, there are no arrays to set/reset
 
2594    * @throws phpmailerException
 
2596    * @todo Should this not be using __set() magic function?
 
2598   public function set($name, $value = '') {
 
2600       if (isset($this->$name) ) {
 
2601         $this->$name = $value;
 
2603         throw new phpmailerException($this->Lang('variable_set') . $name, self::STOP_CRITICAL);
 
2605     } catch (Exception $e) {
 
2606       $this->SetError($e->getMessage());
 
2607       if ($e->getCode() == self::STOP_CRITICAL) {
 
2615    * Strips newlines to prevent header injection.
 
2617    * @param string $str
 
2620   public function SecureHeader($str) {
 
2621     return trim(str_replace(array("\r", "\n"), '', $str));
 
2625    * Set the private key file and password to sign the message.
 
2628    * @param string $cert_filename
 
2629    * @param string $key_filename
 
2630    * @param string $key_pass Password for private key
 
2632   public function Sign($cert_filename, $key_filename, $key_pass) {
 
2633     $this->sign_cert_file = $cert_filename;
 
2634     $this->sign_key_file = $key_filename;
 
2635     $this->sign_key_pass = $key_pass;
 
2639    * Set the private key file and password to sign the message.
 
2642    * @param string $txt
 
2645   public function DKIM_QP($txt) {
 
2647     for ($i = 0; $i < strlen($txt); $i++) {
 
2648       $ord = ord($txt[$i]);
 
2649       if ( ((0x21 <= $ord) && ($ord <= 0x3A)) || $ord == 0x3C || ((0x3E <= $ord) && ($ord <= 0x7E)) ) {
 
2652         $line .= "=".sprintf("%02X", $ord);
 
2659    * Generate DKIM signature
 
2662    * @param string $s Header
 
2663    * @throws phpmailerException
 
2666   public function DKIM_Sign($s) {
 
2667     if (!defined('PKCS7_TEXT')) {
 
2668         if ($this->exceptions) {
 
2669             throw new phpmailerException($this->Lang("signing").' OpenSSL extension missing.');
 
2673     $privKeyStr = file_get_contents($this->DKIM_private);
 
2674     if ($this->DKIM_passphrase != '') {
 
2675       $privKey = openssl_pkey_get_private($privKeyStr, $this->DKIM_passphrase);
 
2677       $privKey = $privKeyStr;
 
2679     if (openssl_sign($s, $signature, $privKey)) {
 
2680       return base64_encode($signature);
 
2686    * Generate DKIM Canonicalization Header
 
2689    * @param string $s Header
 
2692   public function DKIM_HeaderC($s) {
 
2693     $s = preg_replace("/\r\n\s+/", " ", $s);
 
2694     $lines = explode("\r\n", $s);
 
2695     foreach ($lines as $key => $line) {
 
2696       list($heading, $value) = explode(":", $line, 2);
 
2697       $heading = strtolower($heading);
 
2698       $value = preg_replace("/\s+/", " ", $value) ; // Compress useless spaces
 
2699       $lines[$key] = $heading.":".trim($value) ; // Don't forget to remove WSP around the value
 
2701     $s = implode("\r\n", $lines);
 
2706    * Generate DKIM Canonicalization Body
 
2709    * @param string $body Message Body
 
2712   public function DKIM_BodyC($body) {
 
2713     if ($body == '') return "\r\n";
 
2714     // stabilize line endings
 
2715     $body = str_replace("\r\n", "\n", $body);
 
2716     $body = str_replace("\n", "\r\n", $body);
 
2717     // END stabilize line endings
 
2718     while (substr($body, strlen($body) - 4, 4) == "\r\n\r\n") {
 
2719       $body = substr($body, 0, strlen($body) - 2);
 
2725    * Create the DKIM header, body, as new header
 
2728    * @param string $headers_line Header lines
 
2729    * @param string $subject Subject
 
2730    * @param string $body Body
 
2733   public function DKIM_Add($headers_line, $subject, $body) {
 
2734     $DKIMsignatureType    = 'rsa-sha1'; // Signature & hash algorithms
 
2735     $DKIMcanonicalization = 'relaxed/simple'; // Canonicalization of header/body
 
2736     $DKIMquery            = 'dns/txt'; // Query method
 
2737     $DKIMtime             = time() ; // Signature Timestamp = seconds since 00:00:00 - Jan 1, 1970 (UTC time zone)
 
2738     $subject_header       = "Subject: $subject";
 
2739     $headers              = explode($this->LE, $headers_line);
 
2743     foreach($headers as $header) {
 
2744       if (strpos($header, 'From:') === 0) {
 
2745         $from_header = $header;
 
2746         $current = 'from_header';
 
2747       } elseif (strpos($header, 'To:') === 0) {
 
2748         $to_header = $header;
 
2749         $current = 'to_header';
 
2751         if($current && strpos($header, ' =?') === 0){
 
2752           $$current .= $header;
 
2758     $from     = str_replace('|', '=7C', $this->DKIM_QP($from_header));
 
2759     $to       = str_replace('|', '=7C', $this->DKIM_QP($to_header));
 
2760     $subject  = str_replace('|', '=7C', $this->DKIM_QP($subject_header)) ; // Copied header fields (dkim-quoted-printable
 
2761     $body     = $this->DKIM_BodyC($body);
 
2762     $DKIMlen  = strlen($body) ; // Length of body
 
2763     $DKIMb64  = base64_encode(pack("H*", sha1($body))) ; // Base64 of packed binary SHA-1 hash of body
 
2764     $ident    = ($this->DKIM_identity == '')? '' : " i=" . $this->DKIM_identity . ";";
 
2765     $dkimhdrs = "DKIM-Signature: v=1; a=" . $DKIMsignatureType . "; q=" . $DKIMquery . "; l=" . $DKIMlen . "; s=" . $this->DKIM_selector . ";\r\n".
 
2766                 "\tt=" . $DKIMtime . "; c=" . $DKIMcanonicalization . ";\r\n".
 
2767                 "\th=From:To:Subject;\r\n".
 
2768                 "\td=" . $this->DKIM_domain . ";" . $ident . "\r\n".
 
2772                 "\tbh=" . $DKIMb64 . ";\r\n".
 
2774     $toSign   = $this->DKIM_HeaderC($from_header . "\r\n" . $to_header . "\r\n" . $subject_header . "\r\n" . $dkimhdrs);
 
2775     $signed   = $this->DKIM_Sign($toSign);
 
2776     return $dkimhdrs.$signed."\r\n";
 
2781    * @param boolean $isSent
 
2784    * @param string $bcc
 
2785    * @param string $subject
 
2786    * @param string $body
 
2787    * @param string $from
 
2789   protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from = null) {
 
2790     if (!empty($this->action_function) && is_callable($this->action_function)) {
 
2791       $params = array($isSent, $to, $cc, $bcc, $subject, $body, $from);
 
2792       call_user_func_array($this->action_function, $params);
 
2798  * Exception handler for PHPMailer
 
2799  * @package PHPMailer
 
2801 class phpmailerException extends Exception {
 
2803    * Prettify error message output
 
2806   public function errorMessage() {
 
2807     $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";