PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/db/library/xpertmailer/PHP5/MAIL5.php

https://github.com/tjgillies/dbscript
PHP | 775 lines | 721 code | 35 blank | 19 comment | 494 complexity | 8264af6be97ba2633aa04deb58b0d6f7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  3. * *
  4. * XPertMailer is a PHP Mail Class that can send and read messages in MIME format. *
  5. * This file is part of the XPertMailer package (http://xpertmailer.sourceforge.net/) *
  6. * Copyright (C) 2007 Tanase Laurentiu Iulian *
  7. * *
  8. * This library is free software; you can redistribute it and/or modify it under the *
  9. * terms of the GNU Lesser General Public License as published by the Free Software *
  10. * Foundation; either version 2.1 of the License, or (at your option) any later version. *
  11. * *
  12. * This library is distributed in the hope that it will be useful, but WITHOUT ANY *
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
  14. * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU Lesser General Public License along with *
  17. * this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
  18. * Fifth Floor, Boston, MA 02110-1301, USA *
  19. * *
  20. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. if (!class_exists('SMTP5')) require_once library_path().'xpertmailer'.DIRECTORY_SEPARATOR . 'SMTP5.php';
  22. class MAIL5 {
  23. public $From = null;
  24. public $To = array();
  25. public $Cc = array();
  26. public $Bcc = array();
  27. public $Subject = null;
  28. public $Text = null;
  29. public $Html = null;
  30. public $Header = array();
  31. public $Attach = array();
  32. public $Host = null;
  33. public $Port = null;
  34. public $User = null;
  35. public $Pass = null;
  36. public $Vssl = null;
  37. public $Tout = null;
  38. public $Name = null;
  39. public $Path = null;
  40. public $Priority = null;
  41. public $Context = null;
  42. public $SendMail = '/usr/sbin/sendmail';
  43. public $QMail = '/var/qmail/bin/sendmail';
  44. private $_conns = array();
  45. public $History = array();
  46. public $Result = null;
  47. public function __construct() {
  48. $this->_result(array(0 => 'initialize class'));
  49. }
  50. private function _result($data = array(), $ret = null) {
  51. $this->History[][strval(microtime(true))] = $data;
  52. $this->Result = $data;
  53. return $ret;
  54. }
  55. public function context($arr = null, $debug = null) {
  56. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  57. if (!is_array($arr)) FUNC5::trace($debug, 'invalid context type');
  58. else if (!is_resource($res = stream_context_create($arr))) FUNC5::trace($debug, 'invalid context value');
  59. else {
  60. $this->Context = $res;
  61. return $this->_result(array(0 => 'set context connection'), true);
  62. }
  63. }
  64. public function name($host = null, $debug = null) {
  65. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  66. if (!is_string($host)) FUNC5::trace($debug, 'invalid hostname type');
  67. else {
  68. $host = strtolower(trim($host));
  69. if (!($host != '' && ($host == 'localhost' || FUNC5::is_ipv4($host) || FUNC5::is_hostname($host, true, $debug)))) FUNC5::trace($debug, 'invalid hostname value');
  70. $this->Name = $host;
  71. return $this->_result(array(0 => 'set HELO/EHLO hostname'), true);
  72. }
  73. }
  74. public function path($addr = null, $debug = null) {
  75. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  76. if (!is_string($addr)) FUNC5::trace($debug, 'invalid address type');
  77. else {
  78. if (!($addr != '' && FUNC5::is_mail($addr))) FUNC5::trace($debug, 'invalid address value');
  79. $this->Path = $addr;
  80. return $this->_result(array(0 => 'set return-path address'), true);
  81. }
  82. }
  83. public function priority($level = null, $debug = null) {
  84. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  85. if ($level == null) {
  86. $this->Priority = null;
  87. return $this->_result(array(0 => 'unset priority'), true);
  88. } else if (is_int($level) || is_string($level)) {
  89. if (is_string($level)) $level = strtolower(trim(FUNC5::str_clear($level)));
  90. if ($level == 1 || $level == 3 || $level == 5 || $level == 'high' || $level == 'normal' || $level == 'low') {
  91. $this->Priority = $level;
  92. return $this->_result(array(0 => 'set priority'), true);
  93. } else FUNC5::trace($debug, 'invalid level value');
  94. } else FUNC5::trace($debug, 'invalid level type');
  95. }
  96. public function from($addr = null, $name = null, $charset = null, $encoding = null, $debug = null) {
  97. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  98. $err = array();
  99. if (!is_string($addr)) $err[] = 'invalid address type';
  100. else if (!FUNC5::is_mail($addr)) $err[] = 'invalid address value';
  101. if ($name != null) {
  102. if (!is_string($name)) $err[] = 'invalid name type';
  103. else {
  104. $name = trim(FUNC5::str_clear($name));
  105. if ($name == '') $err[] = 'invalid name value';
  106. }
  107. }
  108. if ($charset != null) {
  109. if (!is_string($charset)) $err[] = 'invalid charset type';
  110. else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
  111. }
  112. if ($encoding != null) {
  113. if (!is_string($encoding)) $err[] = 'invalid encoding type';
  114. else {
  115. $encoding = strtolower($encoding);
  116. if (!isset(MIME5::$hencarr[$encoding])) $err[] = 'invalid encoding value';
  117. }
  118. }
  119. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  120. else {
  121. $this->From = array('address' => $addr, 'name' => $name, 'charset' => $charset, 'encoding' => $encoding);
  122. return $this->_result(array(0 => 'set from address'), true);
  123. }
  124. }
  125. public function addto($addr = null, $name = null, $charset = null, $encoding = null, $debug = null) {
  126. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  127. $err = array();
  128. if (!is_string($addr)) $err[] = 'invalid address type';
  129. else if (!FUNC5::is_mail($addr)) $err[] = 'invalid address value';
  130. if ($name != null) {
  131. if (!is_string($name)) $err[] = 'invalid name type';
  132. else {
  133. $name = trim(FUNC5::str_clear($name));
  134. if ($name == '') $err[] = 'invalid name value';
  135. }
  136. }
  137. if ($charset != null) {
  138. if (!is_string($charset)) $err[] = 'invalid charset type';
  139. else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
  140. }
  141. if ($encoding != null) {
  142. if (!is_string($encoding)) $err[] = 'invalid encoding type';
  143. else {
  144. $encoding = strtolower($encoding);
  145. if (!isset(MIME5::$hencarr[$encoding])) $err[] = 'invalid encoding value';
  146. }
  147. }
  148. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  149. else {
  150. $find = false;
  151. if (count($this->To) > 0) {
  152. $ladr = strtolower($addr);
  153. foreach ($this->To as $to) {
  154. if ($ladr == strtolower($to['address'])) {
  155. FUNC5::trace($debug, 'duplicate To address "'.$addr.'"', 1);
  156. $find = true;
  157. }
  158. }
  159. }
  160. if ($find) return false;
  161. else {
  162. $this->To[] = array('address' => $addr, 'name' => $name, 'charset' => $charset, 'encoding' => $encoding);
  163. return $this->_result(array(0 => 'add To address'), true);
  164. }
  165. }
  166. }
  167. public function delto($addr = null, $debug = null) {
  168. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  169. if ($addr == null) {
  170. $this->To = array();
  171. return $this->_result(array(0 => 'delete all To addresses'), true);
  172. } else if (!(is_string($addr) && FUNC5::is_mail($addr))) {
  173. FUNC5::trace($debug, 'invalid address value');
  174. } else {
  175. $ret = false;
  176. $new = array();
  177. if (count($this->To) > 0) {
  178. $addr = strtolower($addr);
  179. foreach ($this->To as $to) {
  180. if ($addr == strtolower($to['address'])) $ret = true;
  181. else $new[] = $to;
  182. }
  183. }
  184. if ($ret) {
  185. $this->To = $new;
  186. return $this->_result(array(0 => 'delete To address'), true);
  187. } else return FUNC5::trace($debug, 'To address "'.$addr.'" not found', 1);
  188. }
  189. }
  190. public function addcc($addr = null, $name = null, $charset = null, $encoding = null, $debug = null) {
  191. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  192. $err = array();
  193. if (!is_string($addr)) $err[] = 'invalid address type';
  194. else if (!FUNC5::is_mail($addr)) $err[] = 'invalid address value';
  195. if ($name != null) {
  196. if (!is_string($name)) $err[] = 'invalid name type';
  197. else {
  198. $name = trim(FUNC5::str_clear($name));
  199. if ($name == '') $err[] = 'invalid name value';
  200. }
  201. }
  202. if ($charset != null) {
  203. if (!is_string($charset)) $err[] = 'invalid charset type';
  204. else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
  205. }
  206. if ($encoding != null) {
  207. if (!is_string($encoding)) $err[] = 'invalid encoding type';
  208. else {
  209. $encoding = strtolower($encoding);
  210. if (!isset(MIME5::$hencarr[$encoding])) $err[] = 'invalid encoding value';
  211. }
  212. }
  213. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  214. else {
  215. $find = false;
  216. if (count($this->Cc) > 0) {
  217. $ladr = strtolower($addr);
  218. foreach ($this->Cc as $cc) {
  219. if ($ladr == strtolower($cc['address'])) {
  220. FUNC5::trace($debug, 'duplicate Cc address "'.$addr.'"', 1);
  221. $find = true;
  222. }
  223. }
  224. }
  225. if ($find) return false;
  226. else {
  227. $this->Cc[] = array('address' => $addr, 'name' => $name, 'charset' => $charset, 'encoding' => $encoding);
  228. return $this->_result(array(0 => 'add Cc address'), true);
  229. }
  230. }
  231. }
  232. public function delcc($addr = null, $debug = null) {
  233. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  234. if ($addr == null) {
  235. $this->Cc = array();
  236. return $this->_result(array(0 => 'delete all Cc addresses'), true);
  237. } else if (!(is_string($addr) && FUNC5::is_mail($addr))) {
  238. FUNC5::trace($debug, 'invalid address value');
  239. } else {
  240. $ret = false;
  241. $new = array();
  242. if (count($this->Cc) > 0) {
  243. $addr = strtolower($addr);
  244. foreach ($this->Cc as $cc) {
  245. if ($addr == strtolower($cc['address'])) $ret = true;
  246. else $new[] = $cc;
  247. }
  248. }
  249. if ($ret) {
  250. $this->Cc = $new;
  251. return $this->_result(array(0 => 'delete Cc address'), true);
  252. } else return FUNC5::trace($debug, 'Cc address "'.$addr.'" not found', 1);
  253. }
  254. }
  255. public function addbcc($addr = null, $debug = null) {
  256. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  257. if (!is_string($addr)) FUNC5::trace($debug, 'invalid address type');
  258. else if (!FUNC5::is_mail($addr)) FUNC5::trace($debug, 'invalid address value');
  259. $find = false;
  260. if (count($this->Bcc) > 0) {
  261. $ladr = strtolower($addr);
  262. foreach ($this->Bcc as $bcc) {
  263. if ($ladr == strtolower($bcc)) {
  264. FUNC5::trace($debug, 'duplicate Bcc address "'.$addr.'"', 1);
  265. $find = true;
  266. }
  267. }
  268. }
  269. if ($find) return false;
  270. else {
  271. $this->Bcc[] = $addr;
  272. return $this->_result(array(0 => 'add Bcc address'), true);
  273. }
  274. }
  275. public function delbcc($addr = null, $debug = null) {
  276. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  277. if ($addr == null) {
  278. $this->Bcc = array();
  279. return $this->_result(array(0 => 'delete all Bcc addresses'), true);
  280. } else if (!(is_string($addr) && FUNC5::is_mail($addr))) {
  281. FUNC5::trace($debug, 'invalid address value');
  282. } else {
  283. $ret = false;
  284. $new = array();
  285. if (count($this->Bcc) > 0) {
  286. $addr = strtolower($addr);
  287. foreach ($this->Bcc as $bcc) {
  288. if ($addr == strtolower($bcc)) $ret = true;
  289. else $new[] = $bcc;
  290. }
  291. }
  292. if ($ret) {
  293. $this->Bcc = $new;
  294. return $this->_result(array(0 => 'delete Bcc address'), true);
  295. } else return FUNC5::trace($debug, 'Bcc address "'.$addr.'" not found', 1);
  296. }
  297. }
  298. public function addheader($name = null, $value = null, $charset = null, $encoding = null, $debug = null) {
  299. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  300. $err = array();
  301. if (!is_string($name)) $err[] = 'invalid name type';
  302. else {
  303. $name = ucfirst(trim(FUNC5::str_clear($name)));
  304. if (!(strlen($name) >= 2 && FUNC5::is_alpha($name, true, '-'))) $err[] = 'invalid name value';
  305. }
  306. if (!is_string($value)) $err[] = 'invalid content type';
  307. else {
  308. $value = trim(FUNC5::str_clear($value));
  309. if ($value == '') $err[] = 'invalid content value';
  310. }
  311. if ($charset != null) {
  312. if (!is_string($charset)) $err[] = 'invalid charset type';
  313. else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
  314. }
  315. if ($encoding != null) {
  316. if (!is_string($encoding)) $err[] = 'invalid encoding type';
  317. else {
  318. $encoding = strtolower($encoding);
  319. if (!isset(MIME5::$hencarr[$encoding])) $err[] = 'invalid encoding value';
  320. }
  321. }
  322. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  323. else {
  324. $ver = strtolower($name);
  325. $err = false;
  326. if ($ver == 'to') $err = 'can not set "To", for this, use function "AddTo()"';
  327. else if ($ver == 'cc') $err = 'can not set "Cc", for this, use function "AddCc()"';
  328. else if ($ver == 'bcc') $err = 'can not set "Bcc", for this, use function "AddBcc()"';
  329. else if ($ver == 'from') $err = 'can not set "From", for this, use function "From()"';
  330. else if ($ver == 'subject') $err = 'can not set "Subject", for this, use function "Subject()"';
  331. else if ($ver == 'x-priority') $err = 'can not set "X-Priority", for this, use function "Priority()"';
  332. else if ($ver == 'x-msmail-priority') $err = 'can not set "X-MSMail-Priority", for this, use function "Priority()"';
  333. else if ($ver == 'date') $err = 'can not set "Date", this value is automaticaly set';
  334. else if ($ver == 'content-type') $err = 'can not set "Content-Type", this value is automaticaly set';
  335. else if ($ver == 'content-transfer-encoding') $err = 'can not set "Content-Transfer-Encoding", this value is automaticaly set';
  336. else if ($ver == 'content-disposition') $err = 'can not set "Content-Disposition", this value is automaticaly set';
  337. else if ($ver == 'mime-version') $err = 'can not set "Mime-Version", this value is automaticaly set';
  338. else if ($ver == 'x-mailer') $err = 'can not set "X-Mailer", this value is automaticaly set';
  339. else if ($ver == 'message-id') $err = 'can not set "Message-Id", this value is automaticaly set';
  340. if ($err) FUNC5::trace($debug, $err);
  341. else {
  342. $this->Header[] = array('name' => $name, 'value' => $value, 'charset' => $charset, 'encoding' => $encoding);
  343. return $this->_result(array(0 => 'add header'), true);
  344. }
  345. }
  346. }
  347. public function delheader($name = null, $debug = null) {
  348. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  349. if ($name == null) {
  350. $this->Header = array();
  351. return $this->_result(array(0 => 'delete all headers'), true);
  352. } else if (!(is_string($name) && strlen($name) >= 2 && FUNC5::is_alpha($name, true, '-'))) {
  353. FUNC5::trace($debug, 'invalid name value');
  354. } else {
  355. $ret = false;
  356. $new = array();
  357. if (count($this->Header) > 0) {
  358. $name = strtolower($name);
  359. foreach ($this->Header as $header) {
  360. if ($name == strtolower($header['name'])) $ret = true;
  361. else $new[] = $header;
  362. }
  363. }
  364. if ($ret) {
  365. $this->Header = $new;
  366. return $this->_result(array(0 => 'delete header'), true);
  367. } else return FUNC5::trace($debug, 'header not found', 1);
  368. }
  369. }
  370. public function subject($content = null, $charset = null, $encoding = null, $debug = null) {
  371. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  372. $err = array();
  373. if (!is_string($content)) $err[] = 'invalid content type';
  374. else {
  375. $content = trim(FUNC5::str_clear($content));
  376. if ($content == '') $err[] = 'invalid content value';
  377. }
  378. if ($charset != null) {
  379. if (!is_string($charset)) $err[] = 'invalid charset type';
  380. else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
  381. }
  382. if ($encoding != null) {
  383. if (!is_string($encoding)) $err[] = 'invalid encoding type';
  384. else {
  385. $encoding = strtolower($encoding);
  386. if (!isset(MIME5::$hencarr[$encoding])) $err[] = 'invalid encoding value';
  387. }
  388. }
  389. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  390. else {
  391. $this->Subject = array('content' => $content, 'charset' => $charset, 'encoding' => $encoding);
  392. return $this->_result(array(0 => 'set subject'), true);
  393. }
  394. }
  395. public function text($content = null, $charset = null, $encoding = null, $debug = null) {
  396. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  397. $err = array();
  398. if (!(is_string($content) && $content != '')) $err[] = 'invalid content type';
  399. if ($charset != null) {
  400. if (!is_string($charset)) $err[] = 'invalid charset type';
  401. else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
  402. }
  403. if ($encoding != null) {
  404. if (!is_string($encoding)) $err[] = 'invalid encoding type';
  405. else {
  406. $encoding = strtolower($encoding);
  407. if (!isset(MIME5::$mencarr[$encoding])) $err[] = 'invalid encoding value';
  408. }
  409. }
  410. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  411. else {
  412. $this->Text = array('content' => $content, 'charset' => $charset, 'encoding' => $encoding);
  413. return $this->_result(array(0 => 'set text version'), true);
  414. }
  415. }
  416. public function html($content = null, $charset = null, $encoding = null, $debug = null) {
  417. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  418. $err = array();
  419. if (!(is_string($content) && $content != '')) $err[] = 'invalid content type';
  420. if ($charset != null) {
  421. if (!is_string($charset)) $err[] = 'invalid charset type';
  422. else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
  423. }
  424. if ($encoding != null) {
  425. if (!is_string($encoding)) $err[] = 'invalid encoding type';
  426. else {
  427. $encoding = strtolower($encoding);
  428. if (!isset(MIME5::$mencarr[$encoding])) $err[] = 'invalid encoding value';
  429. }
  430. }
  431. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  432. else {
  433. $this->Html = array('content' => $content, 'charset' => $charset, 'encoding' => $encoding);
  434. return $this->_result(array(0 => 'set html version'), true);
  435. }
  436. }
  437. public function attach($content = null, $type = null, $name = null, $charset = null, $encoding = null, $disposition = null, $id = null, $debug = null) {
  438. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  439. $err = array();
  440. if (!(is_string($content) && $content != '')) $err[] = 'invalid content type';
  441. if ($type != null) {
  442. if (!is_string($type)) $err[] = 'invalid type value';
  443. else {
  444. $type = trim(FUNC5::str_clear($type));
  445. if (strlen($type) < 4) $err[] = 'invalid type value';
  446. }
  447. }
  448. if ($name != null) {
  449. if (!is_string($name)) $err[] = 'invalid name type';
  450. else {
  451. $name = trim(FUNC5::str_clear($name));
  452. if ($name == '') $err[] = 'invalid name value';
  453. }
  454. }
  455. if ($charset != null) {
  456. if (!is_string($charset)) $err[] = 'invalid charset type';
  457. else if (!(strlen($charset) >= 2 && FUNC5::is_alpha($charset, true, '-'))) $err[] = 'invalid charset value';
  458. }
  459. if ($encoding == null) $encoding = 'base64';
  460. else if (is_string($encoding)) {
  461. $encoding = strtolower($encoding);
  462. if (!isset(MIME5::$mencarr[$encoding])) $err[] = 'invalid encoding value';
  463. } else $err[] = 'invalid encoding type';
  464. if ($disposition == null) $disposition = 'attachment';
  465. else if (is_string($disposition)) {
  466. $disposition = strtolower(FUNC5::str_clear($disposition));
  467. if (!($disposition == 'inline' || $disposition == 'attachment')) $err[] = 'invalid disposition value';
  468. } else $err[] = 'invalid disposition type';
  469. if ($id != null) {
  470. if (!is_string($id)) $err[] = 'invalid id type';
  471. else {
  472. $id = FUNC5::str_clear($id, array(' '));
  473. if ($id == '') $err[] = 'invalid id value';
  474. }
  475. }
  476. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  477. else {
  478. $this->Attach[] = array('content' => $content, 'type' => $type, 'name' => $name, 'charset' => $charset, 'encoding' => $encoding, 'disposition' => $disposition, 'id' => $id);
  479. return $this->_result(array(0 => 'add attachment'), true);
  480. }
  481. }
  482. public function delattach($name = null, $debug = null) {
  483. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  484. if ($name == null) {
  485. $this->Attach = array();
  486. return $this->_result(array(0 => 'delete all attachments'), true);
  487. } else if (!(is_string($name) && strlen($name) > 1)) {
  488. FUNC5::trace($debug, 'invalid name value');
  489. } else {
  490. $ret = false;
  491. $new = array();
  492. if (count($this->Attach) > 0) {
  493. $name = strtolower($name);
  494. foreach ($this->Attach as $att) {
  495. if ($name == strtolower($att['name'])) $ret = true;
  496. else $new[] = $att;
  497. }
  498. }
  499. if ($ret) {
  500. $this->Attach = $new;
  501. return $this->_result(array(0 => 'delete attachment'), true);
  502. } else return FUNC5::trace($debug, 'attachment not found', 1);
  503. }
  504. }
  505. public function connect($host = null, $port = null, $user = null, $pass = null, $vssl = null, $tout = null, $name = null, $context = null, $debug = null) {
  506. global $_RESULT;
  507. $_RESULT = array();
  508. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  509. if ($host == null) $host = $this->Host;
  510. if ($port == null) $port = $this->Port;
  511. if ($user == null) $user = $this->User;
  512. if ($pass == null) $pass = $this->Pass;
  513. if ($vssl == null) $vssl = $this->Vssl;
  514. if ($tout == null) $tout = $this->Tout;
  515. if ($name == null) $name = $this->Name;
  516. if ($context == null) $context = $this->Context;
  517. if ($ret = SMTP5::connect($host, $port, $user, $pass, $vssl, $tout, $name, $context, $debug)) $this->_conns[] = $ret;
  518. return $this->_result($_RESULT, $ret);
  519. }
  520. public function disconnect($resc = null, $debug = null) {
  521. global $_RESULT;
  522. $_RESULT = array();
  523. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  524. if ($resc != null) {
  525. if (count($this->_conns) > 0) {
  526. $new = array();
  527. foreach ($this->_conns as $cres) {
  528. if ($cres != $resc) $new[] = $cres;
  529. }
  530. $this->_conns = $new;
  531. }
  532. $disc = SMTP5::disconnect($resc, $debug);
  533. return $this->_result($_RESULT, $disc);
  534. } else {
  535. $rarr = array();
  536. $disc = true;
  537. if (count($this->_conns) > 0) {
  538. foreach ($this->_conns as $cres) {
  539. if (!SMTP5::disconnect($cres, $debug)) $disc = false;
  540. $rarr[] = $_RESULT;
  541. }
  542. }
  543. return $this->_result($rarr, $disc);
  544. }
  545. }
  546. public function send($resc = null, $debug = null) {
  547. global $_RESULT;
  548. $_RESULT = $err = array();
  549. if (!FUNC5::is_debug($debug)) $debug = debug_backtrace();
  550. if (is_resource($resc)) $delivery = 'relay';
  551. else {
  552. if ($resc == null) $delivery = 'local';
  553. if (!is_string($resc)) $err[] = 'invalid connection type';
  554. else {
  555. $resc = strtolower(trim($resc));
  556. if ($resc == 'local' || $resc == 'client' || $resc == 'sendmail' || $resc == 'qmail') $delivery = $resc;
  557. else $err[] = 'invalid connection value';
  558. }
  559. }
  560. if (count($this->To) == 0) $err[] = 'to mail address is not set';
  561. if (!isset($this->Subject['content'])) $err[] = 'mail subject is not set';
  562. if (!(isset($this->Text['content']) || isset($this->Html['content']))) $err[] = 'mail message is not set';
  563. if (count($err) > 0) FUNC5::trace($debug, implode(', ', $err));
  564. else {
  565. $header['local'] = $header['client'] = array();
  566. $body = '';
  567. $from = null;
  568. if (isset($this->From['address']) && is_string($this->From['address'])) {
  569. $from = $this->From['address'];
  570. $hv = 'From: ';
  571. if (isset($this->From['name'])) {
  572. $hn = MIME5::encode_header($this->From['name'],
  573. isset($this->From['charset']) ? $this->From['charset'] : null,
  574. isset($this->From['encoding']) ? $this->From['encoding'] : null,
  575. null, null, $debug);
  576. if ($hn == $this->From['name']) $hn = '"'.str_replace('"', '\\"', $this->From['name']).'"';
  577. $hv .= $hn.' <'.$this->From['address'].'>';
  578. } else $hv .= $this->From['address'];
  579. $header['local'][] = $hv;
  580. $header['client'][] = $hv;
  581. }
  582. $addrs = $arr = array();
  583. foreach ($this->To as $to) {
  584. if (isset($to['address']) && FUNC5::is_mail($to['address'], false, $debug)) {
  585. $addrs[] = $to['address'];
  586. if (isset($to['name'])) {
  587. $hn = MIME5::encode_header($to['name'],
  588. isset($to['charset']) ? $to['charset'] : null,
  589. isset($to['encoding']) ? $to['encoding'] : null,
  590. null, null, $debug);
  591. if ($hn == $to['name']) $hn = '"'.str_replace('"', '\\"', $to['name']).'"';
  592. $arr[] = $hn.' <'.$to['address'].'>';
  593. } else $arr[] = $to['address'];
  594. }
  595. }
  596. if (count($arr) > 0) {
  597. $to = implode(', ', $arr);
  598. $header['client'][] = 'To: '.implode(', '.MIME5::LE."\t", $arr);
  599. } else FUNC5::trace($debug, 'to mail address is not set');
  600. if (count($this->Cc) > 0) {
  601. $arr = array();
  602. foreach ($this->Cc as $cc) {
  603. if (isset($cc['address']) && FUNC5::is_mail($cc['address'], false, $debug)) {
  604. $addrs[] = $cc['address'];
  605. if (isset($cc['name'])) {
  606. $hn = MIME5::encode_header($cc['name'],
  607. isset($cc['charset']) ? $cc['charset'] : null,
  608. isset($cc['encoding']) ? $cc['encoding'] : null,
  609. null, null, $debug);
  610. if ($hn == $cc['name']) $hn = '"'.str_replace('"', '\\"', $cc['name']).'"';
  611. $arr[] = $hn.' <'.$cc['address'].'>';
  612. } else $arr[] = $cc['address'];
  613. }
  614. }
  615. if (count($arr) > 0) {
  616. $header['local'][] = 'Cc: '.implode(', ', $arr);
  617. $header['client'][] = 'Cc: '.implode(', '.MIME5::LE."\t", $arr);
  618. }
  619. }
  620. $hbcc = '';
  621. if (count($this->Bcc) > 0) {
  622. $arr = array();
  623. foreach ($this->Bcc as $bcc) {
  624. if (FUNC5::is_mail($bcc, false, $debug)) {
  625. $arr[] = $bcc;
  626. $addrs[] = $bcc;
  627. }
  628. }
  629. if (count($arr) > 0) {
  630. $header['local'][] = 'Bcc: '.implode(', ', $arr);
  631. $hbcc = MIME5::LE.'Bcc: '.implode(', ', $arr);
  632. }
  633. }
  634. $hn = MIME5::encode_header($this->Subject['content'],
  635. isset($this->Subject['charset']) ? $this->Subject['charset'] : null,
  636. isset($this->Subject['encoding']) ? $this->Subject['encoding'] : null,
  637. null, null, $debug);
  638. $subject = $hn;
  639. $header['client'][] = 'Subject: '.$hn;
  640. if (is_int($this->Priority) || is_string($this->Priority)) {
  641. $arr = false;
  642. if ($this->Priority == 1 || $this->Priority == 'high') $arr = array(1, 'high');
  643. else if ($this->Priority == 3 || $this->Priority == 'normal') $arr = array(3, 'normal');
  644. else if ($this->Priority == 5 || $this->Priority == 'low') $arr = array(5, 'low');
  645. if ($arr) {
  646. $header['local'][] = 'X-Priority: '.$arr[0];
  647. $header['local'][] = 'X-MSMail-Priority: '.$arr[1];
  648. $header['client'][] = 'X-Priority: '.$arr[0];
  649. $header['client'][] = 'X-MSMail-Priority: '.$arr[1];
  650. }
  651. }
  652. $header['client'][] = 'Message-Id: <'.MIME5::unique().'@xpertmailer.com>';
  653. if (count($this->Header) > 0) {
  654. foreach ($this->Header as $harr) {
  655. if (isset($harr['name'], $harr['value']) && strlen($harr['name']) >= 2 && FUNC5::is_alpha($harr['name'], true, '-')) {
  656. $hn = MIME5::encode_header($harr['value'],
  657. isset($harr['charset']) ? $harr['charset'] : null,
  658. isset($harr['encoding']) ? $harr['encoding'] : null,
  659. null, null, $debug);
  660. $header['local'][] = ucfirst($harr['name']).': '.$hn;
  661. $header['client'][] = ucfirst($harr['name']).': '.$hn;
  662. }
  663. }
  664. }
  665. $text = $html = $att = null;
  666. if (isset($this->Text['content'])) {
  667. $text = MIME5::message($this->Text['content'], 'text/plain', null,
  668. isset($this->Text['charset']) ? $this->Text['charset'] : null,
  669. isset($this->Text['encoding']) ? $this->Text['encoding'] : null,
  670. null, null, null, null, $debug);
  671. }
  672. if (isset($this->Html['content'])) {
  673. $html = MIME5::message($this->Html['content'], 'text/html', null,
  674. isset($this->Html['charset']) ? $this->Html['charset'] : null,
  675. isset($this->Html['encoding']) ? $this->Html['encoding'] : null,
  676. null, null, null, null, $debug);
  677. }
  678. if (count($this->Attach) > 0) {
  679. $att = array();
  680. foreach ($this->Attach as $attach) {
  681. if (isset($attach['content'])) {
  682. $att[] = MIME5::message($attach['content'],
  683. isset($attach['type']) ? $attach['type'] : null,
  684. isset($attach['name']) ? $attach['name'] : null,
  685. isset($attach['charset']) ? $attach['charset'] : null,
  686. isset($attach['encoding']) ? $attach['encoding'] : null,
  687. isset($attach['disposition']) ? $attach['disposition'] : null,
  688. isset($attach['id']) ? $attach['id'] : null,
  689. null, null, $debug);
  690. }
  691. }
  692. if (count($att) == 0) $att = null;
  693. }
  694. $arr = MIME5::compose($text, $html, $att);
  695. if ($delivery == 'relay') {
  696. $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);
  697. return $this->_result($_RESULT, $res);
  698. } else if ($delivery == 'local') {
  699. $rpath = (!FUNC5::is_win() && $this->Path != null) ? '-f '.$this->Path : null;
  700. $spath = ($this->Path != null) ? @ini_set('sendmail_from', $this->Path) : false;
  701. $res = mail($to, $subject, $arr['content'], implode(MIME5::LE, $header['local']).MIME5::LE.$arr['header'], $rpath);
  702. if ($spath) @ini_restore('sendmail_from');
  703. return $this->_result(array(0 => 'send mail local'), $res);
  704. } else if ($delivery == 'client') {
  705. $group = array();
  706. foreach ($addrs as $addr) {
  707. $exp = explode('@', $addr);
  708. $group[strtolower($exp[1])][] = $addr;
  709. }
  710. $ret = true;
  711. $reg = (count($group) == 1);
  712. foreach ($group as $domain => $arrs) {
  713. $con = SMTP5::mxconnect($domain, $this->Port, $this->Tout, $this->Name, $this->Context, $debug);
  714. if ($reg) $this->_result(array($domain => $_RESULT));
  715. if ($con) {
  716. 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;
  717. if ($reg) $this->_result(array($domain => $_RESULT));
  718. SMTP5::disconnect($con, $debug);
  719. } else $ret = false;
  720. }
  721. if (!$reg) $this->_result(array(0 => 'send mail client'));
  722. return $ret;
  723. } else if ($delivery == 'sendmail' || $delivery == 'qmail') {
  724. $ret = false;
  725. $comm = (($delivery == 'sendmail') ? $this->SendMail : $this->QMail).' -oi'.(($this->Path != null) ? ' -f '.$this->Path : '').' -t';
  726. if ($con = popen($comm, 'w')) {
  727. if (fputs($con, implode(MIME5::LE, $header['client']).$hbcc.MIME5::LE.$arr['header'].MIME5::LE.MIME5::LE.$arr['content'])) {
  728. $res = pclose($con) >> 8 & 0xFF;
  729. if ($res == 0) {
  730. $ret = true;
  731. $this->_result(array(0 => 'send mail using "'.ucfirst($delivery).'" program'));
  732. } else $this->_result(array(0 => $res));
  733. } else $this->_result(array(0 => 'can not write'));
  734. } else $this->_result(array(0 => 'can not write line command'));
  735. return $ret;
  736. }
  737. }
  738. }
  739. }
  740. ?>