Xataface Email Module 0.2
Email/Mailmerge Module for Xataface
lib/XPM/PHP5/MAIL5.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  *                                                                                         *
00005  *  XPertMailer is a PHP Mail Class that can send and read messages in MIME format.        *
00006  *  This file is part of the XPertMailer package (http://xpertmailer.sourceforge.net/)     *
00007  *  Copyright (C) 2007 Tanase Laurentiu Iulian                                             *
00008  *                                                                                         *
00009  *  This library is free software; you can redistribute it and/or modify it under the      *
00010  *  terms of the GNU Lesser General Public License as published by the Free Software       *
00011  *  Foundation; either version 2.1 of the License, or (at your option) any later version.  *
00012  *                                                                                         *
00013  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A        *
00015  *  PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.        *
00016  *                                                                                         *
00017  *  You should have received a copy of the GNU Lesser General Public License along with    *
00018  *  this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  *  Fifth Floor, Boston, MA 02110-1301, USA                                                *
00020  *                                                                                         *
00021  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00022 
00023 if (!class_exists('SMTP5')) require_once 'SMTP5.php';
00024 
00025 class MAIL5 {
00026 
00027         public $From = null;
00028         public $To = array();
00029         public $Cc = array();
00030         public $Bcc = array();
00031 
00032         public $Subject = null;
00033         public $Text = null;
00034         public $Html = null;
00035         public $Header = array();
00036         public $Attach = array();
00037 
00038         public $Host = null;
00039         public $Port = null;
00040         public $User = null;
00041         public $Pass = null;
00042         public $Vssl = null;
00043         public $Tout = null;
00044         public $Auth = null;
00045 
00046         public $Name = null;
00047         public $Path = null;
00048         public $Priority = null;
00049 
00050         public $Context = null;
00051 
00052         public $SendMail = '/usr/sbin/sendmail';
00053         public $QMail = '/var/qmail/bin/sendmail';
00054 
00055         private $_conns = array();
00056         public $History = array();
00057         public $Result = null;
00058 
00059         public function __construct() {
00060                 $this->_result(array(0 => 'initialize class'));
00061         }
00062 
00063         private function _result($data = array(), $ret = null) {
00064                 $this->History[][strval(microtime(true))] = $data;
00065                 $this->Result = $data;
00066                 return $ret;
00067         }
00068 
00069         public function context($arr = null, $debug = null) {
00070                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00071                 if (!is_array($arr)) FUNC5::trace($debug, 'invalid context type');
00072                 else if (!is_resource($res = stream_context_create($arr))) FUNC5::trace($debug, 'invalid context value');
00073                 else {
00074                         $this->Context = $res;
00075                         return $this->_result(array(0 => 'set context connection'), true);
00076                 }
00077         }
00078 
00079         public function name($host = null, $debug = null) {
00080                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00081                 if (!is_string($host)) FUNC5::trace($debug, 'invalid hostname type');
00082                 else {
00083                         $host = strtolower(trim($host));
00084                         if (!($host != '' && ($host == 'localhost' || FUNC5::is_ipv4($host) || FUNC5::is_hostname($host, true, $debug)))) FUNC5::trace($debug, 'invalid hostname value');
00085                         $this->Name = $host;
00086                         return $this->_result(array(0 => 'set HELO/EHLO hostname'), true);
00087                 }
00088         }
00089 
00090         public function path($addr = null, $debug = null) {
00091                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00092                 if (!is_string($addr)) FUNC5::trace($debug, 'invalid address type');
00093                 else {
00094                         if (!($addr != '' && FUNC5::is_mail($addr))) FUNC5::trace($debug, 'invalid address value');
00095                         $this->Path = $addr;
00096                         return $this->_result(array(0 => 'set Return-Path address'), true);
00097                 }
00098         }
00099 
00100         public function priority($level = null, $debug = null) {
00101                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00102                 if ($level == null) {
00103                         $this->Priority = null;
00104                         return $this->_result(array(0 => 'unset priority'), true);
00105                 } else if (is_int($level) || is_string($level)) {
00106                         if (is_string($level)) $level = strtolower(trim(FUNC5::str_clear($level)));
00107                         if ($level == 1 || $level == 3 || $level == 5 || $level == 'high' || $level == 'normal' || $level == 'low') {
00108                                 $this->Priority = $level;
00109                                 return $this->_result(array(0 => 'set priority'), true);
00110                         } else FUNC5::trace($debug, 'invalid level value');
00111                 } else FUNC5::trace($debug, 'invalid level type');
00112         }
00113 
00114         public function from($addr = null, $name = null, $charset = null, $encoding = null, $debug = null) {
00115                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00116                 $err = array();
00117                 if (!is_string($addr)) $err[] = 'invalid address type';
00118                 else if (!FUNC5::is_mail($addr)) $err[] = 'invalid address value';
00119                 if ($name != null) {
00120                         if (!is_string($name)) $err[] = 'invalid name type';
00121                         else {
00122                                 $name = trim(FUNC5::str_clear($name));
00123                                 if ($name == '') $err[] = 'invalid name value';
00124                         }
00125                 }
00126                 if ($charset != null) {
00127                         if (!is_string($charset)) $err[] = 'invalid charset type';
00128                         else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
00129                 }
00130                 if ($encoding != null) {
00131                         if (!is_string($encoding)) $err[] = 'invalid encoding type';
00132                         else {
00133                                 $encoding = strtolower($encoding);
00134                                 if (!isset(MIME5::$hencarr[$encoding])) $err[] = 'invalid encoding value';
00135                         }
00136                 }
00137                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00138                 else {
00139                         $this->From = array('address' => $addr, 'name' => $name, 'charset' => $charset, 'encoding' => $encoding);
00140                         return $this->_result(array(0 => 'set From address'), true);
00141                 }
00142         }
00143 
00144         public function addto($addr = null, $name = null, $charset = null, $encoding = null, $debug = null) {
00145                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00146                 $err = array();
00147                 if (!is_string($addr)) $err[] = 'invalid address type';
00148                 else if (!FUNC5::is_mail($addr)) $err[] = 'invalid address value';
00149                 if ($name != null) {
00150                         if (!is_string($name)) $err[] = 'invalid name type';
00151                         else {
00152                                 $name = trim(FUNC5::str_clear($name));
00153                                 if ($name == '') $err[] = 'invalid name value';
00154                         }
00155                 }
00156                 if ($charset != null) {
00157                         if (!is_string($charset)) $err[] = 'invalid charset type';
00158                         else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
00159                 }
00160                 if ($encoding != null) {
00161                         if (!is_string($encoding)) $err[] = 'invalid encoding type';
00162                         else {
00163                                 $encoding = strtolower($encoding);
00164                                 if (!isset(MIME5::$hencarr[$encoding])) $err[] = 'invalid encoding value';
00165                         }
00166                 }
00167                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00168                 else {
00169                         $find = false;
00170                         if (count($this->To) > 0) {
00171                                 $ladr = strtolower($addr);
00172                                 foreach ($this->To as $to) {
00173                                         if ($ladr == strtolower($to['address'])) {
00174                                                 FUNC5::trace($debug, 'duplicate To address "'.$addr.'"', 1);
00175                                                 $find = true;
00176                                         }
00177                                 }
00178                         }
00179                         if ($find) return false;
00180                         else {
00181                                 $this->To[] = array('address' => $addr, 'name' => $name, 'charset' => $charset, 'encoding' => $encoding);
00182                                 return $this->_result(array(0 => 'add To address'), true);
00183                         }
00184                 }
00185         }
00186 
00187         public function delto($addr = null, $debug = null) {
00188                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00189                 if ($addr == null) {
00190                         $this->To = array();
00191                         return $this->_result(array(0 => 'delete all To addresses'), true);
00192                 } else if (!(is_string($addr) && FUNC5::is_mail($addr))) {
00193                         FUNC5::trace($debug, 'invalid address value');
00194                 } else {
00195                         $ret = false;
00196                         $new = array();
00197                         if (count($this->To) > 0) {
00198                                 $addr = strtolower($addr);
00199                                 foreach ($this->To as $to) {
00200                                         if ($addr == strtolower($to['address'])) $ret = true;
00201                                         else $new[] = $to;
00202                                 }
00203                         }
00204                         if ($ret) {
00205                                 $this->To = $new;
00206                                 return $this->_result(array(0 => 'delete To address'), true);
00207                         } else return FUNC5::trace($debug, 'To address "'.$addr.'" not found', 1);
00208                 }
00209         }
00210 
00211         public function addcc($addr = null, $name = null, $charset = null, $encoding = null, $debug = null) {
00212                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00213                 $err = array();
00214                 if (!is_string($addr)) $err[] = 'invalid address type';
00215                 else if (!FUNC5::is_mail($addr)) $err[] = 'invalid address value';
00216                 if ($name != null) {
00217                         if (!is_string($name)) $err[] = 'invalid name type';
00218                         else {
00219                                 $name = trim(FUNC5::str_clear($name));
00220                                 if ($name == '') $err[] = 'invalid name value';
00221                         }
00222                 }
00223                 if ($charset != null) {
00224                         if (!is_string($charset)) $err[] = 'invalid charset type';
00225                         else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
00226                 }
00227                 if ($encoding != null) {
00228                         if (!is_string($encoding)) $err[] = 'invalid encoding type';
00229                         else {
00230                                 $encoding = strtolower($encoding);
00231                                 if (!isset(MIME5::$hencarr[$encoding])) $err[] = 'invalid encoding value';
00232                         }
00233                 }
00234                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00235                 else {
00236                         $find = false;
00237                         if (count($this->Cc) > 0) {
00238                                 $ladr = strtolower($addr);
00239                                 foreach ($this->Cc as $cc) {
00240                                         if ($ladr == strtolower($cc['address'])) {
00241                                                 FUNC5::trace($debug, 'duplicate Cc address "'.$addr.'"', 1);
00242                                                 $find = true;
00243                                         }
00244                                 }
00245                         }
00246                         if ($find) return false;
00247                         else {
00248                                 $this->Cc[] = array('address' => $addr, 'name' => $name, 'charset' => $charset, 'encoding' => $encoding);
00249                                 return $this->_result(array(0 => 'add Cc address'), true);
00250                         }
00251                 }
00252         }
00253 
00254         public function delcc($addr = null, $debug = null) {
00255                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00256                 if ($addr == null) {
00257                         $this->Cc = array();
00258                         return $this->_result(array(0 => 'delete all Cc addresses'), true);
00259                 } else if (!(is_string($addr) && FUNC5::is_mail($addr))) {
00260                         FUNC5::trace($debug, 'invalid address value');
00261                 } else {
00262                         $ret = false;
00263                         $new = array();
00264                         if (count($this->Cc) > 0) {
00265                                 $addr = strtolower($addr);
00266                                 foreach ($this->Cc as $cc) {
00267                                         if ($addr == strtolower($cc['address'])) $ret = true;
00268                                         else $new[] = $cc;
00269                                 }
00270                         }
00271                         if ($ret) {
00272                                 $this->Cc = $new;
00273                                 return $this->_result(array(0 => 'delete Cc address'), true);
00274                         } else return FUNC5::trace($debug, 'Cc address "'.$addr.'" not found', 1);
00275                 }
00276         }
00277 
00278         public function addbcc($addr = null, $debug = null) {
00279                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00280                 if (!is_string($addr)) FUNC5::trace($debug, 'invalid address type');
00281                 else if (!FUNC5::is_mail($addr)) FUNC5::trace($debug, 'invalid address value');
00282                 $find = false;
00283                 if (count($this->Bcc) > 0) {
00284                         $ladr = strtolower($addr);
00285                         foreach ($this->Bcc as $bcc) {
00286                                 if ($ladr == strtolower($bcc)) {
00287                                         FUNC5::trace($debug, 'duplicate Bcc address "'.$addr.'"', 1);
00288                                         $find = true;
00289                                 }
00290                         }
00291                 }
00292                 if ($find) return false;
00293                 else {
00294                         $this->Bcc[] = $addr;
00295                         return $this->_result(array(0 => 'add Bcc address'), true);
00296                 }
00297         }
00298 
00299         public function delbcc($addr = null, $debug = null) {
00300                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00301                 if ($addr == null) {
00302                         $this->Bcc = array();
00303                         return $this->_result(array(0 => 'delete all Bcc addresses'), true);
00304                 } else if (!(is_string($addr) && FUNC5::is_mail($addr))) {
00305                         FUNC5::trace($debug, 'invalid address value');
00306                 } else {
00307                         $ret = false;
00308                         $new = array();
00309                         if (count($this->Bcc) > 0) {
00310                                 $addr = strtolower($addr);
00311                                 foreach ($this->Bcc as $bcc) {
00312                                         if ($addr == strtolower($bcc)) $ret = true;
00313                                         else $new[] = $bcc;
00314                                 }
00315                         }
00316                         if ($ret) {
00317                                 $this->Bcc = $new;
00318                                 return $this->_result(array(0 => 'delete Bcc address'), true);
00319                         } else return FUNC5::trace($debug, 'Bcc address "'.$addr.'" not found', 1);
00320                 }
00321         }
00322 
00323         public function addheader($name = null, $value = null, $charset = null, $encoding = null, $debug = null) {
00324                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00325                 $err = array();
00326                 if (!is_string($name)) $err[] = 'invalid name type';
00327                 else {
00328                         $name = ucfirst(trim(FUNC5::str_clear($name)));
00329                         if (!(strlen($name) >= 2 && FUNC5::is_alpha($name, true, '-'))) $err[] = 'invalid name value';
00330                 }
00331                 if (!is_string($value)) $err[] = 'invalid content type';
00332                 else {
00333                         $value = trim(FUNC5::str_clear($value));
00334                         if ($value == '') $err[] = 'invalid content value';
00335                 }
00336                 if ($charset != null) {
00337                         if (!is_string($charset)) $err[] = 'invalid charset type';
00338                         else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
00339                 }
00340                 if ($encoding != null) {
00341                         if (!is_string($encoding)) $err[] = 'invalid encoding type';
00342                         else {
00343                                 $encoding = strtolower($encoding);
00344                                 if (!isset(MIME5::$hencarr[$encoding])) $err[] = 'invalid encoding value';
00345                         }
00346                 }
00347                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00348                 else {
00349                         $ver = strtolower($name);
00350                         $err = false;
00351                         if ($ver == 'to') $err = 'can not set "To", for this, use function "AddTo()"';
00352                         else if ($ver == 'cc') $err = 'can not set "Cc", for this, use function "AddCc()"';
00353                         else if ($ver == 'bcc') $err = 'can not set "Bcc", for this, use function "AddBcc()"';
00354                         else if ($ver == 'from') $err = 'can not set "From", for this, use function "From()"';
00355                         else if ($ver == 'subject') $err = 'can not set "Subject", for this, use function "Subject()"';
00356                         else if ($ver == 'x-priority') $err = 'can not set "X-Priority", for this, use function "Priority()"';
00357                         else if ($ver == 'x-msmail-priority') $err = 'can not set "X-MSMail-Priority", for this, use function "Priority()"';
00358                         else if ($ver == 'x-mimeole') $err = 'can not set "X-MimeOLE", for this, use function "Priority()"';
00359                         else if ($ver == 'date') $err = 'can not set "Date", this value is automaticaly set';
00360                         else if ($ver == 'content-type') $err = 'can not set "Content-Type", this value is automaticaly set';
00361                         else if ($ver == 'content-transfer-encoding') $err = 'can not set "Content-Transfer-Encoding", this value is automaticaly set';
00362                         else if ($ver == 'content-disposition') $err = 'can not set "Content-Disposition", this value is automaticaly set';
00363                         else if ($ver == 'mime-version') $err = 'can not set "Mime-Version", this value is automaticaly set';
00364                         else if ($ver == 'x-mailer') $err = 'can not set "X-Mailer", this value is automaticaly set';
00365                         else if ($ver == 'message-id') $err = 'can not set "Message-ID", this value is automaticaly set';
00366                         if ($err) FUNC5::trace($debug, $err);
00367                         else {
00368                                 $this->Header[] = array('name' => $name, 'value' => $value, 'charset' => $charset, 'encoding' => $encoding);
00369                                 return $this->_result(array(0 => 'add header'), true);
00370                         }
00371                 }
00372         }
00373 
00374         public function delheader($name = null, $debug = null) {
00375                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00376                 if ($name == null) {
00377                         $this->Header = array();
00378                         return $this->_result(array(0 => 'delete all headers'), true);
00379                 } else if (!(is_string($name) && strlen($name) >= 2 && FUNC5::is_alpha($name, true, '-'))) {
00380                         FUNC5::trace($debug, 'invalid name value');
00381                 } else {
00382                         $ret = false;
00383                         $new = array();
00384                         if (count($this->Header) > 0) {
00385                                 $name = strtolower($name);
00386                                 foreach ($this->Header as $header) {
00387                                         if ($name == strtolower($header['name'])) $ret = true;
00388                                         else $new[] = $header;
00389                                 }
00390                         }
00391                         if ($ret) {
00392                                 $this->Header = $new;
00393                                 return $this->_result(array(0 => 'delete header'), true);
00394                         } else return FUNC5::trace($debug, 'header not found', 1);
00395                 }
00396         }
00397 
00398         public function subject($content = null, $charset = null, $encoding = null, $debug = null) {
00399                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00400                 $err = array();
00401                 if (!is_string($content)) $err[] = 'invalid content type';
00402                 else {
00403                         $content = trim(FUNC5::str_clear($content));
00404                         if ($content == '') $err[] = 'invalid content value';
00405                 }
00406                 if ($charset != null) {
00407                         if (!is_string($charset)) $err[] = 'invalid charset type';
00408                         else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
00409                 }
00410                 if ($encoding != null) {
00411                         if (!is_string($encoding)) $err[] = 'invalid encoding type';
00412                         else {
00413                                 $encoding = strtolower($encoding);
00414                                 if (!isset(MIME5::$hencarr[$encoding])) $err[] = 'invalid encoding value';
00415                         }
00416                 }
00417                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00418                 else {
00419                         $this->Subject = array('content' => $content, 'charset' => $charset, 'encoding' => $encoding);
00420                         return $this->_result(array(0 => 'set subject'), true);
00421                 }
00422         }
00423 
00424         public function text($content = null, $charset = null, $encoding = null, $debug = null) {
00425                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00426                 $err = array();
00427                 if (!(is_string($content) && $content != '')) $err[] = 'invalid content type';
00428                 if ($charset != null) {
00429                         if (!is_string($charset)) $err[] = 'invalid charset type';
00430                         else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
00431                 }
00432                 if ($encoding != null) {
00433                         if (!is_string($encoding)) $err[] = 'invalid encoding type';
00434                         else {
00435                                 $encoding = strtolower($encoding);
00436                                 if (!isset(MIME5::$mencarr[$encoding])) $err[] = 'invalid encoding value';
00437                         }
00438                 }
00439                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00440                 else {
00441                         $this->Text = array('content' => $content, 'charset' => $charset, 'encoding' => $encoding);
00442                         return $this->_result(array(0 => 'set text version'), true);
00443                 }
00444         }
00445 
00446         public function html($content = null, $charset = null, $encoding = null, $debug = null) {
00447                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00448                 $err = array();
00449                 if (!(is_string($content) && $content != '')) $err[] = 'invalid content type';
00450                 if ($charset != null) {
00451                         if (!is_string($charset)) $err[] = 'invalid charset type';
00452                         else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
00453                 }
00454                 if ($encoding != null) {
00455                         if (!is_string($encoding)) $err[] = 'invalid encoding type';
00456                         else {
00457                                 $encoding = strtolower($encoding);
00458                                 if (!isset(MIME5::$mencarr[$encoding])) $err[] = 'invalid encoding value';
00459                         }
00460                 }
00461                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00462                 else {
00463                         $this->Html = array('content' => $content, 'charset' => $charset, 'encoding' => $encoding);
00464                         return $this->_result(array(0 => 'set html version'), true);
00465                 }
00466         }
00467 
00468         public function attach($content = null, $type = null, $name = null, $charset = null, $encoding = null, $disposition = null, $id = null, $debug = null) {
00469                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00470                 $err = array();
00471                 if (!(is_string($content) && $content != '')) $err[] = 'invalid content type';
00472                 if ($type != null) {
00473                         if (!is_string($type)) $err[] = 'invalid type value';
00474                         else {
00475                                 $type = trim(FUNC5::str_clear($type));
00476                                 if (strlen($type) < 4) $err[] = 'invalid type value';
00477                         }
00478                 }
00479                 if ($name != null) {
00480                         if (!is_string($name)) $err[] = 'invalid name type';
00481                         else {
00482                                 $name = trim(FUNC5::str_clear($name));
00483                                 if ($name == '') $err[] = 'invalid name value';
00484                         }
00485                 }
00486                 if ($charset != null) {
00487                         if (!is_string($charset)) $err[] = 'invalid charset type';
00488                         else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
00489                 }
00490                 if ($encoding == null) $encoding = 'base64';
00491                 else if (is_string($encoding)) {
00492                         $encoding = strtolower($encoding);
00493                         if (!isset(MIME5::$mencarr[$encoding])) $err[] = 'invalid encoding value';
00494                 } else $err[] = 'invalid encoding type';
00495                 if ($disposition == null) $disposition = 'attachment';
00496                 else if (is_string($disposition)) {
00497                         $disposition = strtolower(FUNC5::str_clear($disposition));
00498                         if (!($disposition == 'inline' || $disposition == 'attachment')) $err[] = 'invalid disposition value';
00499                 } else $err[] = 'invalid disposition type';
00500                 if ($id != null) {
00501                         if (!is_string($id)) $err[] = 'invalid id type';
00502                         else {
00503                                 $id = FUNC5::str_clear($id, array(' '));
00504                                 if ($id == '') $err[] = 'invalid id value';
00505                         }
00506                 }
00507                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00508                 else {
00509                         $this->Attach[] = array('content' => $content, 'type' => $type, 'name' => $name, 'charset' => $charset, 'encoding' => $encoding, 'disposition' => $disposition, 'id' => $id);
00510                         return $this->_result(array(0 => 'add attachment'), true);
00511                 }
00512         }
00513 
00514         public function delattach($name = null, $debug = null) {
00515                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00516                 if ($name == null) {
00517                         $this->Attach = array();
00518                         return $this->_result(array(0 => 'delete all attachments'), true);
00519                 } else if (!(is_string($name) && strlen($name) > 1)) {
00520                         FUNC5::trace($debug, 'invalid name value');
00521                 } else {
00522                         $ret = false;
00523                         $new = array();
00524                         if (count($this->Attach) > 0) {
00525                                 $name = strtolower($name);
00526                                 foreach ($this->Attach as $att) {
00527                                         if ($name == strtolower($att['name'])) $ret = true;
00528                                         else $new[] = $att;
00529                                 }
00530                         }
00531                         if ($ret) {
00532                                 $this->Attach = $new;
00533                                 return $this->_result(array(0 => 'delete attachment'), true);
00534                         } else return FUNC5::trace($debug, 'attachment not found', 1);
00535                 }
00536         }
00537 
00538         public function connect($host = null, $port = null, $user = null, $pass = null, $vssl = null, $tout = null, $name = null, $context = null, $auth = null, $debug = null) {
00539                 global $_RESULT;
00540                 $_RESULT = array();
00541                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00542                 if ($host == null) $host = $this->Host;
00543                 if ($port == null) $port = $this->Port;
00544                 if ($user == null) $user = $this->User;
00545                 if ($pass == null) $pass = $this->Pass;
00546                 if ($vssl == null) $vssl = $this->Vssl;
00547                 if ($tout == null) $tout = $this->Tout;
00548                 if ($name == null) $name = $this->Name;
00549                 if ($context == null) $context = $this->Context;
00550                 if ($auth == null) $auth = $this->Auth;
00551                 if ($ret = SMTP5::connect($host, $port, $user, $pass, $vssl, $tout, $name, $context, $auth, $debug)) $this->_conns[] = $ret;
00552                 return $this->_result($_RESULT, $ret);
00553         }
00554 
00555         public function disconnect($resc = null, $debug = null) {
00556                 global $_RESULT;
00557                 $_RESULT = array();
00558                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00559                 if ($resc != null) {
00560                         if (count($this->_conns) > 0) {
00561                                 $new = array();
00562                                 foreach ($this->_conns as $cres) {
00563                                         if ($cres != $resc) $new[] = $cres;
00564                                 }
00565                                 $this->_conns = $new;
00566                         }
00567                         $disc = SMTP5::disconnect($resc, $debug);
00568                         return $this->_result($_RESULT, $disc);
00569                 } else {
00570                         $rarr = array();
00571                         $disc = true;
00572                         if (count($this->_conns) > 0) {
00573                                 foreach ($this->_conns as $cres) {
00574                                         if (!SMTP5::disconnect($cres, $debug)) $disc = false;
00575                                         $rarr[] = $_RESULT;
00576                                 }
00577                         }
00578                         return $this->_result($rarr, $disc);
00579                 }
00580         }
00581 
00582         public function send($resc = null, $debug = null) {
00583                 global $_RESULT;
00584                 $_RESULT = $err = array();
00585                 if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
00586                 if (is_resource($resc)) $delivery = 'relay';
00587                 else {
00588                         if ($resc == null) $resc = 'local';
00589                         if (!is_string($resc)) $err[] = 'invalid connection type';
00590                         else {
00591                                 $resc = strtolower(trim($resc));
00592                                 if ($resc == 'local' || $resc == 'client' || $resc == 'sendmail' || $resc == 'qmail') $delivery = $resc;
00593                                 else $err[] = 'invalid connection value';
00594                         }
00595                 }
00596                 if (count($this->To) == 0) $err[] = 'to mail address is not set';
00597                 if (!isset($this->Subject['content'])) $err[] = 'mail subject is not set';
00598                 if (!(isset($this->Text['content']) || isset($this->Html['content']))) $err[] = 'mail message is not set';
00599                 if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
00600                 else {
00601                         $header['local'] = $header['client'] = array();
00602                         $body = '';
00603                         $from = null;
00604                         if (isset($this->From['address']) && is_string($this->From['address'])) {
00605                                 $from = $this->From['address'];
00606                                 $hv = 'From: ';
00607                                 if (isset($this->From['name']) && trim($this->From['name']) != '') {
00608                                         $hn = MIME5::encode_header($this->From['name'], 
00609                                                 isset($this->From['charset']) ? $this->From['charset'] : null, 
00610                                                 isset($this->From['encoding']) ? $this->From['encoding'] : null, 
00611                                                 null, null, $debug);
00612                                         if ($hn == $this->From['name']) $hn = '"'.str_replace('"', '\\"', $this->From['name']).'"';
00613                                         $hv .= $hn.' <'.$this->From['address'].'>';
00614                                 } else $hv .= $this->From['address'];
00615                                 $header['local'][] = $hv;
00616                                 $header['client'][] = $hv;
00617                         }
00618                         $addrs = $arr = array();
00619                         foreach ($this->To as $to) {
00620                                 if (isset($to['address']) && FUNC5::is_mail($to['address'], false, $debug)) {
00621                                         $addrs[] = $to['address'];
00622                                         if (isset($to['name']) && trim($to['name']) != '') {
00623                                                 $hn = MIME5::encode_header($to['name'], 
00624                                                         isset($to['charset']) ? $to['charset'] : null, 
00625                                                         isset($to['encoding']) ? $to['encoding'] : null, 
00626                                                         null, null, $debug);
00627                                                 if ($hn == $to['name']) $hn = '"'.str_replace('"', '\\"', $to['name']).'"';
00628                                                 $arr[] = $hn.' <'.$to['address'].'>';
00629                                         } else $arr[] = $to['address'];
00630                                 }
00631                         }
00632                         if (count($arr) > 0) {
00633                                 $to = implode(', ', $arr);
00634                                 $header['client'][] = 'To: '.implode(', '.MIME5::LE."\t", $arr);
00635                         } else FUNC5::trace($debug, 'to mail address is not set');
00636                         if (count($this->Cc) > 0) {
00637                                 $arr = array();
00638                                 foreach ($this->Cc as $cc) {
00639                                         if (isset($cc['address']) && FUNC5::is_mail($cc['address'], false, $debug)) {
00640                                                 $addrs[] = $cc['address'];
00641                                                 if (isset($cc['name']) && trim($cc['name']) != '') {
00642                                                         $hn = MIME5::encode_header($cc['name'], 
00643                                                                 isset($cc['charset']) ? $cc['charset'] : null, 
00644                                                                 isset($cc['encoding']) ? $cc['encoding'] : null, 
00645                                                                 null, null, $debug);
00646                                                         if ($hn == $cc['name']) $hn = '"'.str_replace('"', '\\"', $cc['name']).'"';
00647                                                         $arr[] = $hn.' <'.$cc['address'].'>';
00648                                                 } else $arr[] = $cc['address'];
00649                                         }
00650                                 }
00651                                 if (count($arr) > 0) {
00652                                         $header['local'][] = 'Cc: '.implode(', ', $arr);
00653                                         $header['client'][] = 'Cc: '.implode(', '.MIME5::LE."\t", $arr);
00654                                 }
00655                         }
00656                         $hbcc = '';
00657                         if (count($this->Bcc) > 0) {
00658                                 $arr = array();
00659                                 foreach ($this->Bcc as $bcc) {
00660                                         if (FUNC5::is_mail($bcc, false, $debug)) {
00661                                                 $arr[] = $bcc;
00662                                                 $addrs[] = $bcc;
00663                                         }
00664                                 }
00665                                 if (count($arr) > 0) {
00666                                         $header['local'][] = 'Bcc: '.implode(', ', $arr);
00667                                         $hbcc = MIME5::LE.'Bcc: '.implode(', ', $arr);
00668                                 }
00669                         }
00670                         $hn = MIME5::encode_header($this->Subject['content'], 
00671                                 isset($this->Subject['charset']) ? $this->Subject['charset'] : null, 
00672                                 isset($this->Subject['encoding']) ? $this->Subject['encoding'] : null, 
00673                                 null, null, $debug);
00674                         $subject = $hn;
00675                         $header['client'][] = 'Subject: '.$hn;
00676                         if (is_int($this->Priority) || is_string($this->Priority)) {
00677                                 $arr = false;
00678                                 if ($this->Priority == 1 || $this->Priority == 'high') $arr = array(1, 'high');
00679                                 else if ($this->Priority == 3 || $this->Priority == 'normal') $arr = array(3, 'normal');
00680                                 else if ($this->Priority == 5 || $this->Priority == 'low') $arr = array(5, 'low');
00681                                 if ($arr) {
00682                                         $header['local'][] = 'X-Priority: '.$arr[0];
00683                                         $header['local'][] = 'X-MSMail-Priority: '.$arr[1];
00684                                         $header['local'][] = 'X-MimeOLE: Produced By XPertMailer v.4 MIME Class'; // << required by SpamAssassin in conjunction with "X-MSMail-Priority"
00685                                         $header['client'][] = 'X-Priority: '.$arr[0];
00686                                         $header['client'][] = 'X-MSMail-Priority: '.$arr[1];
00687                                         $header['client'][] = 'X-MimeOLE: Produced By XPertMailer v.4 MIME Class';
00688                                 }
00689                         }
00690                         $header['client'][] = 'Message-ID: <'.MIME5::unique().'@xpertmailer.com>';
00691                         if (count($this->Header) > 0) {
00692                                 foreach ($this->Header as $harr) {
00693                                         if (isset($harr['name'], $harr['value']) && strlen($harr['name']) >= 2 && FUNC5::is_alpha($harr['name'], true, '-')) {
00694                                                 $hn = MIME5::encode_header($harr['value'], 
00695                                                         isset($harr['charset']) ? $harr['charset'] : null, 
00696                                                         isset($harr['encoding']) ? $harr['encoding'] : null, 
00697                                                         null, null, $debug);
00698                                                 $header['local'][] = ucfirst($harr['name']).': '.$hn;
00699                                                 $header['client'][] = ucfirst($harr['name']).': '.$hn;
00700                                         }
00701                                 }
00702                         }
00703                         $text = $html = $att = null;
00704                         if (isset($this->Text['content'])) {
00705                                 $text = MIME5::message($this->Text['content'], 'text/plain', null, 
00706                                         isset($this->Text['charset']) ? $this->Text['charset'] : null, 
00707                                         isset($this->Text['encoding']) ? $this->Text['encoding'] : null, 
00708                                         null, null, null, null, $debug);
00709                         }
00710                         if (isset($this->Html['content'])) {
00711                                 $html = MIME5::message($this->Html['content'], 'text/html', null, 
00712                                         isset($this->Html['charset']) ? $this->Html['charset'] : null, 
00713                                         isset($this->Html['encoding']) ? $this->Html['encoding'] : null, 
00714                                         null, null, null, null, $debug);
00715                         }
00716                         if (count($this->Attach) > 0) {
00717                                 $att = array();
00718                                 foreach ($this->Attach as $attach) {
00719                                         if (isset($attach['content'])) {
00720                                                 $att[] = MIME5::message($attach['content'], 
00721                                                         isset($attach['type']) ? $attach['type'] : null, 
00722                                                         isset($attach['name']) ? $attach['name'] : null, 
00723                                                         isset($attach['charset']) ? $attach['charset'] : null, 
00724                                                         isset($attach['encoding']) ? $attach['encoding'] : null, 
00725                                                         isset($attach['disposition']) ? $attach['disposition'] : null, 
00726                                                         isset($attach['id']) ? $attach['id'] : null, 
00727                                                         null, null, $debug);
00728                                         }
00729                                 }
00730                                 if (count($att) == 0) $att = null;
00731                         }
00732                         $arr = MIME5::compose($text, $html, $att);
00733                         if ($delivery == 'relay') {
00734                                 $res = SMTP5::send($resc, $addrs, implode(MIME5::LE, $header['client']).MIME5::LE.$arr['header'].MIME5::LE.MIME5::LE.$arr['content'], (($this->Path != null) ? $this->Path : $from), $debug);
00735                                 return $this->_result($_RESULT, $res);
00736                         } else if ($delivery == 'local') {
00737                                 $rpath = (!FUNC5::is_win() && $this->Path != null) ? '-f '.$this->Path : null;
00738                                 $spath = ($this->Path != null) ? @ini_set('sendmail_from', $this->Path) : false;
00739                                 if (!FUNC5::is_win()) $arr['content'] = str_replace("\r\n", "\n", $arr['content']);
00740                                 $res = mail($to, $subject, $arr['content'], implode(MIME5::LE, $header['local']).MIME5::LE.$arr['header'], $rpath);
00741                                 if ($spath) @ini_restore('sendmail_from');
00742                                 return $this->_result(array(0 => 'send mail local'), $res);
00743                         } else if ($delivery == 'client') {
00744                                 $group = array();
00745                                 foreach ($addrs as $addr) {
00746                                         $exp = explode('@', $addr);
00747                                         $group[strtolower($exp[1])][] = $addr;
00748                                 }
00749                                 $ret = true;
00750                                 $reg = (count($group) == 1);
00751                                 foreach ($group as $domain => $arrs) {
00752                                         $con = SMTP5::mxconnect($domain, $this->Port, $this->Tout, $this->Name, $this->Context, $debug);
00753                                         if ($reg) $this->_result(array($domain => $_RESULT));
00754                                         if ($con) {
00755                                                 if (!SMTP5::send($con, $arrs, implode(MIME5::LE, $header['client']).MIME5::LE.$arr['header'].MIME5::LE.MIME5::LE.$arr['content'], (($this->Path != null) ? $this->Path : $from), $debug)) $ret = false;
00756                                                 if ($reg) $this->_result(array($domain => $_RESULT));
00757                                                 SMTP5::disconnect($con, $debug);
00758                                         } else $ret = false;
00759                                 }
00760                                 if (!$reg) $this->_result(array(0 => 'send mail client'));
00761                                 return $ret;
00762                         } else if ($delivery == 'sendmail' || $delivery == 'qmail') {
00763                                 $ret = false;
00764                                 $comm = (($delivery == 'sendmail') ? $this->SendMail : $this->QMail).' -oi'.(($this->Path != null) ? ' -f '.$this->Path : '').' -t';
00765                                 if ($con = popen($comm, 'w')) {
00766                                         if (fputs($con, implode(MIME5::LE, $header['client']).$hbcc.MIME5::LE.$arr['header'].MIME5::LE.MIME5::LE.$arr['content'])) {
00767                                                 $res = pclose($con) >> 8 & 0xFF;
00768                                                 if ($res == 0) {
00769                                                         $ret = true;
00770                                                         $this->_result(array(0 => 'send mail using "'.ucfirst($delivery).'" program'));
00771                                                 } else $this->_result(array(0 => $res));
00772                                         } else $this->_result(array(0 => 'can not write'));
00773                                 } else $this->_result(array(0 => 'can not write line command'));
00774                                 return $ret;
00775                         }
00776                 }
00777         }
00778 
00779 }
00780 
00781 ?>
 All Data Structures Files Functions Variables Enumerations