Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-phpmailer.php

    r18107 r20715  
    33.---------------------------------------------------------------------------.
    44|  Software: PHPMailer - PHP email class                                    |
    5 |   Version: 5.1                                                            |
    6 |   Contact: via sourceforge.net support pages (also www.worxware.com)      |
    7 |      Info: http://phpmailer.sourceforge.net                               |
    8 |   Support: http://sourceforge.net/projects/phpmailer/                     |
     5|   Version: 5.2.1                                                          |
     6|      Site: https://code.google.com/a/apache-extras.org/p/phpmailer/       |
    97| ------------------------------------------------------------------------- |
    10 |     Admin: Andy Prevost (project admininistrator)                         |
     8|     Admin: Jim Jagielski (project admininistrator)                        |
    119|   Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
    1210|          : Marcus Bointon (coolbru) coolbru@users.sourceforge.net         |
     11|          : Jim Jagielski (jimjag) jimjag@gmail.com                        |
    1312|   Founder: Brent R. Matzelle (original founder)                           |
     13| Copyright (c) 2010-2012, Jim Jagielski. All Rights Reserved.              |
    1414| Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved.               |
    1515| Copyright (c) 2001-2003, Brent R. Matzelle                                |
    … …  
    2020| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     |
    2121| FITNESS FOR A PARTICULAR PURPOSE.                                         |
    22 | ------------------------------------------------------------------------- |
    23 | We offer a number of paid services (www.worxware.com):                    |
    24 | - Web Hosting on highly optimized fast and secure servers                 |
    25 | - Technology Consulting                                                   |
    26 | - Oursourcing (highly qualified programmers and graphic designers)        |
    2722'---------------------------------------------------------------------------'
    2823*/
    … …  
    3429 * @author Andy Prevost
    3530 * @author Marcus Bointon
     31 * @author Jim Jagielski
     32 * @copyright 2010 - 2012 Jim Jagielski
    3633 * @copyright 2004 - 2009 Andy Prevost
    37  * @version $Id: class.phpmailer.php 447 2009-05-25 01:36:38Z codeworxtech $
     34 * @version $Id: class.phpmailer.php 450 2010-06-23 16:46:33Z coolbru $
    3835 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
    3936 */
    … …  
    118115   */
    119116  public $AltBody           = '';
     117
     118  /**
     119   * Stores the complete compiled MIME message body.
     120   * @var string
     121   * @access protected
     122   */
     123  protected $MIMEBody       = '';
     124
     125  /**
     126   * Stores the complete compiled MIME message headers.
     127   * @var string
     128   * @access protected
     129   */
     130  protected $MIMEHeader     = '';
     131
     132  /**
     133   * Stores the complete sent MIME message (Body and Headers)
     134   * @var string
     135   * @access protected
     136  */
     137  protected $SentMIMEMessage     = '';
    120138
    121139  /**
    … …  
    269287   */
    270288  public $DKIM_identity   = '';
     289
     290  /**
     291   * Used with DKIM DNS Resource Record
     292   * @var string
     293   */
     294  public $DKIM_passphrase   = '';
    271295
    272296  /**
    … …  
    301325   * @var string
    302326   */
    303   public $Version         = '5.1';
     327  public $Version         = '5.2.1';
     328
     329  /**
     330   * What to use in the X-Mailer header
     331   * @var string
     332   */
     333  public $XMailer         = '';
    304334
    305335  /////////////////////////////////////////////////
    … …  
    307337  /////////////////////////////////////////////////
    308338
    309   private   $smtp           = NULL;
    310   private   $to             = array();
    311   private   $cc             = array();
    312   private   $bcc            = array();
    313   private   $ReplyTo        = array();
    314   private   $all_recipients = array();
    315   private   $attachment     = array();
    316   private   $CustomHeader   = array();
    317   private   $message_type   = '';
    318   private   $boundary       = array();
    319   protected $language       = array();
    320   private   $error_count    = 0;
    321   private   $sign_cert_file = "";
    322   private   $sign_key_file  = "";
    323   private   $sign_key_pass  = "";
    324   private   $exceptions     = false;
     339  protected   $smtp           = NULL;
     340  protected   $to             = array();
     341  protected   $cc             = array();
     342  protected   $bcc            = array();
     343  protected   $ReplyTo        = array();
     344  protected   $all_recipients = array();
     345  protected   $attachment     = array();
     346  protected   $CustomHeader   = array();
     347  protected   $message_type   = '';
     348  protected   $boundary       = array();
     349  protected   $language       = array();
     350  protected   $error_count    = 0;
     351  protected   $sign_cert_file = '';
     352  protected   $sign_key_file  = '';
     353  protected   $sign_key_pass  = '';
     354  protected   $exceptions     = false;
    325355
    326356  /////////////////////////////////////////////////
    … …  
    438468   */
    439469  public function AddReplyTo($address, $name = '') {
    440     return $this->AddAnAddress('ReplyTo', $address, $name);
     470    return $this->AddAnAddress('Reply-To', $address, $name);
    441471  }
    442472
    … …  
    448478   * @param string $name
    449479   * @return boolean true on success, false if address already used or invalid in some way
    450    * @access private
    451    */
    452   private function AddAnAddress($kind, $address, $name = '') {
    453     if (!preg_match('/^(to|cc|bcc|ReplyTo)$/', $kind)) {
    454       echo 'Invalid recipient array: ' . $kind;
     480   * @access protected
     481   */
     482  protected function AddAnAddress($kind, $address, $name = '') {
     483    if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) {
     484      $this->SetError($this->Lang('Invalid recipient array').': '.$kind);
     485      if ($this->exceptions) {
     486        throw new phpmailerException('Invalid recipient array: ' . $kind);
     487      }
     488          if ($this->SMTPDebug) {
     489        echo $this->Lang('Invalid recipient array').': '.$kind;
     490      }
    455491      return false;
    456492    }
    … …  
    462498        throw new phpmailerException($this->Lang('invalid_address').': '.$address);
    463499      }
    464       echo $this->Lang('invalid_address').': '.$address;
     500          if ($this->SMTPDebug) {
     501        echo $this->Lang('invalid_address').': '.$address;
     502      }
    465503      return false;
    466504    }
    467     if ($kind != 'ReplyTo') {
     505    if ($kind != 'Reply-To') {
    468506      if (!isset($this->all_recipients[strtolower($address)])) {
    469507        array_push($this->$kind, array($address, $name));
    … …  
    486524 * @return boolean
    487525 */
    488   public function SetFrom($address, $name = '',$auto=1) {
     526  public function SetFrom($address, $name = '', $auto = 1) {
    489527    $address = trim($address);
    490528    $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
    … …  
    494532        throw new phpmailerException($this->Lang('invalid_address').': '.$address);
    495533      }
    496       echo $this->Lang('invalid_address').': '.$address;
     534          if ($this->SMTPDebug) {
     535        echo $this->Lang('invalid_address').': '.$address;
     536      }
    497537      return false;
    498538    }
    … …  
    501541    if ($auto) {
    502542      if (empty($this->ReplyTo)) {
    503         $this->AddAnAddress('ReplyTo', $address, $name);
     543        $this->AddAnAddress('Reply-To', $address, $name);
    504544      }
    505545      if (empty($this->Sender)) {
    … …  
    545585  public function Send() {
    546586    try {
     587      if(!$this->PreSend()) return false;
     588      return $this->PostSend();
     589    } catch (phpmailerException $e) {
     590          $this->SentMIMEMessage = '';
     591      $this->SetError($e->getMessage());
     592      if ($this->exceptions) {
     593        throw $e;
     594      }
     595      return false;
     596    }
     597  }
     598
     599  protected function PreSend() {
     600    try {
     601          $mailHeader = "";
    547602      if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
    548603        throw new phpmailerException($this->Lang('provide_address'), self::STOP_CRITICAL);
    … …  
    556611      $this->error_count = 0; // reset errors
    557612      $this->SetMessageType();
    558       $header = $this->CreateHeader();
    559       $body = $this->CreateBody();
    560 
     613      //Refuse to send an empty message
    561614      if (empty($this->Body)) {
    562615        throw new phpmailerException($this->Lang('empty_message'), self::STOP_CRITICAL);
    563616      }
    564617
     618      $this->MIMEHeader = $this->CreateHeader();
     619      $this->MIMEBody = $this->CreateBody();
     620
     621      // To capture the complete message when using mail(), create
     622          // an extra header list which CreateHeader() doesn't fold in
     623      if ($this->Mailer == 'mail') {
     624        if (count($this->to) > 0) {
     625          $mailHeader .= $this->AddrAppend("To", $this->to);
     626        } else {
     627          $mailHeader .= $this->HeaderLine("To", "undisclosed-recipients:;");
     628        }
     629        $mailHeader .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader(trim($this->Subject))));
     630        // if(count($this->cc) > 0) {
     631            // $mailHeader .= $this->AddrAppend("Cc", $this->cc);
     632        // }
     633      }
     634
    565635      // digitally sign with DKIM if enabled
    566636      if ($this->DKIM_domain && $this->DKIM_private) {
    567         $header_dkim = $this->DKIM_Add($header,$this->Subject,$body);
    568         $header = str_replace("\r\n","\n",$header_dkim) . $header;
    569       }
    570 
    571       // Choose the mailer and send through it
    572       switch($this->Mailer) {
    573         case 'sendmail':
    574           return $this->SendmailSend($header, $body);
    575         case 'smtp':
    576           return $this->SmtpSend($header, $body);
    577         default:
    578           return $this->MailSend($header, $body);
    579       }
     637        $header_dkim = $this->DKIM_Add($this->MIMEHeader, $this->EncodeHeader($this->SecureHeader($this->Subject)), $this->MIMEBody);
     638        $this->MIMEHeader = str_replace("\r\n", "\n", $header_dkim) . $this->MIMEHeader;
     639      }
     640
     641      $this->SentMIMEMessage = sprintf("%s%s\r\n\r\n%s",$this->MIMEHeader,$mailHeader,$this->MIMEBody);
     642      return true;
    580643
    581644    } catch (phpmailerException $e) {
    … …  
    584647        throw $e;
    585648      }
    586       echo $e->getMessage()."\n";
     649      return false;
     650    }
     651  }
     652
     653  protected function PostSend() {
     654    try {
     655      // Choose the mailer and send through it
     656      switch($this->Mailer) {
     657        case 'sendmail':
     658          return $this->SendmailSend($this->MIMEHeader, $this->MIMEBody);
     659        case 'smtp':
     660          return $this->SmtpSend($this->MIMEHeader, $this->MIMEBody);
     661        case 'mail':
     662          return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
     663        default:
     664          return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
     665      }
     666
     667    } catch (phpmailerException $e) {
     668      $this->SetError($e->getMessage());
     669      if ($this->exceptions) {
     670        throw $e;
     671      }
     672          if ($this->SMTPDebug) {
     673        echo $e->getMessage()."\n";
     674      }
    587675      return false;
    588676    }
    … …  
    613701        // implement call back function if it exists
    614702        $isSent = ($result == 0) ? 1 : 0;
    615         $this->doCallback($isSent,$val,$this->cc,$this->bcc,$this->Subject,$body);
     703        $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
    616704        if($result != 0) {
    617705          throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
    … …  
    627715      // implement call back function if it exists
    628716      $isSent = ($result == 0) ? 1 : 0;
    629       $this->doCallback($isSent,$this->to,$this->cc,$this->bcc,$this->Subject,$body);
     717      $this->doCallback($isSent, $this->to, $this->cc, $this->bcc, $this->Subject, $body);
    630718      if($result != 0) {
    631719        throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
    … …  
    649737    $to = implode(', ', $toArr);
    650738
    651     $params = sprintf("-oi -f %s", $this->Sender);
    652     if ($this->Sender != '' && strlen(ini_get('safe_mode'))< 1) {
     739    if (empty($this->Sender)) {
     740      $params = "-oi ";
     741    } else {
     742      $params = sprintf("-oi -f %s", $this->Sender);
     743    }
     744    if ($this->Sender != '' and !ini_get('safe_mode')) {
    653745      $old_from = ini_get('sendmail_from');
    654746      ini_set('sendmail_from', $this->Sender);
    … …  
    658750          // implement call back function if it exists
    659751          $isSent = ($rt == 1) ? 1 : 0;
    660           $this->doCallback($isSent,$val,$this->cc,$this->bcc,$this->Subject,$body);
     752          $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
    661753        }
    662754      } else {
    … …  
    664756        // implement call back function if it exists
    665757        $isSent = ($rt == 1) ? 1 : 0;
    666         $this->doCallback($isSent,$to,$this->cc,$this->bcc,$this->Subject,$body);
     758        $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body);
    667759      }
    668760    } else {
    … …  
    672764          // implement call back function if it exists
    673765          $isSent = ($rt == 1) ? 1 : 0;
    674           $this->doCallback($isSent,$val,$this->cc,$this->bcc,$this->Subject,$body);
     766          $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
    675767        }
    676768      } else {
    677         $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
     769        $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
    678770        // implement call back function if it exists
    679771        $isSent = ($rt == 1) ? 1 : 0;
    680         $this->doCallback($isSent,$to,$this->cc,$this->bcc,$this->Subject,$body);
     772        $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body);
    681773      }
    682774    }
    … …  
    717809        // implement call back function if it exists
    718810        $isSent = 0;
    719         $this->doCallback($isSent,$to[0],'','',$this->Subject,$body);
     811        $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
    720812      } else {
    721813        // implement call back function if it exists
    722814        $isSent = 1;
    723         $this->doCallback($isSent,$to[0],'','',$this->Subject,$body);
     815        $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
    724816      }
    725817    }
    … …  
    729821        // implement call back function if it exists
    730822        $isSent = 0;
    731         $this->doCallback($isSent,'',$cc[0],'',$this->Subject,$body);
     823        $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
    732824      } else {
    733825        // implement call back function if it exists
    734826        $isSent = 1;
    735         $this->doCallback($isSent,'',$cc[0],'',$this->Subject,$body);
     827        $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
    736828      }
    737829    }
    … …  
    741833        // implement call back function if it exists
    742834        $isSent = 0;
    743         $this->doCallback($isSent,'','',$bcc[0],$this->Subject,$body);
     835        $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
    744836      } else {
    745837        // implement call back function if it exists
    746838        $isSent = 1;
    747         $this->doCallback($isSent,'','',$bcc[0],$this->Subject,$body);
     839        $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
    748840      }
    749841    }
    … …  
    823915    } catch (phpmailerException $e) {
    824916      $this->smtp->Reset();
    825       throw $e;
     917          if ($this->exceptions) {
     918        throw $e;
     919      }
    826920    }
    827921    return true;
    … …  
    9431037    $line = explode($this->LE, $message);
    9441038    $message = '';
    945     for ($i=0 ;$i < count($line); $i++) {
     1039    for ($i = 0 ;$i < count($line); $i++) {
    9461040      $line_part = explode(' ', $line[$i]);
    9471041      $buf = '';
    … …  
    10571151    switch($this->message_type) {
    10581152      case 'alt':
    1059       case 'alt_attachments':
     1153      case 'alt_inline':
     1154      case 'alt_attach':
     1155      case 'alt_inline_attach':
    10601156        $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
    10611157        break;
    … …  
    10781174    $this->boundary[1] = 'b1_' . $uniq_id;
    10791175    $this->boundary[2] = 'b2_' . $uniq_id;
     1176    $this->boundary[3] = 'b3_' . $uniq_id;
    10801177
    10811178    $result .= $this->HeaderLine('Date', self::RFCDate());
    … …  
    10991196        }
    11001197      }
    1101     }
     1198        }
    11021199
    11031200    $from = array();
    … …  
    11171214
    11181215    if(count($this->ReplyTo) > 0) {
    1119       $result .= $this->AddrAppend('Reply-to', $this->ReplyTo);
     1216      $result .= $this->AddrAppend('Reply-To', $this->ReplyTo);
    11201217    }
    11211218
    … …  
    11261223
    11271224    if($this->MessageID != '') {
    1128       $result .= $this->HeaderLine('Message-ID',$this->MessageID);
     1225      $result .= $this->HeaderLine('Message-ID', $this->MessageID);
    11291226    } else {
    11301227      $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
    11311228    }
    11321229    $result .= $this->HeaderLine('X-Priority', $this->Priority);
    1133     $result .= $this->HeaderLine('X-Mailer', 'PHPMailer '.$this->Version.' (phpmailer.sourceforge.net)');
     1230    if($this->XMailer) {
     1231      $result .= $this->HeaderLine('X-Mailer', $this->XMailer);
     1232    } else {
     1233      $result .= $this->HeaderLine('X-Mailer', 'PHPMailer '.$this->Version.' (http://code.google.com/a/apache-extras.org/p/phpmailer/)');
     1234    }
    11341235
    11351236    if($this->ConfirmReadingTo != '') {
    … …  
    11591260      case 'plain':
    11601261        $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
    1161         $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
     1262        $result .= $this->TextLine('Content-Type: '.$this->ContentType.'; charset="'.$this->CharSet.'"');
    11621263        break;
    1163       case 'attachments':
    1164       case 'alt_attachments':
    1165         if($this->InlineImageExists()){
    1166           $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);
    1167         } else {
    1168           $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
    1169           $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
    1170         }
     1264      case 'inline':
     1265        $result .= $this->HeaderLine('Content-Type', 'multipart/related;');
     1266        $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
     1267        break;
     1268      case 'attach':
     1269      case 'inline_attach':
     1270      case 'alt_attach':
     1271      case 'alt_inline_attach':
     1272        $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
     1273        $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
    11711274        break;
    11721275      case 'alt':
     1276      case 'alt_inline':
    11731277        $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
    11741278        $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
    … …  
    11841288
    11851289  /**
     1290   * Returns the MIME message (headers and body). Only really valid post PreSend().
     1291   * @access public
     1292   * @return string
     1293   */
     1294  public function GetSentMIMEMessage() {
     1295    return $this->SentMIMEMessage;
     1296  }
     1297
     1298
     1299  /**
    11861300   * Assembles the message body.  Returns an empty string on failure.
    11871301   * @access public
    … …  
    11981312
    11991313    switch($this->message_type) {
     1314      case 'plain':
     1315        $body .= $this->EncodeString($this->Body, $this->Encoding);
     1316        break;
     1317      case 'inline':
     1318        $body .= $this->GetBoundary($this->boundary[1], '', '', '');
     1319        $body .= $this->EncodeString($this->Body, $this->Encoding);
     1320        $body .= $this->LE.$this->LE;
     1321        $body .= $this->AttachAll("inline", $this->boundary[1]);
     1322        break;
     1323      case 'attach':
     1324        $body .= $this->GetBoundary($this->boundary[1], '', '', '');
     1325        $body .= $this->EncodeString($this->Body, $this->Encoding);
     1326        $body .= $this->LE.$this->LE;
     1327        $body .= $this->AttachAll("attachment", $this->boundary[1]);
     1328        break;
     1329      case 'inline_attach':
     1330        $body .= $this->TextLine("--" . $this->boundary[1]);
     1331        $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
     1332        $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
     1333        $body .= $this->LE;
     1334        $body .= $this->GetBoundary($this->boundary[2], '', '', '');
     1335        $body .= $this->EncodeString($this->Body, $this->Encoding);
     1336        $body .= $this->LE.$this->LE;
     1337        $body .= $this->AttachAll("inline", $this->boundary[2]);
     1338        $body .= $this->LE;
     1339        $body .= $this->AttachAll("attachment", $this->boundary[1]);
     1340        break;
    12001341      case 'alt':
    12011342        $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
    … …  
    12071348        $body .= $this->EndBoundary($this->boundary[1]);
    12081349        break;
    1209       case 'plain':
    1210         $body .= $this->EncodeString($this->Body, $this->Encoding);
    1211         break;
    1212       case 'attachments':
    1213         $body .= $this->GetBoundary($this->boundary[1], '', '', '');
    1214         $body .= $this->EncodeString($this->Body, $this->Encoding);
    1215         $body .= $this->LE;
    1216         $body .= $this->AttachAll();
    1217         break;
    1218       case 'alt_attachments':
    1219         $body .= sprintf("--%s%s", $this->boundary[1], $this->LE);
    1220         $body .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
    1221         $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
     1350      case 'alt_inline':
     1351        $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
    12221352        $body .= $this->EncodeString($this->AltBody, $this->Encoding);
    12231353        $body .= $this->LE.$this->LE;
    1224         $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
     1354        $body .= $this->TextLine("--" . $this->boundary[1]);
     1355        $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
     1356        $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
     1357        $body .= $this->LE;
     1358        $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '');
     1359        $body .= $this->EncodeString($this->Body, $this->Encoding);
     1360        $body .= $this->LE.$this->LE;
     1361        $body .= $this->AttachAll("inline", $this->boundary[2]);
     1362        $body .= $this->LE;
     1363        $body .= $this->EndBoundary($this->boundary[1]);
     1364        break;
     1365      case 'alt_attach':
     1366        $body .= $this->TextLine("--" . $this->boundary[1]);
     1367        $body .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
     1368        $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
     1369        $body .= $this->LE;
     1370        $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '');
     1371        $body .= $this->EncodeString($this->AltBody, $this->Encoding);
     1372        $body .= $this->LE.$this->LE;
     1373        $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '');
    12251374        $body .= $this->EncodeString($this->Body, $this->Encoding);
    12261375        $body .= $this->LE.$this->LE;
    12271376        $body .= $this->EndBoundary($this->boundary[2]);
    1228         $body .= $this->AttachAll();
     1377        $body .= $this->LE;
     1378        $body .= $this->AttachAll("attachment", $this->boundary[1]);
     1379        break;
     1380      case 'alt_inline_attach':
     1381        $body .= $this->TextLine("--" . $this->boundary[1]);
     1382        $body .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
     1383        $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
     1384        $body .= $this->LE;
     1385        $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '');
     1386        $body .= $this->EncodeString($this->AltBody, $this->Encoding);
     1387        $body .= $this->LE.$this->LE;
     1388        $body .= $this->TextLine("--" . $this->boundary[2]);
     1389        $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
     1390        $body .= $this->TextLine("\tboundary=\"" . $this->boundary[3] . '"');
     1391        $body .= $this->LE;
     1392        $body .= $this->GetBoundary($this->boundary[3], '', 'text/html', '');
     1393        $body .= $this->EncodeString($this->Body, $this->Encoding);
     1394        $body .= $this->LE.$this->LE;
     1395        $body .= $this->AttachAll("inline", $this->boundary[3]);
     1396        $body .= $this->LE;
     1397        $body .= $this->EndBoundary($this->boundary[2]);
     1398        $body .= $this->LE;
     1399        $body .= $this->AttachAll("attachment", $this->boundary[1]);
    12291400        break;
    12301401    }
    … …  
    12391410        if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), NULL)) {
    12401411          @unlink($file);
     1412          $body = file_get_contents($signed);
    12411413          @unlink($signed);
    1242           $body = file_get_contents($signed);
    12431414        } else {
    12441415          @unlink($file);
    … …  
    12591430  /**
    12601431   * Returns the start of a message boundary.
    1261    * @access private
    1262    */
    1263   private function GetBoundary($boundary, $charSet, $contentType, $encoding) {
     1432   * @access protected
     1433   * @return string
     1434   */
     1435  protected function GetBoundary($boundary, $charSet, $contentType, $encoding) {
    12641436    $result = '';
    12651437    if($charSet == '') {
    … …  
    12731445    }
    12741446    $result .= $this->TextLine('--' . $boundary);
    1275     $result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
     1447    $result .= sprintf("Content-Type: %s; charset=\"%s\"", $contentType, $charSet);
    12761448    $result .= $this->LE;
    12771449    $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
    … …  
    12831455  /**
    12841456   * Returns the end of a message boundary.
    1285    * @access private
    1286    */
    1287   private function EndBoundary($boundary) {
     1457   * @access protected
     1458   * @return string
     1459   */
     1460  protected function EndBoundary($boundary) {
    12881461    return $this->LE . '--' . $boundary . '--' . $this->LE;
    12891462  }
    … …  
    12911464  /**
    12921465   * Sets the message type.
    1293    * @access private
     1466   * @access protected
    12941467   * @return void
    12951468   */
    1296   private function SetMessageType() {
    1297     if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {
    1298       $this->message_type = 'plain';
    1299     } else {
    1300       if(count($this->attachment) > 0) {
    1301         $this->message_type = 'attachments';
    1302       }
    1303       if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {
    1304         $this->message_type = 'alt';
    1305       }
    1306       if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {
    1307         $this->message_type = 'alt_attachments';
    1308       }
    1309     }
     1469  protected function SetMessageType() {
     1470    $this->message_type = array();
     1471    if($this->AlternativeExists()) $this->message_type[] = "alt";
     1472    if($this->InlineImageExists()) $this->message_type[] = "inline";
     1473    if($this->AttachmentExists()) $this->message_type[] = "attach";
     1474    $this->message_type = implode("_", $this->message_type);
     1475    if($this->message_type == "") $this->message_type = "plain";
    13101476  }
    13111477
    … …  
    13681534        throw $e;
    13691535      }
    1370       echo $e->getMessage()."\n";
     1536          if ($this->SMTPDebug) {
     1537        echo $e->getMessage()."\n";
     1538      }
    13711539      if ( $e->getCode() == self::STOP_CRITICAL ) {
    13721540        return false;
    … …  
    13871555   * Attaches all fs, string, and binary attachments to the message.
    13881556   * Returns an empty string on failure.
    1389    * @access private
     1557   * @access protected
    13901558   * @return string
    13911559   */
    1392   private function AttachAll() {
     1560  protected function AttachAll($disposition_type, $boundary) {
    13931561    // Return text of body
    13941562    $mime = array();
    … …  
    13981566    // Add all attachments
    13991567    foreach ($this->attachment as $attachment) {
    1400       // Check for string attachment
    1401       $bString = $attachment[5];
    1402       if ($bString) {
    1403         $string = $attachment[0];
    1404       } else {
    1405         $path = $attachment[0];
    1406       }
    1407 
    1408       if (in_array($attachment[0], $incl)) { continue; }
    1409       $filename    = $attachment[1];
    1410       $name        = $attachment[2];
    1411       $encoding    = $attachment[3];
    1412       $type        = $attachment[4];
    1413       $disposition = $attachment[6];
    1414       $cid         = $attachment[7];
    1415       $incl[]      = $attachment[0];
    1416       if ( $disposition == 'inline' && isset($cidUniq[$cid]) ) { continue; }
    1417       $cidUniq[$cid] = true;
    1418 
    1419       $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
    1420       $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);
    1421       $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
    1422 
    1423       if($disposition == 'inline') {
    1424         $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
    1425       }
    1426 
    1427       $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);
    1428 
    1429       // Encode as string attachment
    1430       if($bString) {
    1431         $mime[] = $this->EncodeString($string, $encoding);
    1432         if($this->IsError()) {
    1433           return '';
     1568      // CHECK IF IT IS A VALID DISPOSITION_FILTER
     1569      if($attachment[6] == $disposition_type) {
     1570        // Check for string attachment
     1571        $bString = $attachment[5];
     1572        if ($bString) {
     1573          $string = $attachment[0];
     1574        } else {
     1575          $path = $attachment[0];
    14341576        }
    1435         $mime[] = $this->LE.$this->LE;
    1436       } else {
    1437         $mime[] = $this->EncodeFile($path, $encoding);
    1438         if($this->IsError()) {
    1439           return '';
     1577
     1578        $inclhash = md5(serialize($attachment));
     1579        if (in_array($inclhash, $incl)) { continue; }
     1580        $incl[]      = $inclhash;
     1581        $filename    = $attachment[1];
     1582        $name        = $attachment[2];
     1583        $encoding    = $attachment[3];
     1584        $type        = $attachment[4];
     1585        $disposition = $attachment[6];
     1586        $cid         = $attachment[7];
     1587        if ( $disposition == 'inline' && isset($cidUniq[$cid]) ) { continue; }
     1588        $cidUniq[$cid] = true;
     1589
     1590        $mime[] = sprintf("--%s%s", $boundary, $this->LE);
     1591        $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);
     1592        $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
     1593
     1594        if($disposition == 'inline') {
     1595          $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
    14401596        }
    1441         $mime[] = $this->LE.$this->LE;
    1442       }
    1443     }
    1444 
    1445     $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
    1446 
    1447     return join('', $mime);
     1597
     1598        $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);
     1599
     1600        // Encode as string attachment
     1601        if($bString) {
     1602          $mime[] = $this->EncodeString($string, $encoding);
     1603          if($this->IsError()) {
     1604            return '';
     1605          }
     1606          $mime[] = $this->LE.$this->LE;
     1607        } else {
     1608          $mime[] = $this->EncodeFile($path, $encoding);
     1609          if($this->IsError()) {
     1610            return '';
     1611          }
     1612          $mime[] = $this->LE.$this->LE;
     1613        }
     1614      }
     1615    }
     1616
     1617    $mime[] = sprintf("--%s--%s", $boundary, $this->LE);
     1618
     1619    return implode("", $mime);
    14481620  }
    14491621
    … …  
    14541626   * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
    14551627   * @see EncodeFile()
    1456    * @access private
     1628   * @access protected
    14571629   * @return string
    14581630   */
    1459   private function EncodeFile($path, $encoding = 'base64') {
     1631  protected function EncodeFile($path, $encoding = 'base64') {
    14601632    try {
    14611633      if (!is_readable($path)) {
    … …  
    14671639        }
    14681640      }
    1469       if (PHP_VERSION < 6) {
    1470         $magic_quotes = get_magic_quotes_runtime();
    1471         set_magic_quotes_runtime(0);
    1472       }
     1641          $magic_quotes = get_magic_quotes_runtime();
     1642          if ($magic_quotes) {
     1643        if (version_compare(PHP_VERSION, '5.3.0', '<')) {
     1644          set_magic_quotes_runtime(0);
     1645        } else {
     1646                  ini_set('magic_quotes_runtime', 0);
     1647                }
     1648          }
    14731649      $file_buffer  = file_get_contents($path);
    14741650      $file_buffer  = $this->EncodeString($file_buffer, $encoding);
    1475       if (PHP_VERSION < 6) { set_magic_quotes_runtime($magic_quotes); }
     1651          if ($magic_quotes) {
     1652        if (version_compare(PHP_VERSION, '5.3.0', '<')) {
     1653          set_magic_quotes_runtime($magic_quotes);
     1654        } else {
     1655                  ini_set('magic_quotes_runtime', $magic_quotes);
     1656            }
     1657          }
    14761658      return $file_buffer;
    14771659    } catch (Exception $e) {
    … …  
    14891671   * @return string
    14901672   */
    1491   public function EncodeString ($str, $encoding = 'base64') {
     1673  public function EncodeString($str, $encoding = 'base64') {
    14921674    $encoded = '';
    14931675    switch(strtolower($encoding)) {
    … …  
    16381820  */
    16391821  public function EncodeQPphp( $input = '', $line_max = 76, $space_conv = false) {
    1640     $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
     1822    $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
    16411823    $lines = preg_split('/(?:\r\n|\r|\n)/', $input);
    16421824    $eol = "\r\n";
    … …  
    17191901   * @return string
    17201902   */
    1721   public function EncodeQ ($str, $position = 'text') {
     1903  public function EncodeQ($str, $position = 'text') {
    17221904    // There should not be any EOL in the string
    17231905    $encoded = preg_replace('/[\r\n]*/', '', $str);
    … …  
    17341916        //TODO using /e (equivalent to eval()) is probably not a good idea
    17351917        $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
    1736               "'='.sprintf('%02X', ord('\\1'))", $encoded);
     1918                                "'='.sprintf('%02X', ord(stripslashes('\\1')))", $encoded);
    17371919        break;
    17381920    }
    … …  
    18081990  }
    18091991
     1992  public function AddStringEmbeddedImage($string, $cid, $filename = '', $encoding = 'base64', $type = 'application/octet-stream') {
     1993    // Append to $attachment array
     1994    $this->attachment[] = array(
     1995      0 => $string,
     1996      1 => $filename,
     1997      2 => basename($filename),
     1998      3 => $encoding,
     1999      4 => $type,
     2000      5 => true,  // isStringAttachment
     2001      6 => 'inline',
     2002      7 => $cid
     2003    );
     2004  }
     2005
    18102006  /**
    18112007   * Returns true if an inline attachment is present.
    … …  
    18202016    }
    18212017    return false;
     2018  }
     2019
     2020  public function AttachmentExists() {
     2021    foreach($this->attachment as $attachment) {
     2022      if ($attachment[6] == 'attachment') {
     2023        return true;
     2024      }
     2025    }
     2026    return false;
     2027  }
     2028
     2029  public function AlternativeExists() {
     2030    return strlen($this->AltBody)>0;
    18222031  }
    18232032
    … …  
    19342143  /**
    19352144   * Returns the server hostname or 'localhost.localdomain' if unknown.
    1936    * @access private
     2145   * @access protected
    19372146   * @return string
    19382147   */
    1939   private function ServerHostname() {
     2148  protected function ServerHostname() {
    19402149    if (!empty($this->Hostname)) {
    19412150      $result = $this->Hostname;
    … …  
    19512160  /**
    19522161   * Returns a message in the appropriate language.
    1953    * @access private
     2162   * @access protected
    19542163   * @return string
    19552164   */
    1956   private function Lang($key) {
     2165  protected function Lang($key) {
    19572166    if(count($this->language) < 1) {
    19582167      $this->SetLanguage('en'); // set the default language
    … …  
    19772186  /**
    19782187   * Changes every end of line from CR or LF to CRLF.
    1979    * @access private
     2188   * @access public
    19802189   * @return string
    19812190   */
    1982   private function FixEOL($str) {
     2191  public function FixEOL($str) {
    19832192    $str = str_replace("\r\n", "\n", $str);
    19842193    $str = str_replace("\r", "\n", $str);
    … …  
    20022211   */
    20032212  public function MsgHTML($message, $basedir = '') {
    2004     preg_match_all("/(src|background)=\"(.*)\"/Ui", $message, $images);
     2213    preg_match_all("/(src|background)=[\"'](.*)[\"']/Ui", $message, $images);
    20052214    if(isset($images[2])) {
    20062215      foreach($images[2] as $i => $url) {
    20072216        // do not change urls for absolute images (thanks to corvuscorax)
    2008         if (!preg_match('#^[A-z]+://#',$url)) {
     2217        if (!preg_match('#^[A-z]+://#', $url)) {
    20092218          $filename = basename($url);
    20102219          $directory = dirname($url);
    2011           ($directory == '.')?$directory='':'';
     2220          ($directory == '.') ? $directory='': '';
    20122221          $cid = 'cid:' . md5($filename);
    20132222          $ext = pathinfo($filename, PATHINFO_EXTENSION);
    20142223          $mimeType  = self::_mime_types($ext);
    2015           if ( strlen($basedir) > 1 && substr($basedir,-1) != '/') { $basedir .= '/'; }
    2016           if ( strlen($directory) > 1 && substr($directory,-1) != '/') { $directory .= '/'; }
    2017           if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64',$mimeType) ) {
    2018             $message = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, '/')."\"/Ui", $images[1][$i]."=\"".$cid."\"", $message);
     2224          if ( strlen($basedir) > 1 && substr($basedir, -1) != '/') { $basedir .= '/'; }
     2225          if ( strlen($directory) > 1 && substr($directory, -1) != '/') { $directory .= '/'; }
     2226          if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64', $mimeType) ) {
     2227            $message = preg_replace("/".$images[1][$i]."=[\"']".preg_quote($url, '/')."[\"']/Ui", $images[1][$i]."=\"".$cid."\"", $message);
    20192228          }
    20202229        }
    … …  
    20232232    $this->IsHTML(true);
    20242233    $this->Body = $message;
    2025     $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));
    2026     if (!empty($textMsg) && empty($this->AltBody)) {
    2027       $this->AltBody = html_entity_decode($textMsg);
    2028     }
     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        }
    20292240    if (empty($this->AltBody)) {
    20302241      $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . "\n\n";
    20312242    }
     2243        return $message;
    20322244  }
    20332245
    … …  
    21932405   */
    21942406  public function DKIM_QP($txt) {
    2195     $tmp="";
    2196     $line="";
    2197     for ($i=0;$i<strlen($txt);$i++) {
    2198       $ord=ord($txt[$i]);
     2407    $tmp = '';
     2408    $line = '';
     2409    for ($i = 0; $i < strlen($txt); $i++) {
     2410      $ord = ord($txt[$i]);
    21992411      if ( ((0x21 <= $ord) && ($ord <= 0x3A)) || $ord == 0x3C || ((0x3E <= $ord) && ($ord <= 0x7E)) ) {
    2200         $line.=$txt[$i];
     2412        $line .= $txt[$i];
    22012413      } else {
    2202         $line.="=".sprintf("%02X",$ord);
     2414        $line .= "=".sprintf("%02X", $ord);
    22032415      }
    22042416    }
    … …  
    22142426  public function DKIM_Sign($s) {
    22152427    $privKeyStr = file_get_contents($this->DKIM_private);
    2216     if ($this->DKIM_passphrase!='') {
    2217       $privKey = openssl_pkey_get_private($privKeyStr,$this->DKIM_passphrase);
     2428    if ($this->DKIM_passphrase != '') {
     2429      $privKey = openssl_pkey_get_private($privKeyStr, $this->DKIM_passphrase);
    22182430    } else {
    22192431      $privKey = $privKeyStr;
    … …  
    22312443   */
    22322444  public function DKIM_HeaderC($s) {
    2233     $s=preg_replace("/\r\n\s+/"," ",$s);
    2234     $lines=explode("\r\n",$s);
    2235     foreach ($lines as $key=>$line) {
    2236       list($heading,$value)=explode(":",$line,2);
    2237       $heading=strtolower($heading);
    2238       $value=preg_replace("/\s+/"," ",$value) ; // Compress useless spaces
    2239       $lines[$key]=$heading.":".trim($value) ; // Don't forget to remove WSP around the value
    2240     }
    2241     $s=implode("\r\n",$lines);
     2445    $s = preg_replace("/\r\n\s+/", " ", $s);
     2446    $lines = explode("\r\n", $s);
     2447    foreach ($lines as $key => $line) {
     2448      list($heading, $value) = explode(":", $line, 2);
     2449      $heading = strtolower($heading);
     2450      $value = preg_replace("/\s+/", " ", $value) ; // Compress useless spaces
     2451      $lines[$key] = $heading.":".trim($value) ; // Don't forget to remove WSP around the value
     2452    }
     2453    $s = implode("\r\n", $lines);
    22422454    return $s;
    22432455  }
    … …  
    22522464    if ($body == '') return "\r\n";
    22532465    // stabilize line endings
    2254     $body=str_replace("\r\n","\n",$body);
    2255     $body=str_replace("\n","\r\n",$body);
     2466    $body = str_replace("\r\n", "\n", $body);
     2467    $body = str_replace("\n", "\r\n", $body);
    22562468    // END stabilize line endings
    2257     while (substr($body,strlen($body)-4,4) == "\r\n\r\n") {
    2258       $body=substr($body,0,strlen($body)-2);
     2469    while (substr($body, strlen($body) - 4, 4) == "\r\n\r\n") {
     2470      $body = substr($body, 0, strlen($body) - 2);
    22592471    }
    22602472    return $body;
    … …  
    22692481   * @param string $body Body
    22702482   */
    2271   public function DKIM_Add($headers_line,$subject,$body) {
     2483  public function DKIM_Add($headers_line, $subject, $body) {
    22722484    $DKIMsignatureType    = 'rsa-sha1'; // Signature & hash algorithms
    22732485    $DKIMcanonicalization = 'relaxed/simple'; // Canonicalization of header/body
    … …  
    22752487    $DKIMtime             = time() ; // Signature Timestamp = seconds since 00:00:00 - Jan 1, 1970 (UTC time zone)
    22762488    $subject_header       = "Subject: $subject";
    2277     $headers              = explode("\r\n",$headers_line);
     2489    $headers              = explode($this->LE, $headers_line);
    22782490    foreach($headers as $header) {
    2279       if (strpos($header,'From:') === 0) {
    2280         $from_header=$header;
    2281       } elseif (strpos($header,'To:') === 0) {
    2282         $to_header=$header;
    2283       }
    2284     }
    2285     $from     = str_replace('|','=7C',$this->DKIM_QP($from_header));
    2286     $to       = str_replace('|','=7C',$this->DKIM_QP($to_header));
    2287     $subject  = str_replace('|','=7C',$this->DKIM_QP($subject_header)) ; // Copied header fields (dkim-quoted-printable
     2491      if (strpos($header, 'From:') === 0) {
     2492        $from_header = $header;
     2493      } elseif (strpos($header, 'To:') === 0) {
     2494        $to_header = $header;
     2495      }
     2496    }
     2497    $from     = str_replace('|', '=7C', $this->DKIM_QP($from_header));
     2498    $to       = str_replace('|', '=7C', $this->DKIM_QP($to_header));
     2499    $subject  = str_replace('|', '=7C', $this->DKIM_QP($subject_header)) ; // Copied header fields (dkim-quoted-printable
    22882500    $body     = $this->DKIM_BodyC($body);
    22892501    $DKIMlen  = strlen($body) ; // Length of body
    … …  
    23042516  }
    23052517
    2306   protected function doCallback($isSent,$to,$cc,$bcc,$subject,$body) {
     2518  protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body) {
    23072519    if (!empty($this->action_function) && function_exists($this->action_function)) {
    2308       $params = array($isSent,$to,$cc,$bcc,$subject,$body);
    2309       call_user_func_array($this->action_function,$params);
     2520      $params = array($isSent, $to, $cc, $bcc, $subject, $body);
     2521      call_user_func_array($this->action_function, $params);
    23102522    }
    23112523  }
Note: See TracChangeset for help on using the changeset viewer.