Changeset 1147

Show
Ignore:
Timestamp:
11/04/08 13:28:14 (2 months ago)
Author:
laurentj
Message:

Ticket #389: updated jMailer to PHPMailer 2.x. Sources of PHPMailer and SMTP are now in a dedicated directory

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/build/manifests/jelix-lib.mn

    r1141 r1147  
    635635! WSDLStruct.class.php 
    636636! WSException.class.php 
     637 
     638cd lib/phpMailer 
     639! class.phpmailer.php 
     640! class.pop3.php 
     641! class.smtp.php 
     642! LICENSE 
     643! README 
     644 
     645cd lib/phpMailer/language 
     646! phpmailer.lang-br.php 
     647! phpmailer.lang-ca.php 
     648! phpmailer.lang-cz.php 
     649! phpmailer.lang-de.php 
     650! phpmailer.lang-dk.php 
     651! phpmailer.lang-en.php 
     652! phpmailer.lang-es.php 
     653! phpmailer.lang-et.php 
     654! phpmailer.lang-fi.php 
     655! phpmailer.lang-fo.php 
     656! phpmailer.lang-fr.php 
     657! phpmailer.lang-hu.php 
     658! phpmailer.lang-it.php 
     659! phpmailer.lang-ja.php 
     660! phpmailer.lang-nl.php 
     661! phpmailer.lang-no.php 
     662! phpmailer.lang-pl.php 
     663! phpmailer.lang-ro.php 
     664! phpmailer.lang-ru.php 
     665! phpmailer.lang-se.php 
     666! phpmailer.lang-tr.php 
  • trunk/lib/jelix/core-modules/jelix/locales/en_EN/errors.UTF-8.properties

    r1017 r1147  
    105105mail.file_open = (360)jMailer : File Error: Could not open file:  %s 
    106106mail.encoding = (361)jMailer : Unknown encoding: %s 
    107  
     107mail.signing = (362) Signing Error: %s 
    108108 
    109109#---- acl 
  • trunk/lib/jelix/core-modules/jelix/locales/en_US/errors.UTF-8.properties

    r1017 r1147  
    105105mail.file_open = (360)jMailer : File Error: Could not open file:  %s 
    106106mail.encoding = (361)jMailer : Unknown encoding: %s 
    107  
     107mail.signing = (362) Signing Error: %s 
    108108 
    109109#---- acl 
  • trunk/lib/jelix/core-modules/jelix/locales/fr_FR/errors.UTF-8.properties

    r939 r1147  
    9898mail.instantiate = (353)jMailer : Impossible d'instancier la fonction mail. 
    9999mail.authenticate = (354)jMailer : SMTP Erreur: Echec de l'authentification. 
    100 mail.from_failed = (355)jMailer : L'adresse From suivante a échoué : %s 
    101 mail.recipients_failed = (356)SMTP Erreur: Les destinataires suivants sont en erreur : %s 
    102 mail.data_not_accepted = (357)SMTP Erreur: Data non acceptée
    103 mail.connect_host = (358)SMTP Erreur: Impossible de connecter le serveur SMTP . 
    104 mail.file_access = (359)jMailer : N'arrive pas à accéder au fichier: %s 
     100mail.from_failed = (355)jMailer : L'adresse d'expéditeur suivante a échoué : %s 
     101mail.recipients_failed = (356)Erreur SMTP: Les destinataires suivants sont en erreur : %s 
     102mail.data_not_accepted = (357)Erreur SMTP: Données incorrects
     103mail.connect_host = (358)Erreur SMTP: Impossible de se connecter au serveur SMTP . 
     104mail.file_access = (359)jMailer : Impossible d'accéder au fichier: %s 
    105105mail.file_open = (360)jMailer : Erreur Fichier: ouverture impossible: %s 
    106106mail.encoding = (361)jMailer : Encodage inconnu: %s 
     107mail.signing = (362)jMailer : erreur signature: %s 
    107108 
    108109#---- acl 
  • trunk/lib/jelix/utils/jMailer.class.php

    r1044 r1147  
    1111* @package     jelix 
    1212* @subpackage  utils 
    13 * @author      Brent R. Matzelle 
    14 * @contributor Laurent Jouanneau, Kévin Lepeltier 
    15 * @copyright   2001 - 2003  Brent R. Matzelle, 2006 Laurent Jouanneau 
     13* @author      Laurent Jouanneau 
     14* @contributor Kévin Lepeltier 
     15* @copyright   2006-2008 Laurent Jouanneau 
    1616* @copyright   2008 Kévin Lepeltier 
    17 * @link        http://www.jelix.org 
     17* @link        http://jelix.org 
    1818* @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
    1919*/ 
     20 
     21require(LIB_PATH.'phpMailer/class.phpmailer.php'); 
     22 
    2023 
    2124/** 
     
    2326 * @package jelix 
    2427 * @subpackage  utils 
    25  * @author Brent R. Matzelle, Andy Prevost 
    26  * @contributor Laurent Jouanneau 
    27  * @copyright 2001 - 2003 Brent R. Matzelle,  2006 Laurent Jouanneau 
    28  * @copyright 2004 - 2007 Andy Prevost 
     28 * @author Laurent Jouanneau 
     29 * @contributor Kévin Lepeltier 
     30 * @copyright   2006-2008 Laurent Jouanneau 
     31 * @copyright   2008 Kévin Lepeltier 
    2932 * @since 1.0b1 
     33 * @see PHPMailer 
    3034 */ 
    31 class jMailer { 
    32     /** 
    33      * Email priority (1 = High, 3 = Normal, 5 = low). 
    34      * @var int 
    35      */ 
    36     public $Priority          = 3; 
    37  
    38     /** 
    39      * Sets the CharSet of the message. 
    40      * @var string 
    41      */ 
    42     public $CharSet           = "iso-8859-1"; 
    43  
    44     /** 
    45      * Sets the Content-type of the message. 
    46      * @var string 
    47      */ 
    48     public $ContentType        = "text/plain"; 
    49  
    50     /** 
    51      * Sets the Encoding of the message. Options for this are "8bit", 
    52      * "7bit", "binary", "base64", and "quoted-printable". 
    53      * @var string 
    54      */ 
    55     public $Encoding          = "8bit"; 
    56  
    57     /** 
    58      * Sets the From email address for the message. 
    59      * @var string 
    60      */ 
    61     public $From               = "root@localhost"; 
    62  
    63     /** 
    64      * Sets the From name of the message. 
    65      * @var string 
    66      */ 
    67     public $FromName           = ""; 
    68  
    69     /** 
    70      * Sets the Sender email (Return-Path) of the message.  If not empty, 
    71      * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode. 
    72      * @var string 
    73      */ 
    74     public $Sender            = ""; 
    75  
    76     /** 
    77      * Sets the Subject of the message. 
    78      * @var string 
    79      */ 
    80     public $Subject           = ""; 
    81  
    82     /** 
    83      * Sets the Body of the message.  This can be either an HTML or text body. 
    84      * If HTML then run IsHTML(true). 
    85      * @var string 
    86      */ 
    87     public $Body               = ""; 
     35class jMailer extends PHPMailer { 
    8836 
    8937    /** 
     
    9442    public $bodyTpl; 
    9543 
    96     /** 
    97      * Sets the text-only body of the message.  This automatically sets the 
    98      * email to multipart/alternative.  This body can be read by mail 
    99      * clients that do not have HTML email capability such as mutt. Clients 
    100      * that can read HTML will view the normal Body. 
    101      * @var string 
    102      */ 
    103     public $AltBody           = ""; 
    104  
    105     /** 
    106      * Sets word wrapping on the body of the message to a given number of 
    107      * characters. 
    108      * @var int 
    109      */ 
    110     public $WordWrap          = 0; 
    111  
    112     /** 
    113      * Method to send mail: ("mail", "sendmail", or "smtp"). 
    114      * @var string 
    115      */ 
    116     public $Mailer            = "mail"; 
    117  
    118     /** 
    119      * Sets the path of the sendmail program. 
    120      * @var string 
    121      */ 
    122     public $Sendmail          = "/usr/sbin/sendmail"; 
    123  
    124     /** 
    125      *  Holds PHPMailer version. 
    126      *  @var string 
    127      */ 
    128     public $Version           = "1.73"; 
    129  
    130     /** 
    131      * Sets the email address that a reading confirmation will be sent. 
    132      * @var string 
    133      */ 
    134     public $ConfirmReadingTo  = ""; 
    135  
    136     /** 
    137      *  Sets the hostname to use in Message-Id and Received headers 
    138      *  and as default HELO string. If empty, the value returned 
    139      *  by SERVER_NAME is used or 'localhost.localdomain'. 
    140      *  @var string 
    141      */ 
    142     public $Hostname          = ""; 
    143  
    144     ///////////////////////////////////////////////// 
    145     // SMTP VARIABLES 
    146     ///////////////////////////////////////////////// 
    147  
    148     /** 
    149      *  Sets the SMTP hosts.  All hosts must be separated by a 
    150      *  semicolon.  You can also specify a different port 
    151      *  for each host by using this format: [hostname:port] 
    152      *  (e.g. "smtp1.example.com:25;smtp2.example.com"). 
    153      *  Hosts will be tried in order. 
    154      *  @var string 
    155      */ 
    156     public $Host        = "localhost"; 
    157  
    158     /** 
    159      *  Sets the default SMTP server port. 
    160      *  @var int 
    161      */ 
    162     public $Port        = 25; 
    163  
    164     /** 
    165      *  Sets the SMTP HELO of the message (Default is $Hostname). 
    166      *  @var string 
    167      */ 
    168     public $Helo        = ""; 
    169  
    170     /** 
    171      *  Sets SMTP authentication. Utilizes the Username and Password variables. 
    172      *  @var bool 
    173      */ 
    174     public $SMTPAuth     = false; 
    175  
    176     /** 
    177      *  Sets SMTP username. 
    178      *  @var string 
    179      */ 
    180     public $Username     = ""; 
    181  
    182     /** 
    183      *  Sets SMTP password. 
    184      *  @var string 
    185      */ 
    186     public $Password     = ""; 
    187  
    188     /** 
    189      *  Sets the SMTP server timeout in seconds. This function will not 
    190      *  work with the win32 version. 
    191      *  @var int 
    192      */ 
    193     public $Timeout      = 10; 
    194  
    195     /** 
    196      *  Sets SMTP class debugging on or off. 
    197      *  @var bool 
    198      */ 
    199     public $SMTPDebug    = false; 
    200  
    201     /** 
    202      * Prevents the SMTP connection from being closed after each mail 
    203      * sending.  If this is set to true then to close the connection 
    204      * requires an explicit call to SmtpClose(). 
    205      * @var bool 
    206      */ 
    207     public $SMTPKeepAlive = false; 
    208  
    209     /**#@+ 
    210      * @access private 
    211      */ 
    212     protected $smtp            = NULL; 
    213     protected $to              = array(); 
    214     protected $cc              = array(); 
    215     protected $bcc             = array(); 
    216     protected $ReplyTo         = array(); 
    217     protected $attachment      = array(); 
    218     protected $CustomHeader    = array(); 
    219     protected $message_type    = ""; 
    220     protected $boundary        = array(); 
    221     protected $language        = array(); 
    222     protected $error_count     = 0; 
    223     protected $LE              = "\n"; 
    224     /**#@-*/ 
    225  
    226  
     44    protected $lang; 
    22745 
    22846    /** 
     
    23149    function __construct(){ 
    23250        global $gJConfig; 
     51        $this->lang = $gJConfig->locale; 
    23352        $this->CharSet = $gJConfig->charset; 
    23453        $this->Mailer = $gJConfig->mailer['mailerType']; 
     
    24968 
    25069 
    251     ///////////////////////////////////////////////// 
    252     // VARIABLE METHODS 
    253     ///////////////////////////////////////////////// 
    254  
    255     /** 
    256      * Sets message type to HTML. 
    257      * @param bool $bool 
    258      * @return void 
    259      */ 
    260     function IsHTML($bool) { 
    261         if($bool == true) 
    262             $this->ContentType = "text/html"; 
    263         else 
    264             $this->ContentType = "text/plain"; 
    265     } 
    266  
    267     /** 
    268      * Sets Mailer to send message using SMTP. 
    269      * @return void 
    270      */ 
    271     function IsSMTP() { 
    272         $this->Mailer = "smtp"; 
    273     } 
    274  
    275     /** 
    276      * Sets Mailer to send message using PHP mail() function. 
    277      * @return void 
    278      */ 
    279     function IsMail() { 
    280         $this->Mailer = "mail"; 
    281     } 
    282  
    283     /** 
    284      * Sets Mailer to send message using the $Sendmail program. 
    285      * @return void 
    286      */ 
    287     function IsSendmail() { 
    288         $this->Mailer = "sendmail"; 
    289     } 
    290  
    291     /** 
    292      * Sets Mailer to send message using the qmail MTA. 
    293      * @return void 
    294      */ 
    295     function IsQmail() { 
    296         $this->Sendmail = "/var/qmail/bin/sendmail"; 
    297         $this->Mailer = "sendmail"; 
    298     } 
    299  
    300  
    301     ///////////////////////////////////////////////// 
    302     // RECIPIENT METHODS 
    303     ///////////////////////////////////////////////// 
    304  
    305     /** 
    306      * Adds a "To" address. 
    307      * @param string $address 
    308      * @param string $name 
    309      * @return void 
    310      */ 
    311     function AddAddress($address, $name = "") { 
    312         $cur = count($this->to); 
    313         $this->to[$cur][0] = trim($address); 
    314         $this->to[$cur][1] = $name; 
    315     } 
    316  
    317     /** 
    318      * Adds a "Cc" address. Note: this function works 
    319      * with the SMTP mailer on win32, not with the "mail" 
    320      * mailer. 
    321      * @param string $address 
    322      * @param string $name 
    323      * @return void 
    324     */ 
    325     function AddCC($address, $name = "") { 
    326         $cur = count($this->cc); 
    327         $this->cc[$cur][0] = trim($address); 
    328         $this->cc[$cur][1] = $name; 
    329     } 
    330  
    331     /** 
    332      * Adds a "Bcc" address. Note: this function works 
    333      * with the SMTP mailer on win32, not with the "mail" 
    334      * mailer. 
    335      * @param string $address 
    336      * @param string $name 
    337      * @return void 
    338      */ 
    339     function AddBCC($address, $name = "") { 
    340         $cur = count($this->bcc); 
    341         $this->bcc[$cur][0] = trim($address); 
    342         $this->bcc[$cur][1] = $name; 
    343     } 
    344  
    345     /** 
    346      * Adds a "Reply-to" address. 
    347      * @param string $address 
    348      * @param string $name 
    349      * @return void 
    350      */ 
    351     function AddReplyTo($address, $name = "") { 
    352         $cur = count($this->ReplyTo); 
    353         $this->ReplyTo[$cur][0] = trim($address); 
    354         $this->ReplyTo[$cur][1] = $name; 
    355     } 
    356      
    357  
    35870    /** 
    35971     * Find the name and address in the form "name<address@hop.tld>" 
     
    36274     */ 
    36375    function getAddrName($address) { 
    364          
    36576        preg_match ('`^([^<]*)<([^>]*)>$`', $address, $tab ); 
    36677        array_shift($tab); 
    36778        return $tab; 
    368          
    36979    } 
    37080 
     
    37585     */ 
    37686    function Tpl( $selector ) { 
    377          
    37887        $this->bodyTpl = $selector; 
    379          
    38088    } 
    38189 
     
    394102        $body = ""; 
    395103        $result = true; 
    396          
     104 
    397105        if( isset($this->bodyTpl) && $this->bodyTpl != "") { 
    398              
     106 
    399107            $mailtpl = new jTpl(); 
    400108            $metas = $mailtpl->meta( $this->bodyTpl ); 
    401              
     109 
    402110            if( isset($metas['Subject']) ) 
    403111                $this->Subject = $metas['Subject']; 
    404                  
     112 
    405113            if( isset($metas['Priority']) ) 
    406114                $this->Priority = $metas['Priority']; 
    407115            $mailtpl->assign('Priority', $this->Priority ); 
    408                  
     116 
    409117            if( isset($metas['From']) ) { 
    410118                $adr = $this->getAddrName( $metas['From'] ); 
     
    414122            $mailtpl->assign('From', $this->From ); 
    415123            $mailtpl->assign('FromName', $this->FromName ); 
    416              
     124 
    417125            if( isset($metas['Sender']) ) 
    418126                $this->Sender = $metas['Sender']; 
    419127            $mailtpl->assign('Sender', $this->Sender ); 
    420              
     128 
    421129            if( isset($metas['to']) ) 
    422130                foreach( $metas['to'] as $val ) 
    423131                    $this->to[] = $this->getAddrName( $val ); 
    424132            $mailtpl->assign('to', $this->to ); 
    425                      
     133 
    426134            if( isset($metas['cc']) ) 
    427135                foreach( $metas['cc'] as $val ) 
    428136                    $this->cc[] = $this->getAddrName( $val ); 
    429137            $mailtpl->assign('cc', $this->cc ); 
    430                      
     138 
    431139            if( isset($metas['bcc']) ) 
    432140                foreach( $metas['bcc'] as $val ) 
    433141                    $this->bcc[] = $this->getAddrName( $val ); 
    434142            $mailtpl->assign('bcc', $this->bcc ); 
    435                      
     143 
    436144            if( isset($metas['ReplyTo']) ) 
    437145                foreach( $metas['ReplyTo'] as $val ) 
    438146                    $this->ReplyTo[] = $this->getAddrName( $val ); 
    439147            $mailtpl->assign('ReplyTo', $this->ReplyTo ); 
    440              
     148 
    441149            $this->Body = $mailtpl->fetch( $this->bodyTpl ); 
    442              
    443150        } 
    444151 
    445         if((count($this->to) + count($this->cc) + count($this->bcc)) < 1) 
    446         { 
    447             throw new jException('jelix~errors.mail.provide_address'); 
    448         } 
    449  
    450         // Set whether the message is multipart/alternative 
    451         if(!empty($this->AltBody)) 
    452             $this->ContentType = "multipart/alternative"; 
    453  
    454         $this->error_count = 0; // reset errors 
    455         $this->SetMessageType(); 
    456         $header .= $this->CreateHeader(); 
    457         $body = $this->CreateBody(); 
    458  
    459         if($body == "") { return false; } 
    460  
    461         // Choose the mailer 
    462         switch($this->Mailer) 
    463         { 
    464             case "sendmail": 
    465                 $result = $this->SendmailSend($header, $body); 
    466                 break; 
    467             case "mail": 
    468                 $result = $this->MailSend($header, $body); 
    469                 break; 
    470             case "smtp": 
    471                 $result = $this->SmtpSend($header, $body); 
    472                 break; 
    473             default: 
    474                 throw new jException('jelix~errors.mail.mailer_not_supported', $this->Mailer); 
    475                 break; 
    476         } 
    477  
    478         return $result; 
     152        return parent::Send(); 
    479153    } 
    480154 
    481     /** 
    482      * Sends mail using the $Sendmail program. 
    483      * @return bool 
    484      */ 
    485     protected function SendmailSend($header, $body) { 
    486         if ($this->Sender != "") 
    487             $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender)); 
    488         else 
    489             $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail)); 
    490  
    491         if(!@$mail = popen($sendmail, "w")) 
    492         { 
    493             throw new jException('jelix~errors.mail.execute', $this->Sendmail); 
    494         } 
    495  
    496         fputs($mail, $header); 
    497         fputs($mail, $body); 
    498  
    499         $result = pclose($mail) >> 8 & 0xFF; 
    500         if($result != 0) 
    501         { 
    502             throw new jException('jelix~errors.mail.execute', $this->Sendmail); 
    503         } 
    504  
    505         return true; 
     155    function SetLanguage($lang_type = 'en_EN', $lang_path = 'language/') { 
     156        $this->lang = $lang_type; 
    506157    } 
    507158 
    508     /** 
    509      * Sends mail using the PHP mail() function. 
    510      * @return bool 
    511      */ 
    512     protected function MailSend($header, $body) { 
    513         $to = ""; 
    514         for($i = 0; $i < count($this->to); $i++) 
    515         { 
    516             if($i != 0) { $to .= ", "; } 
    517             $to .= $this->to[$i][0]; 
     159    protected function SetError($msg) { 
     160        if (preg_match("/^([^#]*)#([^#]+)#(.*)$/", $msg, $m)) { 
     161            $arg = null; 
     162            if($m[1] != '') 
     163                $arg = $m[1]; 
     164            if($m[3] != '') 
     165                $arg = $m[3]; 
     166            if(strpos($m[2], 'WARNING:') !== false) { 
     167                $locale = 'jelix~errors.mail.'.substr($m[2],8); 
     168                if($arg !== null)  
     169                    parent::SetError(jLocale::get($locale, $arg, $this->lang, $this->CharSet)); 
     170                else 
     171                    parent::SetError(jLocale::get($locale, $arg, $this->lang, $this->CharSet)); 
     172                return; 
     173            } 
     174            $locale = 'jelix~errors.mail.'.$m[2]; 
     175            if ($arg !== null) { 
     176                throw new jException($locale, $arg, 1, $this->lang, $this->CharSet); 
     177            } 
     178            else 
     179                throw new jException($locale, array(), $this->lang, $this->CharSet); 
    518180        } 
    519  
    520         if ($this->Sender != "" && strlen(ini_get("safe_mode"))< 1) 
    521         { 
    522             $old_from = ini_get("sendmail_from"); 
    523             ini_set("sendmail_from", $this->Sender); 
    524             $params = sprintf("-oi -f %s", $this->Sender); 
    525             $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, 
    526                         $header, $params); 
    527         } 
    528         else 
    529             $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header); 
    530  
    531         if (isset($old_from)) 
    532             ini_set("sendmail_from", $old_from); 
    533  
    534         if(!$rt) 
    535         { 
    536             throw new jException('jelix~errors.mail.instantiate'); 
    537         } 
    538  
    539         return true; 
    540     } 
    541  
    542     /** 
    543      * Sends mail via SMTP using PhpSMTP (Author: 
    544      * Chris Ryan).  Returns bool.  Returns false if there is a 
    545      * bad MAIL FROM, RCPT, or DATA input. 
    546      * @return bool 
    547      */ 
    548     protected function SmtpSend($header, $body) { 
    549         require_once(JELIX_LIB_UTILS_PATH.'jSmtp.class.php'); 
    550         $bad_rcpt = array(); 
    551  
    552         if(!$this->SmtpConnect()) 
    553             return false; 
    554  
    555         $smtp_from = ($this->Sender == "") ? $this->From : $this->Sender; 
    556         if(!$this->smtp->Mail($smtp_from)) 
    557         { 
    558             $this->smtp->Reset(); 
    559             throw new jException('jelix~errors.mail.from_failed',$smtp_from); 
    560         } 
    561  
    562         // Attempt to send attach all recipients 
    563         for($i = 0; $i < count($this->to); $i++) 
    564         { 
    565             if(!$this->smtp->Recipient($this->to[$i][0])) 
    566                 $bad_rcpt[] = $this->to[$i][0]; 
    567         } 
    568         for($i = 0; $i < count($this->cc); $i++) 
    569         { 
    570             if(!$this->smtp->Recipient($this->cc[$i][0])) 
    571                 $bad_rcpt[] = $this->cc[$i][0]; 
    572         } 
    573         for($i = 0; $i < count($this->bcc); $i++) 
    574         { 
    575             if(!$this->smtp->Recipient($this->bcc[$i][0])) 
    576                 $bad_rcpt[] = $this->bcc[$i][0]; 
    577         } 
    578  
    579         if(count($bad_rcpt) > 0) // Create error message 
    580         { 
    581             $error = ''; 
    582             for($i = 0; $i < count($bad_rcpt); $i++) 
    583             { 
    584                 if($i != 0) { $error .= ", "; } 
    585                 $error .= $bad_rcpt[$i]; 
    586             } 
    587             $this->smtp->Reset(); 
    588             throw new jException('jelix~errors.mail.recipients_failed',$error); 
    589         } 
    590  
    591         if(!$this->smtp->Data($header . $body)) 
    592         { 
    593  
    594             $this->smtp->Reset(); 
    595             throw new jException('jelix~errors.mail.data_not_accepted'); 
    596         } 
    597         if($this->SMTPKeepAlive == true) 
    598             $this->smtp->Reset(); 
    599         else 
    600             $this->SmtpClose(); 
    601  
    602         return true; 
    603     } 
    604  
    605     /** 
    606      * Initiates a connection to an SMTP server.  Returns false if the 
    607      * operation failed. 
    608      * @access private 
    609      * @return bool 
    610      */ 
    611     protected function SmtpConnect() { 
    612         if($this->smtp == NULL) { $this->smtp = new jSmtp(); } 
    613  
    614         $this->smtp->do_debug = $this->SMTPDebug; 
    615         $hosts = explode(";", $this->Host); 
    616         $index = 0; 
    617         $connection = ($this->smtp->Connected()); 
    618  
    619         // Retry while there is no connection 
    620         while($index < count($hosts) && $connection == false) 
    621         { 
    622             if(strstr($hosts[$index], ":")) 
    623                 list($host, $port) = explode(":", $hosts[$index]); 
    624             else 
    625             { 
    626                 $host = $hosts[$index]; 
    627                 $port = $this->Port; 
    628             } 
    629  
    630             if($this->smtp->Connect($host, $port, $this->Timeout)) 
    631             { 
    632                 if ($this->Helo != '') 
    633                     $this->smtp->Hello($this->Helo); 
    634                 else 
    635                     $this->smtp->Hello($this->ServerHostname()); 
    636  
    637                 if($this->SMTPAuth) 
    638                 { 
    639                     if(!$this->smtp->Authenticate($this->Username, 
    640                                                   $this->Password)) 
    641                     { 
    642                         $this->smtp->Reset(); 
    643                         throw new jException('jelix~errors.mail.authenticate'); 
    644                     } 
    645                 } 
    646                 $connection = true; 
    647             } 
    648             $index++; 
    649         } 
    650         if(!$connection) 
    651             throw new jException('jelix~errors.mail.connect_host'); 
    652  
    653         return $connection; 
    654     } 
    655  
    656     /** 
    657      * Closes the active SMTP session if one exists. 
    658      * @return void 
    659      */ 
    660     public function SmtpClose() { 
    661         if($this->smtp != NULL) 
    662         { 
    663             if($this->smtp->Connected()) 
    664             { 
    665                 $this->smtp->Quit(); 
    666                 $this->smtp->Close(); 
    667             } 
    668         } 
    669     } 
    670  
    671     ///////////////////////////////////////////////// 
    672     // MESSAGE CREATION METHODS 
    673     ///////////////////////////////////////////////// 
    674  
    675     /** 
    676      * Creates recipient headers. 
    677      * @return string 
    678      */ 
    679     protected function AddrAppend($type, $addr) { 
    680         $addr_str = $type . ": "; 
    681         $addr_str .= $this->AddrFormat($addr[0]); 
    682         if(count($addr) > 1) 
    683         { 
    684             for($i = 1; $i < count($addr); $i++) 
    685                 $addr_str .= ", " . $this->AddrFormat($addr[$i]); 
    686         } 
    687         $addr_str .= $this->LE; 
    688  
    689         return $addr_str; 
    690     } 
    691  
    692     /** 
    693      * Formats an address correctly. 
    694      * @return string 
    695      */ 
    696     protected function AddrFormat($addr) { 
    697         if(empty($addr[1])) 
    698             $formatted = $addr[0]; 
    699         else 
    700         { 
    701             $formatted = $this->EncodeHeader($addr[1], 'phrase') . " <" . 
    702                          $addr[0] . ">"; 
    703         } 
    704  
    705         return $formatted; 
    706     } 
    707  
    708     /** 
    709      * Wraps message for use with mailers that do not 
    710      * automatically perform wrapping and for quoted-printable. 
    711      * Original written by philippe. 
    712      * @internal the code of this method has been picked from version 2.0.0 og PHPMailer 
    713      * @return string 
    714      */ 
    715     protected function WrapText($message, $length, $qp_mode = false) { 
    716         $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE; 
    717  
    718         $message = $this->FixEOL($message); 
    719         if (substr($message, -1) == $this->LE) { 
    720             $message = substr($message, 0, -1); 
    721         } 
    722  
    723         $line = explode($this->LE, $message); 
    724         $message = ''; 
    725         for ($i=0 ;$i < count($line); $i++) { 
    726             $line_part = explode(' ', $line[$i]); 
    727             $buf = ''; 
    728             for ($e = 0; $e<count($line_part); $e++) { 
    729                 $word = $line_part[$e]; 
    730                 if ($qp_mode and (strlen($word) > $length)) { 
    731                     $space_left = $length - strlen($buf) - 1; 
    732                     if ($e != 0) { 
    733                         if ($space_left > 20) { 
    734                             $len = $space_left; 
    735                             if (substr($word, $len - 1, 1) == '=') { 
    736                                 $len--; 
    737                             } elseif (substr($word, $len - 2, 1) == '=') { 
    738                                 $len -= 2; 
    739                             } 
    740                             $part = substr($word, 0, $len); 
    741                             $word = substr($word, $len); 
    742                             $buf .= ' ' . $part; 
    743                             $message .= $buf . sprintf("=%s", $this->LE); 
    744                         } else { 
    745                             $message .= $buf . $soft_break; 
    746                         } 
    747                         $buf = ''; 
    748                     } 
    749                     while (strlen($word) > 0) { 
    750                         $len = $length; 
    751                         if (substr($word, $len - 1, 1) == '=') { 
    752                             $len--; 
    753                         } elseif (substr($word, $len - 2, 1) == '=') { 
    754                             $len -= 2; 
    755                         } 
    756                         $part = substr($word, 0, $len); 
    757                         $word = substr($word, $len); 
    758  
    759                         if (strlen($word) > 0) { 
    760                             $message .= $part . sprintf("=%s", $this->LE); 
    761                         } else { 
    762                             $buf = $part; 
    763                         } 
    764                     } 
    765                 } else { 
    766                     $buf_o = $buf; 
    767                     $buf .= ($e == 0) ? $word : (' ' . $word); 
    768  
    769                     if (strlen($buf) > $length and $buf_o != '') { 
    770                         $message .= $buf_o . $soft_break; 
    771                         $buf = $word; 
    772                     } 
    773                 } 
    774             } 
    775             $message .= $buf . $this->LE; 
    776         } 
    777  
    778         return $message; 
    779     } 
    780  
    781     /** 
    782      * Set the body wrapping. 
    783      * @return void 
    784      */ 
    785     protected function SetWordWrap() { 
    786         if($this->WordWrap < 1) 
    787             return; 
    788  
    789         switch($this->message_type) 
    790         { 
    791            case "alt": 
    792               // fall through 
    793            case "alt_attachments": 
    794               $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap); 
    795               break; 
    796            default: 
    797               $this->Body = $this->WrapText($this->Body, $this->WordWrap); 
    798               break; 
     181        else { 
     182            throw new Exception($msg); 
    799183        } 
    800184    } 
    801185 
    802186    /** 
    803      * Assembles message header. 
    804      * @return string 
    805      */ 
    806     protected function CreateHeader() { 
    807         $result = ""; 
    808  
    809         // Set the boundaries 
    810         $uniq_id = md5(uniqid(time())); 
    811         $this->boundary[1] = "b1_" . $uniq_id; 
    812         $this->boundary[2] = "b2_" . $uniq_id; 
    813  
    814         $result .= $this->HeaderLine("Date", $this->RFCDate()); 
    815         if($this->Sender == "") 
    816             $result .= $this->HeaderLine("Return-Path", trim($this->From)); 
    817         else 
    818             $result .= $this->HeaderLine("Return-Path", trim($this->Sender)); 
    819  
    820         // To be created automatically by mail() 
    821         if($this->Mailer != "mail") 
    822         { 
    823             if(count($this->to) > 0) 
    824                 $result .= $this->AddrAppend("To", $this->to); 
    825             else if (count($this->cc) == 0) 
    826                 $result .= $this->HeaderLine("To", "undisclosed-recipients:;"); 
    827             if(count($this->cc) > 0) 
    828                 $result .= $this->AddrAppend("Cc", $this->cc); 
    829         } 
    830  
    831         $from = array(); 
    832         $from[0][0] = trim($this->From); 
    833         $from[0][1] = $this->FromName; 
    834         $result .= $this->AddrAppend("From", $from); 
    835  
    836         // sendmail and mail() extract Bcc from the header before sending 
    837         if((($this->Mailer == "sendmail") || ($this->Mailer == "mail")) && (count($this->bcc) > 0)) 
    838             $result .= $this->AddrAppend("Bcc", $this->bcc); 
    839  
    840         if(count($this->ReplyTo) > 0) 
    841             $result .= $this->AddrAppend("Reply-to", $this->ReplyTo); 
    842  
    843         // mail() sets the subject itself 
    844         if($this->Mailer != "mail") 
    845             $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject))); 
    846  
    847         $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE); 
    848         $result .= $this->HeaderLine("X-Priority", $this->Priority); 
    849         $result .= $this->HeaderLine("X-Mailer", "PHPMailer [version " . $this->Version . "]"); 
    850  
    851         if($this->ConfirmReadingTo != "") 
    852         { 
    853             $result .= $this->HeaderLine("Disposition-Notification-To", 
    854                        "<" . trim($this->ConfirmReadingTo) . ">"); 
    855         } 
    856  
    857         // Add custom headers 
    858         for($index = 0; $index < count($this->CustomHeader); $index++) 
    859         { 
    860             $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), 
    861                        $this->EncodeHeader(trim($this->CustomHeader[$index][1]))); 
    862         } 
    863         $result .= $this->HeaderLine("MIME-Version", "1.0"); 
    864  
    865         switch($this->message_type) 
    866         { 
    867             case "plain": 
    868                 $result .= $this->HeaderLine("Content-Transfer-Encoding", $this->Encoding); 
    869                 $result .= sprintf("Content-Type: %s; charset=\"%s\"", 
    870                                     $this->ContentType, $this->CharSet); 
    871                 break; 
    872             case "attachments": 
    873                 // fall through 
    874     &n