WordPress.org

Make WordPress Core

Ticket #21074: 21074-phpmailer-5.2.2-beta2-r21841.patch

File 21074-phpmailer-5.2.2-beta2-r21841.patch, 53.6 KB (added by bpetty, 6 years ago)
  • wp-includes/class-phpmailer.php

    diff --git wp-includes/class-phpmailer.php wp-includes/class-phpmailer.php
    index df533c2..b5f0338 100644
     
    22/*~ class.phpmailer.php
    33.---------------------------------------------------------------------------.
    44|  Software: PHPMailer - PHP email class                                    |
    5 |   Version: 5.2.1                                                          |
     5|   Version: 5.2.2-beta2                                                          |
    66|      Site: https://code.google.com/a/apache-extras.org/p/phpmailer/       |
    77| ------------------------------------------------------------------------- |
    88|     Admin: Jim Jagielski (project admininistrator)                        |
     
    3131 * @author Jim Jagielski
    3232 * @copyright 2010 - 2012 Jim Jagielski
    3333 * @copyright 2004 - 2009 Andy Prevost
    34  * @version $Id: class.phpmailer.php 450 2010-06-23 16:46:33Z coolbru $
    3534 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
    3635 */
    3736
     37
    3838if (version_compare(PHP_VERSION, '5.0.0', '<') ) exit("Sorry, this version of PHPMailer will only run on PHP version 5 or greater!\n");
    3939
     40/**
     41 * Main PHPMailer class definition
     42 */
    4043class PHPMailer {
    4144
    4245  /////////////////////////////////////////////////
    class PHPMailer { 
    8790  public $FromName          = 'Root User';
    8891
    8992  /**
    90    * Sets the Sender email (Return-Path) of the message.  If not empty,
     93   * Sets the Sender email of the message.  If not empty,
    9194   * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
     95   * If not empty, and ReturnPath is empty, will also be the
     96   * Return-Path header.
    9297   * @var string
    9398   */
    9499  public $Sender            = '';
    95100
    96101  /**
     102   * Sets the Return-Path of the message.  If empty, it will
     103   * be set to either From or Sender.
     104   * @var string
     105   */
     106  public $ReturnPath        = '';
     107
     108  /**
    97109   * Sets the Subject of the message.
    98110   * @var string
    99111   */
    class PHPMailer { 
    130142  protected $MIMEHeader     = '';
    131143
    132144  /**
    133    * Stores the complete sent MIME message (Body and Headers)
     145   * Stores the extra header list which CreateHeader() doesn't fold in
    134146   * @var string
    135147   * @access protected
    136148  */
    137   protected $SentMIMEMessage     = '';
     149  protected $mailHeader     = '';
    138150
    139151  /**
    140152   * Sets word wrapping on the body of the message to a given number of
    class PHPMailer { 
    156168  public $Sendmail          = '/usr/sbin/sendmail';
    157169
    158170  /**
     171   * Determine if mail() uses a fully sendmail compatible MTA that
     172   * supports sendmail's "-oi -f" options
     173   * @var boolean
     174   */
     175  public $UseSendmailOptions    = true;
     176 
     177  /**
    159178   * Path to PHPMailer plugins.  Useful if the SMTP class
    160179   * is in a different directory than the PHP include path.
    161180   * @var string
    class PHPMailer { 
    235254  public $Password      = '';
    236255
    237256  /**
    238    * Sets the SMTP server timeout in seconds.
     257   *  Sets SMTP auth type.
     258   *  @var string
     259   */
     260  public $AuthType      = '';
     261 
     262  /**
     263   *  Sets SMTP realm.
     264   *  @var string
     265   */
     266  public $Realm         = '';
     267
     268  /**
     269   *  Sets SMTP workstation.
     270   *  @var string
     271   */
     272  public $Workstation   = '';
     273
     274  /**
     275   * Sets the SMTP server   in seconds.
    239276   * This function will not work with the win32 version.
    240277   * @var int
    241278   */
    class PHPMailer { 
    248285  public $SMTPDebug     = false;
    249286
    250287  /**
     288   * Sets the function/method to use for debugging output.
     289   * Right now we only honor "echo" or "error_log"
     290   * @var string
     291   */
     292  public $Debugoutput     = "echo";
     293
     294  /**
    251295   * Prevents the SMTP connection from being closed after each mail
    252296   * sending.  If this is set to true then to close the connection
    253297   * requires an explicit call to SmtpClose().
    class PHPMailer { 
    269313  public $SingleToArray = array();
    270314
    271315 /**
    272    * Provides the ability to change the line ending
     316   * Provides the ability to change the generic line ending
     317   * NOTE: The default remains '\n'. We force CRLF where we KNOW
     318   *        it must be used via self::CRLF
    273319   * @var string
    274320   */
    275321  public $LE              = "\n";
    276322
    277   /**
     323   /**
    278324   * Used with DKIM DNS Resource Record
    279325   * @var string
    280326   */
    class PHPMailer { 
    308354  public $DKIM_private    = '';
    309355
    310356  /**
    311    * Callback Action function name
    312    * the function that handles the result of the send email action. Parameters:
     357   * Callback Action function name.
     358   * The function that handles the result of the send email action.
     359   * It is called out by Send() for each email sent.
     360   *
     361   * Value can be:
     362   * - 'function_name' for function names
     363   * - 'Class::Method' for static method calls
     364   * - array($object, 'Method') for calling methods on $object
     365   * See http://php.net/is_callable manual page for more details.
     366   *
     367   * Parameters:
    313368   *   bool    $result        result of the send action
    314369   *   string  $to            email address of the recipient
    315370   *   string  $cc            cc email addresses
    316371   *   string  $bcc           bcc email addresses
    317372   *   string  $subject       the subject
    318373   *   string  $body          the email body
     374   *   string  $from          email address of sender
    319375   * @var string
    320376   */
    321377  public $action_function = ''; //'callbackAction';
    class PHPMailer { 
    324380   * Sets the PHPMailer Version number
    325381   * @var string
    326382   */
    327   public $Version         = '5.2.1';
     383  public $Version         = '5.2.2-beta2';
    328384
    329385  /**
    330386   * What to use in the X-Mailer header
    331    * @var string
     387   * @var string NULL for default, whitespace for None, or actual string to use
    332388   */
    333389  public $XMailer         = '';
    334390
    class PHPMailer { 
    360416  const STOP_MESSAGE  = 0; // message only, continue processing
    361417  const STOP_CONTINUE = 1; // message?, likely ok to continue processing
    362418  const STOP_CRITICAL = 2; // message, plus full stop, critical error reached
    363 
     419  const CRLF = "\r\n";     // SMTP RFC specified EOL
     420 
    364421  /////////////////////////////////////////////////
    365422  // METHODS, VARIABLES
    366423  /////////////////////////////////////////////////
    367424
    368425  /**
     426   * Calls actual mail() function, but in a safe_mode aware fashion
     427   * Also, unless sendmail_path points to sendmail (or something that
     428   * claims to be sendmail), don't pass params (not a perfect fix,
     429   * but it will do)
     430   * @param string $to To
     431   * @param string $subject Subject
     432   * @param string $body Message Body
     433   * @param string $header Additional Header(s)
     434   * @param string $params Params
     435   * @access private
     436   * @return bool
     437   */
     438  private function mail_passthru($to, $subject, $body, $header, $params) {
     439    if ( ini_get('safe_mode') || !($this->UseSendmailOptions) ) {
     440        $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($subject)), $body, $header);
     441    } else {
     442        $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($subject)), $body, $header, $params);
     443    }
     444    return $rt;
     445  }
     446
     447  /**
     448   * Outputs debugging info via user-defined method
     449   * @param string $str
     450   */
     451  private function edebug($str) {
     452    if ($this->Debugoutput == "error_log") {
     453        error_log($str);
     454    } else {
     455        echo $str;
     456    }
     457  }
     458
     459  /**
    369460   * Constructor
    370461   * @param boolean $exceptions Should we throw external exceptions?
    371462   */
    class PHPMailer { 
    389480  /**
    390481   * Sets Mailer to send message using SMTP.
    391482   * @return void
     483   * @deprecated
    392484   */
    393485  public function IsSMTP() {
    394486    $this->Mailer = 'smtp';
    class PHPMailer { 
    397489  /**
    398490   * Sets Mailer to send message using PHP mail() function.
    399491   * @return void
     492   * @deprecated
    400493   */
    401494  public function IsMail() {
    402495    $this->Mailer = 'mail';
    class PHPMailer { 
    405498  /**
    406499   * Sets Mailer to send message using the $Sendmail program.
    407500   * @return void
     501   * @deprecated
    408502   */
    409503  public function IsSendmail() {
    410504    if (!stristr(ini_get('sendmail_path'), 'sendmail')) {
    class PHPMailer { 
    416510  /**
    417511   * Sets Mailer to send message using the qmail MTA.
    418512   * @return void
     513   * @deprecated
    419514   */
    420515  public function IsQmail() {
    421516    if (stristr(ini_get('sendmail_path'), 'qmail')) {
    class PHPMailer { 
    485580      if ($this->exceptions) {
    486581        throw new phpmailerException('Invalid recipient array: ' . $kind);
    487582      }
    488           if ($this->SMTPDebug) {
    489         echo $this->Lang('Invalid recipient array').': '.$kind;
     583      if ($this->SMTPDebug) {
     584        $this->edebug($this->Lang('Invalid recipient array').': '.$kind);
    490585      }
    491586      return false;
    492587    }
    class PHPMailer { 
    497592      if ($this->exceptions) {
    498593        throw new phpmailerException($this->Lang('invalid_address').': '.$address);
    499594      }
    500           if ($this->SMTPDebug) {
    501         echo $this->Lang('invalid_address').': '.$address;
     595      if ($this->SMTPDebug) {
     596        $this->edebug($this->Lang('invalid_address').': '.$address);
    502597      }
    503598      return false;
    504599    }
    class PHPMailer { 
    521616 * Set the From and FromName properties
    522617 * @param string $address
    523618 * @param string $name
     619 * @param int $auto Also set Reply-To and Sender
    524620 * @return boolean
    525621 */
    526622  public function SetFrom($address, $name = '', $auto = 1) {
    class PHPMailer { 
    531627      if ($this->exceptions) {
    532628        throw new phpmailerException($this->Lang('invalid_address').': '.$address);
    533629      }
    534           if ($this->SMTPDebug) {
    535         echo $this->Lang('invalid_address').': '.$address;
     630      if ($this->SMTPDebug) {
     631        $this->edebug($this->Lang('invalid_address').': '.$address);
    536632      }
    537633      return false;
    538634    }
    class PHPMailer { 
    587683      if(!$this->PreSend()) return false;
    588684      return $this->PostSend();
    589685    } catch (phpmailerException $e) {
    590           $this->SentMIMEMessage = '';
     686      $this->mailHeader = '';
    591687      $this->SetError($e->getMessage());
    592688      if ($this->exceptions) {
    593689        throw $e;
    class PHPMailer { 
    596692    }
    597693  }
    598694
    599   protected function PreSend() {
     695  /**
     696   * Prep mail by constructing all message entities
     697   * @return bool
     698   */
     699  public function PreSend() {
    600700    try {
    601           $mailHeader = "";
     701      $this->mailHeader = "";
    602702      if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
    603703        throw new phpmailerException($this->Lang('provide_address'), self::STOP_CRITICAL);
    604704      }
    class PHPMailer { 
    619719      $this->MIMEBody = $this->CreateBody();
    620720
    621721      // To capture the complete message when using mail(), create
    622           // an extra header list which CreateHeader() doesn't fold in
     722      // an extra header list which CreateHeader() doesn't fold in
    623723      if ($this->Mailer == 'mail') {
    624724        if (count($this->to) > 0) {
    625           $mailHeader .= $this->AddrAppend("To", $this->to);
     725          $this->mailHeader .= $this->AddrAppend("To", $this->to);
    626726        } else {
    627           $mailHeader .= $this->HeaderLine("To", "undisclosed-recipients:;");
     727          $this->mailHeader .= $this->HeaderLine("To", "undisclosed-recipients:;");
    628728        }
    629         $mailHeader .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader(trim($this->Subject))));
     729        $this->mailHeader .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader(trim($this->Subject))));
    630730        // if(count($this->cc) > 0) {
    631             // $mailHeader .= $this->AddrAppend("Cc", $this->cc);
     731            // $this->mailHeader .= $this->AddrAppend("Cc", $this->cc);
    632732        // }
    633733      }
    634734
    class PHPMailer { 
    638738        $this->MIMEHeader = str_replace("\r\n", "\n", $header_dkim) . $this->MIMEHeader;
    639739      }
    640740
    641       $this->SentMIMEMessage = sprintf("%s%s\r\n\r\n%s",$this->MIMEHeader,$mailHeader,$this->MIMEBody);
    642741      return true;
    643742
    644743    } catch (phpmailerException $e) {
    class PHPMailer { 
    650749    }
    651750  }
    652751
    653   protected function PostSend() {
     752  /**
     753   * Actual Email transport function
     754   * Send the email via the selected mechanism
     755   * @return bool
     756   */
     757  public function PostSend() {
     758    $rtn = false;
    654759    try {
    655760      // Choose the mailer and send through it
    656761      switch($this->Mailer) {
    657762        case 'sendmail':
    658           return $this->SendmailSend($this->MIMEHeader, $this->MIMEBody);
     763          $rtn = $this->SendmailSend($this->MIMEHeader, $this->MIMEBody);
     764          break;
    659765        case 'smtp':
    660           return $this->SmtpSend($this->MIMEHeader, $this->MIMEBody);
     766          $rtn = $this->SmtpSend($this->MIMEHeader, $this->MIMEBody);
     767          break;
    661768        case 'mail':
    662           return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
     769          $rtn = $this->MailSend($this->MIMEHeader, $this->MIMEBody);
     770          break;
    663771        default:
    664           return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
     772          $rtn = $this->MailSend($this->MIMEHeader, $this->MIMEBody);
     773          break;
    665774      }
    666775
    667776    } catch (phpmailerException $e) {
    class PHPMailer { 
    669778      if ($this->exceptions) {
    670779        throw $e;
    671780      }
    672           if ($this->SMTPDebug) {
    673         echo $e->getMessage()."\n";
     781      if ($this->SMTPDebug) {
     782        $this->edebug($e->getMessage()."\n");
    674783      }
    675784      return false;
    676785    }
     786    return $rtn;
    677787  }
    678788
    679789  /**
    class PHPMailer { 
    744854    if ($this->Sender != '' and !ini_get('safe_mode')) {
    745855      $old_from = ini_get('sendmail_from');
    746856      ini_set('sendmail_from', $this->Sender);
    747       if ($this->SingleTo === true && count($toArr) > 1) {
    748         foreach ($toArr as $key => $val) {
    749           $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
    750           // implement call back function if it exists
    751           $isSent = ($rt == 1) ? 1 : 0;
    752           $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
    753         }
    754       } else {
    755         $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
     857    }
     858    if ($this->SingleTo === true && count($toArr) > 1) {
     859      foreach ($toArr as $key => $val) {
     860        $rt = $this->mail_passthru($val, $this->Subject, $body, $header, $params);
    756861        // implement call back function if it exists
    757862        $isSent = ($rt == 1) ? 1 : 0;
    758         $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body);
     863        $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
    759864      }
    760865    } else {
    761       if ($this->SingleTo === true && count($toArr) > 1) {
    762         foreach ($toArr as $key => $val) {
    763           $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
    764           // implement call back function if it exists
    765           $isSent = ($rt == 1) ? 1 : 0;
    766           $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
    767         }
    768       } else {
    769         $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
    770         // implement call back function if it exists
    771         $isSent = ($rt == 1) ? 1 : 0;
    772         $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body);
    773       }
     866      $rt = $this->mail_passthru($to, $this->Subject, $body, $header, $params);
     867      // implement call back function if it exists
     868      $isSent = ($rt == 1) ? 1 : 0;
     869      $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body);
    774870    }
    775871    if (isset($old_from)) {
    776872      ini_set('sendmail_from', $old_from);
    class PHPMailer { 
    866962      $this->smtp = new SMTP();
    867963    }
    868964
     965    $this->smtp->Timeout = $this->Timeout;
    869966    $this->smtp->do_debug = $this->SMTPDebug;
    870967    $hosts = explode(';', $this->Host);
    871968    $index = 0;
    872969    $connection = $this->smtp->Connected();
     970    $rtn = true;
    873971
    874972    // Retry while there is no connection
    875973    try {
    class PHPMailer { 
    893991
    894992          if ($tls) {
    895993            if (!$this->smtp->StartTLS()) {
     994              $rtn = false;
    896995              throw new phpmailerException($this->Lang('tls'));
    897996            }
    898997
    class PHPMailer { 
    9021001
    9031002          $connection = true;
    9041003          if ($this->SMTPAuth) {
    905             if (!$this->smtp->Authenticate($this->Username, $this->Password)) {
     1004            if (!$this->smtp->Authenticate($this->Username, $this->Password, $this->AuthType,
     1005                                           $this->Realm, $this->Workstation)) {
     1006              $rtn = false;
    9061007              throw new phpmailerException($this->Lang('authenticate'));
    9071008            }
    9081009          }
    9091010        }
    9101011        $index++;
    9111012        if (!$connection) {
     1013          $rtn = false;
    9121014          throw new phpmailerException($this->Lang('connect_host'));
    9131015        }
    9141016      }
    9151017    } catch (phpmailerException $e) {
    9161018      $this->smtp->Reset();
    917           if ($this->exceptions) {
     1019      if ($this->exceptions) {
    9181020        throw $e;
    9191021      }
     1022      $rtn = false;
    9201023    }
    921     return true;
     1024    return $rtn;
    9221025  }
    9231026
    9241027  /**
    class PHPMailer { 
    10281131    // If utf-8 encoding is used, we will need to make sure we don't
    10291132    // split multibyte characters when we wrap
    10301133    $is_utf8 = (strtolower($this->CharSet) == "utf-8");
     1134    $lelen = strlen($this->LE);
     1135    $crlflen = strlen(self::CRLF);
    10311136
    10321137    $message = $this->FixEOL($message);
    1033     if (substr($message, -1) == $this->LE) {
    1034       $message = substr($message, 0, -1);
     1138    if (substr($message, -$lelen) == $this->LE) {
     1139      $message = substr($message, 0, -$lelen);
    10351140    }
    10361141
    1037     $line = explode($this->LE, $message);
     1142    $line = explode($this->LE, $message);   // Magic. We know FixEOL uses $LE
    10381143    $message = '';
    10391144    for ($i = 0 ;$i < count($line); $i++) {
    10401145      $line_part = explode(' ', $line[$i]);
    class PHPMailer { 
    10421147      for ($e = 0; $e<count($line_part); $e++) {
    10431148        $word = $line_part[$e];
    10441149        if ($qp_mode and (strlen($word) > $length)) {
    1045           $space_left = $length - strlen($buf) - 1;
     1150          $space_left = $length - strlen($buf) - $crlflen;
    10461151          if ($e != 0) {
    10471152            if ($space_left > 20) {
    10481153              $len = $space_left;
    class PHPMailer { 
    10561161              $part = substr($word, 0, $len);
    10571162              $word = substr($word, $len);
    10581163              $buf .= ' ' . $part;
    1059               $message .= $buf . sprintf("=%s", $this->LE);
     1164              $message .= $buf . sprintf("=%s", self::CRLF);
    10601165            } else {
    10611166              $message .= $buf . $soft_break;
    10621167            }
    class PHPMailer { 
    10751180            $word = substr($word, $len);
    10761181
    10771182            if (strlen($word) > 0) {
    1078               $message .= $part . sprintf("=%s", $this->LE);
     1183              $message .= $part . sprintf("=%s", self::CRLF);
    10791184            } else {
    10801185              $buf = $part;
    10811186            }
    class PHPMailer { 
    10901195          }
    10911196        }
    10921197      }
    1093       $message .= $buf . $this->LE;
     1198      $message .= $buf . self::CRLF;
    10941199    }
    10951200
    10961201    return $message;
    class PHPMailer { 
    11761281    $this->boundary[3] = 'b3_' . $uniq_id;
    11771282
    11781283    $result .= $this->HeaderLine('Date', self::RFCDate());
    1179     if($this->Sender == '') {
     1284    if ($this->ReturnPath) {
     1285      $result .= $this->HeaderLine('Return-Path', trim($this->ReturnPath));
     1286    } elseif ($this->Sender == '') {
    11801287      $result .= $this->HeaderLine('Return-Path', trim($this->From));
    11811288    } else {
    11821289      $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
    class PHPMailer { 
    11951302          $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
    11961303        }
    11971304      }
    1198         }
     1305    }
    11991306
    12001307    $from = array();
    12011308    $from[0][0] = trim($this->From);
    class PHPMailer { 
    12271334      $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
    12281335    }
    12291336    $result .= $this->HeaderLine('X-Priority', $this->Priority);
    1230     if($this->XMailer) {
    1231       $result .= $this->HeaderLine('X-Mailer', $this->XMailer);
     1337    if ($this->XMailer == '') {
     1338        $result .= $this->HeaderLine('X-Mailer', 'PHPMailer '.$this->Version.' (http://code.google.com/a/apache-extras.org/p/phpmailer/)');
    12321339    } else {
    1233       $result .= $this->HeaderLine('X-Mailer', 'PHPMailer '.$this->Version.' (http://code.google.com/a/apache-extras.org/p/phpmailer/)');
     1340      $myXmailer = trim($this->XMailer);
     1341      if ($myXmailer) {
     1342        $result .= $this->HeaderLine('X-Mailer', $myXmailer);
     1343      }
    12341344    }
    12351345
    12361346    if($this->ConfirmReadingTo != '') {
    class PHPMailer { 
    12571367  public function GetMailMIME() {
    12581368    $result = '';
    12591369    switch($this->message_type) {
    1260       case 'plain':
    1261         $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
    1262         $result .= $this->TextLine('Content-Type: '.$this->ContentType.'; charset="'.$this->CharSet.'"');
    1263         break;
    12641370      case 'inline':
    12651371        $result .= $this->HeaderLine('Content-Type', 'multipart/related;');
    12661372        $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
    class PHPMailer { 
    12771383        $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
    12781384        $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
    12791385        break;
     1386      default:
     1387        // Catches case 'plain': and case '':
     1388        $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
     1389        $result .= $this->TextLine('Content-Type: '.$this->ContentType.'; charset='.$this->CharSet);
     1390        break;
    12801391    }
    12811392
    12821393    if($this->Mailer != 'mail') {
    class PHPMailer { 
    12921403   * @return string
    12931404   */
    12941405  public function GetSentMIMEMessage() {
    1295     return $this->SentMIMEMessage;
     1406    return sprintf("%s%s\r\n\r\n%s",$this->MIMEHeader,$this->mailHeader,$this->MIMEBody);
    12961407  }
    12971408
    12981409
    class PHPMailer { 
    13051416    $body = '';
    13061417
    13071418    if ($this->sign_key_file) {
    1308       $body .= $this->GetMailMIME();
     1419      $body .= $this->GetMailMIME().$this->LE;
    13091420    }
    13101421
    13111422    $this->SetWordWrap();
    13121423
    13131424    switch($this->message_type) {
    1314       case 'plain':
    1315         $body .= $this->EncodeString($this->Body, $this->Encoding);
    1316         break;
    13171425      case 'inline':
    13181426        $body .= $this->GetBoundary($this->boundary[1], '', '', '');
    13191427        $body .= $this->EncodeString($this->Body, $this->Encoding);
    class PHPMailer { 
    13211429        $body .= $this->AttachAll("inline", $this->boundary[1]);
    13221430        break;
    13231431      case 'attach':
    1324         $body .= $this->GetBoundary($this->boundary[1], '', '', '');
     1432        $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
    13251433        $body .= $this->EncodeString($this->Body, $this->Encoding);
    13261434        $body .= $this->LE.$this->LE;
    13271435        $body .= $this->AttachAll("attachment", $this->boundary[1]);
    class PHPMailer { 
    13981506        $body .= $this->LE;
    13991507        $body .= $this->AttachAll("attachment", $this->boundary[1]);
    14001508        break;
     1509      default:
     1510        // catch case 'plain' and case ''
     1511        $body .= $this->EncodeString($this->Body, $this->Encoding);
     1512        break;
    14011513    }
    14021514
    14031515    if ($this->IsError()) {
    class PHPMailer { 
    14441556      $encoding = $this->Encoding;
    14451557    }
    14461558    $result .= $this->TextLine('--' . $boundary);
    1447     $result .= sprintf("Content-Type: %s; charset=\"%s\"", $contentType, $charSet);
     1559    $result .= sprintf("Content-Type: %s; charset=%s", $contentType, $charSet);
    14481560    $result .= $this->LE;
    14491561    $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
    14501562    $result .= $this->LE;
    class PHPMailer { 
    15331645      if ($this->exceptions) {
    15341646        throw $e;
    15351647      }
    1536           if ($this->SMTPDebug) {
    1537         echo $e->getMessage()."\n";
     1648      if ($this->SMTPDebug) {
     1649        $this->edebug($e->getMessage()."\n");
    15381650      }
    15391651      if ( $e->getCode() == self::STOP_CRITICAL ) {
    15401652        return false;
    class PHPMailer { 
    16331745      if (!is_readable($path)) {
    16341746        throw new phpmailerException($this->Lang('file_open') . $path, self::STOP_CONTINUE);
    16351747      }
    1636       if (function_exists('get_magic_quotes')) {
    1637         function get_magic_quotes() {
    1638           return false;
    1639         }
    1640       }
    1641           $magic_quotes = get_magic_quotes_runtime();
    1642           if ($magic_quotes) {
     1748      //  if (!function_exists('get_magic_quotes')) {
     1749      //    function get_magic_quotes() {
     1750      //      return false;
     1751      //    }
     1752      //  }
     1753      $magic_quotes = get_magic_quotes_runtime();
     1754      if ($magic_quotes) {
    16431755        if (version_compare(PHP_VERSION, '5.3.0', '<')) {
    16441756          set_magic_quotes_runtime(0);
    16451757        } else {
    1646                   ini_set('magic_quotes_runtime', 0);
    1647                 }
    1648           }
     1758          ini_set('magic_quotes_runtime', 0);
     1759        }
     1760      }
    16491761      $file_buffer  = file_get_contents($path);
    16501762      $file_buffer  = $this->EncodeString($file_buffer, $encoding);
    1651           if ($magic_quotes) {
     1763      if ($magic_quotes) {
    16521764        if (version_compare(PHP_VERSION, '5.3.0', '<')) {
    16531765          set_magic_quotes_runtime($magic_quotes);
    16541766        } else {
    1655                   ini_set('magic_quotes_runtime', $magic_quotes);
    1656             }
    1657           }
     1767          ini_set('magic_quotes_runtime', $magic_quotes);
     1768        }
     1769      }
    16581770      return $file_buffer;
    16591771    } catch (Exception $e) {
    16601772      $this->SetError($e->getMessage());
    class PHPMailer { 
    17471859      $encoding = 'Q';
    17481860      $encoded = $this->EncodeQ($str, $position);
    17491861      $encoded = $this->WrapText($encoded, $maxlen, true);
    1750       $encoded = str_replace('='.$this->LE, "\n", trim($encoded));
     1862      $encoded = str_replace('='.self::CRLF, "\n", trim($encoded));
    17511863    }
    17521864
    17531865    $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
    class PHPMailer { 
    19012013   * @return string
    19022014   */
    19032015  public function EncodeQ($str, $position = 'text') {
    1904     // There should not be any EOL in the string
    1905     $encoded = preg_replace('/[\r\n]*/', '', $str);
    1906 
     2016    //There should not be any EOL in the string
     2017    $encoded = str_replace(array("\r", "\n"), '', $str);
    19072018    switch (strtolower($position)) {
    19082019      case 'phrase':
    1909         $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
     2020        $pattern = '^A-Za-z0-9!*+\/ -';
    19102021        break;
     2022
    19112023      case 'comment':
    1912         $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
     2024        $pattern = '\(\)"';
     2025        //note that we dont break here!
     2026        //for this reason we build the $pattern withoud including delimiters and []
     2027
    19132028      case 'text':
    19142029      default:
    1915         // Replace every high ascii, control =, ? and _ characters
    1916         //TODO using /e (equivalent to eval()) is probably not a good idea
    1917         $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
    1918                                 "'='.sprintf('%02X', ord(stripslashes('\\1')))", $encoded);
     2030        //Replace every high ascii, control =, ? and _ characters
     2031        //We put \075 (=) as first value to make sure it's the first one in being converted, preventing double encode
     2032        $pattern = '\075\000-\011\013\014\016-\037\077\137\177-\377' . $pattern;
    19192033        break;
    19202034    }
     2035   
     2036    if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) {
     2037      foreach (array_unique($matches[0]) as $char) {
     2038        $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded);
     2039      }
     2040    }
     2041   
     2042    //Replace every spaces to _ (more readable than =20)
     2043    return str_replace(' ', '_', $encoded);
     2044}
    19212045
    1922     // Replace every spaces to _ (more readable than =20)
    1923     $encoded = str_replace(' ', '_', $encoded);
    1924 
    1925     return $encoded;
    1926   }
    19272046
    19282047  /**
    19292048   * Adds a string or binary attachment (non-filesystem) to the list.
    class PHPMailer { 
    20272146  }
    20282147
    20292148  public function AlternativeExists() {
    2030     return strlen($this->AltBody)>0;
     2149    return !empty($this->AltBody);
    20312150  }
    20322151
    20332152  /////////////////////////////////////////////////
    class PHPMailer { 
    21842303  }
    21852304
    21862305  /**
    2187    * Changes every end of line from CR or LF to CRLF.
     2306   * Changes every end of line from CRLF, CR or LF to $this->LE.
    21882307   * @access public
     2308   * @param string $str String to FixEOL
    21892309   * @return string
    21902310   */
    21912311  public function FixEOL($str) {
    2192     $str = str_replace("\r\n", "\n", $str);
    2193     $str = str_replace("\r", "\n", $str);
    2194     $str = str_replace("\n", $this->LE, $str);
    2195     return $str;
     2312        // condense down to \n
     2313        $nstr = str_replace(array("\r\n", "\r"), "\n", $str);
     2314        // Now convert LE as needed
     2315        if ($this->LE !== "\n") {
     2316                $nstr = str_replace("\n", $this->LE, $nstr);
     2317        }
     2318    return  $nstr;
    21962319  }
    21972320
    21982321  /**
    2199    * Adds a custom header.
     2322   * Adds a custom header. $name value can be overloaded to contain
     2323   * both header name and value (name:value)
    22002324   * @access public
     2325   * @param string $name custom header name
     2326   * @param string $value header value
    22012327   * @return void
    22022328   */
    2203   public function AddCustomHeader($custom_header) {
    2204     $this->CustomHeader[] = explode(':', $custom_header, 2);
     2329  public function AddCustomHeader($name, $value=null) {
     2330        if ($value === null) {
     2331                // Value passed in as name:value
     2332                $this->CustomHeader[] = explode(':', $name, 2);
     2333        } else {
     2334                $this->CustomHeader[] = array($name, $value);
     2335        }
    22052336  }
    22062337
    22072338  /**
    22082339   * Evaluates the message and returns modifications for inline images and backgrounds
    22092340   * @access public
     2341   * @param string $message Text to be HTML modified
     2342   * @param string $basedir baseline directory for path
    22102343   * @return $message
    22112344   */
    22122345  public function MsgHTML($message, $basedir = '') {
    class PHPMailer { 
    22312364    }
    22322365    $this->IsHTML(true);
    22332366    $this->Body = $message;
    2234         if (empty($this->AltBody)) {
    2235                 $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s', '', $message)));
    2236                 if (!empty($textMsg)) {
    2237                         $this->AltBody = html_entity_decode($textMsg, ENT_QUOTES, $this->CharSet);
    2238                 }
    2239         }
     2367    if (empty($this->AltBody)) {
     2368        $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s', '', $message)));
     2369        if (!empty($textMsg)) {
     2370            $this->AltBody = html_entity_decode($textMsg, ENT_QUOTES, $this->CharSet);
     2371        }
     2372    }
    22402373    if (empty($this->AltBody)) {
    22412374      $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . "\n\n";
    22422375    }
    2243         return $message;
     2376    return $message;
    22442377  }
    22452378
    22462379  /**
    22472380   * Gets the MIME type of the embedded or inline image
    2248    * @param string File extension
     2381   * @param string $ext File extension
    22492382   * @access public
    22502383   * @return string MIME type of ext
    22512384   * @static
    22522385   */
    22532386  public static function _mime_types($ext = '') {
    22542387    $mimes = array(
     2388      'xl'    =>  'application/excel',
    22552389      'hqx'   =>  'application/mac-binhex40',
    22562390      'cpt'   =>  'application/mac-compactpro',
    2257       'doc'   =>  'application/msword',
    22582391      'bin'   =>  'application/macbinary',
     2392      'doc'   =>  'application/msword',
     2393      'doc'   =>  'application/msword',
     2394      'word'  =>  'application/msword',
     2395      'class' =>  'application/octet-stream',
     2396      'dll'   =>  'application/octet-stream',
    22592397      'dms'   =>  'application/octet-stream',
     2398      'exe'   =>  'application/octet-stream',
    22602399      'lha'   =>  'application/octet-stream',
    22612400      'lzh'   =>  'application/octet-stream',
    2262       'exe'   =>  'application/octet-stream',
    2263       'class' =>  'application/octet-stream',
    22642401      'psd'   =>  'application/octet-stream',
    2265       'so'    =>  'application/octet-stream',
    22662402      'sea'   =>  'application/octet-stream',
    2267       'dll'   =>  'application/octet-stream',
     2403      'so'    =>  'application/octet-stream',
    22682404      'oda'   =>  'application/oda',
    22692405      'pdf'   =>  'application/pdf',
    22702406      'ai'    =>  'application/postscript',
    class PHPMailer { 
    22822418      'dxr'   =>  'application/x-director',
    22832419      'dvi'   =>  'application/x-dvi',
    22842420      'gtar'  =>  'application/x-gtar',
    2285       'php'   =>  'application/x-httpd-php',
    2286       'php4'  =>  'application/x-httpd-php',
    22872421      'php3'  =>  'application/x-httpd-php',
     2422      'php4'  =>  'application/x-httpd-php',
     2423      'php'   =>  'application/x-httpd-php',
    22882424      'phtml' =>  'application/x-httpd-php',
    22892425      'phps'  =>  'application/x-httpd-php-source',
    22902426      'js'    =>  'application/x-javascript',
    class PHPMailer { 
    22922428      'sit'   =>  'application/x-stuffit',
    22932429      'tar'   =>  'application/x-tar',
    22942430      'tgz'   =>  'application/x-tar',
    2295       'xhtml' =>  'application/xhtml+xml',
    22962431      'xht'   =>  'application/xhtml+xml',
     2432      'xhtml' =>  'application/xhtml+xml',
    22972433      'zip'   =>  'application/zip',
    22982434      'mid'   =>  'audio/midi',
    22992435      'midi'  =>  'audio/midi',
    2300       'mpga'  =>  'audio/mpeg',
    23012436      'mp2'   =>  'audio/mpeg',
    23022437      'mp3'   =>  'audio/mpeg',
     2438      'mpga'  =>  'audio/mpeg',
    23032439      'aif'   =>  'audio/x-aiff',
    2304       'aiff'  =>  'audio/x-aiff',
    23052440      'aifc'  =>  'audio/x-aiff',
     2441      'aiff'  =>  'audio/x-aiff',
    23062442      'ram'   =>  'audio/x-pn-realaudio',
    23072443      'rm'    =>  'audio/x-pn-realaudio',
    23082444      'rpm'   =>  'audio/x-pn-realaudio-plugin',
    23092445      'ra'    =>  'audio/x-realaudio',
    2310       'rv'    =>  'video/vnd.rn-realvideo',
    23112446      'wav'   =>  'audio/x-wav',
    23122447      'bmp'   =>  'image/bmp',
    23132448      'gif'   =>  'image/gif',
    23142449      'jpeg'  =>  'image/jpeg',
    2315       'jpg'   =>  'image/jpeg',
    23162450      'jpe'   =>  'image/jpeg',
     2451      'jpg'   =>  'image/jpeg',
    23172452      'png'   =>  'image/png',
    23182453      'tiff'  =>  'image/tiff',
    23192454      'tif'   =>  'image/tiff',
     2455      'eml'   =>  'message/rfc822',
    23202456      'css'   =>  'text/css',
    23212457      'html'  =>  'text/html',
    23222458      'htm'   =>  'text/html',
    23232459      'shtml' =>  'text/html',
    2324       'txt'   =>  'text/plain',
    2325       'text'  =>  'text/plain',
    23262460      'log'   =>  'text/plain',
     2461      'text'  =>  'text/plain',
     2462      'txt'   =>  'text/plain',
    23272463      'rtx'   =>  'text/richtext',
    23282464      'rtf'   =>  'text/rtf',
    23292465      'xml'   =>  'text/xml',
    23302466      'xsl'   =>  'text/xml',
    23312467      'mpeg'  =>  'video/mpeg',
    2332       'mpg'   =>  'video/mpeg',
    23332468      'mpe'   =>  'video/mpeg',
    2334       'qt'    =>  'video/quicktime',
     2469      'mpg'   =>  'video/mpeg',
    23352470      'mov'   =>  'video/quicktime',
     2471      'qt'    =>  'video/quicktime',
     2472      'rv'    =>  'video/vnd.rn-realvideo',
    23362473      'avi'   =>  'video/x-msvideo',
    2337       'movie' =>  'video/x-sgi-movie',
    2338       'doc'   =>  'application/msword',
    2339       'word'  =>  'application/msword',
    2340       'xl'    =>  'application/excel',
    2341       'eml'   =>  'message/rfc822'
     2474      'movie' =>  'video/x-sgi-movie'
    23422475    );
    23432476    return (!isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)];
    23442477  }
    class PHPMailer { 
    23782511   * @return string
    23792512   */
    23802513  public function SecureHeader($str) {
    2381     $str = str_replace("\r", '', $str);
    2382     $str = str_replace("\n", '', $str);
    2383     return trim($str);
     2514    return trim(str_replace(array("\r", "\n"), '', $str));
    23842515  }
    23852516
    23862517  /**
    class PHPMailer { 
    25152646    return "X-PHPMAILER-DKIM: phpmailer.worxware.com\r\n".$dkimhdrs.$signed."\r\n";
    25162647  }
    25172648
    2518   protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body) {
    2519     if (!empty($this->action_function) && function_exists($this->action_function)) {
    2520       $params = array($isSent, $to, $cc, $bcc, $subject, $body);
     2649  /**
     2650   * Perform callback
     2651   */
     2652  protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from=null) {
     2653    if (!empty($this->action_function) && is_callable($this->action_function)) {
     2654      $params = array($isSent, $to, $cc, $bcc, $subject, $body, $from);
    25212655      call_user_func_array($this->action_function, $params);
    25222656    }
    25232657  }
    25242658}
    25252659
     2660/**
     2661 * Exception handling
     2662 */
    25262663class phpmailerException extends Exception {
    25272664  public function errorMessage() {
    25282665    $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";
  • wp-includes/class-smtp.php

    diff --git wp-includes/class-smtp.php wp-includes/class-smtp.php
    index 6977bff..78dbe0b 100644
     
    22/*~ class.smtp.php
    33.---------------------------------------------------------------------------.
    44|  Software: PHPMailer - PHP email class                                    |
    5 |   Version: 5.2.1                                                          |
     5|   Version: 5.2.2-beta2                                                          |
    66|      Site: https://code.google.com/a/apache-extras.org/p/phpmailer/       |
    77| ------------------------------------------------------------------------- |
    88|     Admin: Jim Jagielski (project admininistrator)                        |
     
    3232 * @author Jim Jagielski
    3333 * @copyright 2010 - 2012 Jim Jagielski
    3434 * @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
    35  * @version $Id: class.smtp.php 450 2010-06-23 16:46:33Z coolbru $
    3635 */
    3736
    3837/**
    class SMTP { 
    6362  public $do_debug;       // the level of debug to perform
    6463
    6564  /**
     65   * Sets the function/method to use for debugging output.
     66   * Right now we only honor "echo" or "error_log"
     67   * @var string
     68   */
     69  public $Debugoutput     = "echo";
     70
     71  /**
    6672   *  Sets VERP use on/off (default is off)
    6773   *  @var bool
    6874   */
    6975  public $do_verp = false;
    7076
    7177  /**
     78   * Sets the SMTP timeout value for reads, in seconds
     79   * @var int
     80   */
     81  public $Timeout         = 15;
     82
     83  /**
     84   * Sets the SMTP timelimit value for reads, in seconds
     85   * @var int
     86   */
     87  public $Timelimit       = 30;
     88
     89  /**
    7290   * Sets the SMTP PHPMailer Version number
    7391   * @var string
    7492   */
    75   public $Version         = '5.2.1';
     93  public $Version         = '5.2.2-beta2';
    7694
    7795  /////////////////////////////////////////////////
    7896  // PROPERTIES, PRIVATE AND PROTECTED
    class SMTP { 
    83101  private $helo_rply; // the reply the server sent to us for HELO
    84102
    85103  /**
     104   * Outputs debugging info via user-defined method
     105   * @param string $str
     106   */
     107  private function edebug($str) {
     108    if ($this->Debugoutput == "error_log") {
     109        error_log($str);
     110    } else {
     111        echo $str;
     112    }
     113  }
     114
     115  /**
    86116   * Initialize the class so that the data is in a known state.
    87117   * @access public
    88118   * @return void
    class SMTP { 
    139169                           "errno" => $errno,
    140170                           "errstr" => $errstr);
    141171      if($this->do_debug >= 1) {
    142         echo "SMTP -> ERROR: " . $this->error["error"] . ": $errstr ($errno)" . $this->CRLF . '<br />';
     172        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": $errstr ($errno)" . $this->CRLF . '<br />');
    143173      }
    144174      return false;
    145175    }
    146176
    147177    // SMTP server can take longer to respond, give longer timeout for first read
    148178    // Windows does not have support for this timeout function
    149     if(substr(PHP_OS, 0, 3) != "WIN")
    150      socket_set_timeout($this->smtp_conn, $tval, 0);
     179    if(substr(PHP_OS, 0, 3) != "WIN") {
     180     $max = ini_get('max_execution_time');
     181     if ($max != 0 && $tval > $max) { // don't bother if unlimited
     182      @set_time_limit($tval);
     183     }
     184     stream_set_timeout($this->smtp_conn, $tval, 0);
     185    }
    151186
    152187    // get any announcement
    153188    $announce = $this->get_lines();
    154189
    155190    if($this->do_debug >= 2) {
    156       echo "SMTP -> FROM SERVER:" . $announce . $this->CRLF . '<br />';
     191      $this->edebug("SMTP -> FROM SERVER:" . $announce . $this->CRLF . '<br />');
    157192    }
    158193
    159194    return true;
    class SMTP { 
    182217    $code = substr($rply,0,3);
    183218
    184219    if($this->do_debug >= 2) {
    185       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     220      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    186221    }
    187222
    188223    if($code != 220) {
    class SMTP { 
    191226               "smtp_code" => $code,
    192227               "smtp_msg"  => substr($rply,4));
    193228      if($this->do_debug >= 1) {
    194         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     229        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    195230      }
    196231      return false;
    197232    }
    class SMTP { 
    210245   * @access public
    211246   * @return bool
    212247   */
    213   public function Authenticate($username, $password) {
    214     // Start authentication
    215     fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
    216 
    217     $rply = $this->get_lines();
    218     $code = substr($rply,0,3);
    219 
    220     if($code != 334) {
    221       $this->error =
    222         array("error" => "AUTH not accepted from server",
    223               "smtp_code" => $code,
    224               "smtp_msg" => substr($rply,4));
    225       if($this->do_debug >= 1) {
    226         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
    227       }
    228       return false;
    229     }
    230 
    231     // Send encoded username
    232     fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
    233 
    234     $rply = $this->get_lines();
    235     $code = substr($rply,0,3);
    236 
    237     if($code != 334) {
    238       $this->error =
    239         array("error" => "Username not accepted from server",
    240               "smtp_code" => $code,
    241               "smtp_msg" => substr($rply,4));
    242       if($this->do_debug >= 1) {
    243         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
    244       }
    245       return false;
    246     }
    247 
    248     // Send encoded password
    249     fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
    250 
    251     $rply = $this->get_lines();
    252     $code = substr($rply,0,3);
    253 
    254     if($code != 235) {
    255       $this->error =
    256         array("error" => "Password not accepted from server",
    257               "smtp_code" => $code,
    258               "smtp_msg" => substr($rply,4));
    259       if($this->do_debug >= 1) {
    260         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
    261       }
    262       return false;
     248  public function Authenticate($username, $password, $authtype='LOGIN', $realm='',
     249                               $workstation='') {
     250    if (empty($authtype)) {
     251      $authtype = 'LOGIN';
     252    }
     253
     254    switch ($authtype) {
     255      case 'LOGIN':
     256        // Start authentication
     257        fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
     258   
     259        $rply = $this->get_lines();
     260        $code = substr($rply,0,3);
     261   
     262        if($code != 334) {
     263          $this->error =
     264            array("error" => "AUTH not accepted from server",
     265                  "smtp_code" => $code,
     266                  "smtp_msg" => substr($rply,4));
     267          if($this->do_debug >= 1) {
     268            $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
     269          }
     270          return false;
     271        }
     272   
     273        // Send encoded username
     274        fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
     275   
     276        $rply = $this->get_lines();
     277        $code = substr($rply,0,3);
     278   
     279        if($code != 334) {
     280          $this->error =
     281            array("error" => "Username not accepted from server",
     282                  "smtp_code" => $code,
     283                  "smtp_msg" => substr($rply,4));
     284          if($this->do_debug >= 1) {
     285            $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
     286          }
     287          return false;
     288        }
     289   
     290        // Send encoded password
     291        fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
     292   
     293        $rply = $this->get_lines();
     294        $code = substr($rply,0,3);
     295   
     296        if($code != 235) {
     297          $this->error =
     298            array("error" => "Password not accepted from server",
     299                  "smtp_code" => $code,
     300                  "smtp_msg" => substr($rply,4));
     301          if($this->do_debug >= 1) {
     302            $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
     303          }
     304          return false;
     305        }
     306        break;
     307      case 'NTLM':
     308        /*
     309         * ntlm_sasl_client.php
     310         ** Bundled with Permission
     311         **
     312         ** How to telnet in windows: http://technet.microsoft.com/en-us/library/aa995718%28EXCHG.65%29.aspx
     313         ** PROTOCOL Documentation http://curl.haxx.se/rfc/ntlm.html#ntlmSmtpAuthentication
     314         */
     315        require_once('ntlm_sasl_client.php');
     316        $temp = new stdClass();
     317        $ntlm_client = new ntlm_sasl_client_class;
     318        if(! $ntlm_client->Initialize($temp)){//let's test if every function its available
     319            $this->error = array("error" => $temp->error);
     320            if($this->do_debug >= 1) {
     321                $this->edebug("You need to enable some modules in your php.ini file: " . $this->error["error"] . $this->CRLF);
     322            }
     323            return false;
     324        }
     325        $msg1 = $ntlm_client->TypeMsg1($realm, $workstation);//msg1
     326       
     327        fputs($this->smtp_conn,"AUTH NTLM " . base64_encode($msg1) . $this->CRLF);
     328
     329        $rply = $this->get_lines();
     330        $code = substr($rply,0,3);
     331       
     332
     333        if($code != 334) {
     334            $this->error =
     335                array("error" => "AUTH not accepted from server",
     336                      "smtp_code" => $code,
     337                      "smtp_msg" => substr($rply,4));
     338            if($this->do_debug >= 1) {
     339                $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF);
     340            }
     341            return false;
     342        }
     343       
     344        $challange = substr($rply,3);//though 0 based, there is a white space after the 3 digit number....//msg2
     345        $challange = base64_decode($challange);
     346        $ntlm_res = $ntlm_client->NTLMResponse(substr($challange,24,8),$password);
     347        $msg3 = $ntlm_client->TypeMsg3($ntlm_res,$username,$realm,$workstation);//msg3
     348        // Send encoded username
     349        fputs($this->smtp_conn, base64_encode($msg3) . $this->CRLF);
     350
     351        $rply = $this->get_lines();
     352        $code = substr($rply,0,3);
     353
     354        if($code != 235) {
     355            $this->error =
     356                array("error" => "Could not authenticate",
     357                      "smtp_code" => $code,
     358                      "smtp_msg" => substr($rply,4));
     359            if($this->do_debug >= 1) {
     360                $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF);
     361            }
     362            return false;
     363        }
     364        break;
    263365    }
    264 
    265366    return true;
    266367  }
    267368
    class SMTP { 
    276377      if($sock_status["eof"]) {
    277378        // the socket is valid but we are not connected
    278379        if($this->do_debug >= 1) {
    279             echo "SMTP -> NOTICE:" . $this->CRLF . "EOF caught while checking if connected";
     380            $this->edebug("SMTP -> NOTICE:" . $this->CRLF . "EOF caught while checking if connected");
    280381        }
    281382        $this->Close();
    282383        return false;
    class SMTP { 
    341442    $code = substr($rply,0,3);
    342443
    343444    if($this->do_debug >= 2) {
    344       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     445      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    345446    }
    346447
    347448    if($code != 354) {
    class SMTP { 
    350451              "smtp_code" => $code,
    351452              "smtp_msg" => substr($rply,4));
    352453      if($this->do_debug >= 1) {
    353         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     454        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    354455      }
    355456      return false;
    356457    }
    class SMTP { 
    435536    $code = substr($rply,0,3);
    436537
    437538    if($this->do_debug >= 2) {
    438       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     539      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    439540    }
    440541
    441542    if($code != 250) {
    class SMTP { 
    444545              "smtp_code" => $code,
    445546              "smtp_msg" => substr($rply,4));
    446547      if($this->do_debug >= 1) {
    447         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     548        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    448549      }
    449550      return false;
    450551    }
    class SMTP { 
    500601    $code = substr($rply,0,3);
    501602
    502603    if($this->do_debug >= 2) {
    503       echo "SMTP -> FROM SERVER: " . $rply . $this->CRLF . '<br />';
     604      $this->edebug("SMTP -> FROM SERVER: " . $rply . $this->CRLF . '<br />');
    504605    }
    505606
    506607    if($code != 250) {
    class SMTP { 
    509610              "smtp_code" => $code,
    510611              "smtp_msg" => substr($rply,4));
    511612      if($this->do_debug >= 1) {
    512         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     613        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    513614      }
    514615      return false;
    515616    }
    class SMTP { 
    542643      return false;
    543644    }
    544645
    545     $useVerp = ($this->do_verp ? "XVERP" : "");
     646    $useVerp = ($this->do_verp ? " XVERP" : "");
    546647    fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $useVerp . $this->CRLF);
    547648
    548649    $rply = $this->get_lines();
    549650    $code = substr($rply,0,3);
    550651
    551652    if($this->do_debug >= 2) {
    552       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     653      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    553654    }
    554655
    555656    if($code != 250) {
    class SMTP { 
    558659              "smtp_code" => $code,
    559660              "smtp_msg" => substr($rply,4));
    560661      if($this->do_debug >= 1) {
    561         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     662        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    562663      }
    563664      return false;
    564665    }
    class SMTP { 
    592693    $byemsg = $this->get_lines();
    593694
    594695    if($this->do_debug >= 2) {
    595       echo "SMTP -> FROM SERVER:" . $byemsg . $this->CRLF . '<br />';
     696      $this->edebug("SMTP -> FROM SERVER:" . $byemsg . $this->CRLF . '<br />');
    596697    }
    597698
    598699    $rval = true;
    class SMTP { 
    606707                 "smtp_rply" => substr($byemsg,4));
    607708      $rval = false;
    608709      if($this->do_debug >= 1) {
    609         echo "SMTP -> ERROR: " . $e["error"] . ": " . $byemsg . $this->CRLF . '<br />';
     710        $this->edebug("SMTP -> ERROR: " . $e["error"] . ": " . $byemsg . $this->CRLF . '<br />');
    610711      }
    611712    }
    612713
    class SMTP { 
    644745    $code = substr($rply,0,3);
    645746
    646747    if($this->do_debug >= 2) {
    647       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     748      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    648749    }
    649750
    650751    if($code != 250 && $code != 251) {
    class SMTP { 
    653754              "smtp_code" => $code,
    654755              "smtp_msg" => substr($rply,4));
    655756      if($this->do_debug >= 1) {
    656         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     757        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    657758      }
    658759      return false;
    659760    }
    class SMTP { 
    687788    $code = substr($rply,0,3);
    688789
    689790    if($this->do_debug >= 2) {
    690       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     791      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    691792    }
    692793
    693794    if($code != 250) {
    class SMTP { 
    696797              "smtp_code" => $code,
    697798              "smtp_msg" => substr($rply,4));
    698799      if($this->do_debug >= 1) {
    699         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     800        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    700801      }
    701802      return false;
    702803    }
    class SMTP { 
    735836    $code = substr($rply,0,3);
    736837
    737838    if($this->do_debug >= 2) {
    738       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     839      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    739840    }
    740841
    741842    if($code != 250) {
    class SMTP { 
    744845              "smtp_code" => $code,
    745846              "smtp_msg" => substr($rply,4));
    746847      if($this->do_debug >= 1) {
    747         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     848        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    748849      }
    749850      return false;
    750851    }
    class SMTP { 
    768869    $this->error = array("error" => "This method, TURN, of the SMTP ".
    769870                                    "is not implemented");
    770871    if($this->do_debug >= 1) {
    771       echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF . '<br />';
     872      $this->edebug("SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF . '<br />');
    772873    }
    773874    return false;
    774875  }
    class SMTP { 
    797898   */
    798899  private function get_lines() {
    799900    $data = "";
    800     while(!feof($this->smtp_conn)) {
     901    $endtime = 0;
     902    /* If for some reason the fp is bad, don't inf loop */
     903    if (!is_resource($this->smtp_conn)) {
     904      return $data;
     905    }
     906    stream_set_timeout($this->smtp_conn, $this->Timeout);
     907    if ($this->Timelimit > 0) {
     908      $endtime = time() + $this->Timelimit;
     909    }
     910    while(is_resource($this->smtp_conn) && !feof($this->smtp_conn)) {
    801911      $str = @fgets($this->smtp_conn,515);
    802912      if($this->do_debug >= 4) {
    803         echo "SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . '<br />';
    804         echo "SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF . '<br />';
     913        $this->edebug("SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . '<br />');
     914        $this->edebug("SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF . '<br />');
    805915      }
    806916      $data .= $str;
    807917      if($this->do_debug >= 4) {
    808         echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . '<br />';
     918        $this->edebug("SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . '<br />');
    809919      }
    810920      // if 4th character is a space, we are done reading, break the loop
    811921      if(substr($str,3,1) == " ") { break; }
     922      // Timed-out? Log and break
     923      $info = stream_get_meta_data($this->smtp_conn);
     924      if ($info['timed_out']) {
     925        if($this->do_debug >= 4) {
     926          $this->edebug("SMTP -> get_lines(): timed-out (" . $this->Timeout . " seconds) <br />");
     927        }
     928        break;
     929      }
     930      // Now check if reads took too long
     931      if ($endtime) {
     932        if (time() > $endtime) {
     933          if($this->do_debug >= 4) {
     934            $this->edebug("SMTP -> get_lines(): timelimit reached (" . $this->Timelimit . " seconds) <br />");
     935          }
     936          break;
     937        }
     938      }
    812939    }
    813940    return $data;
    814941  }