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