PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/db/library/xpertmailer/PHP4/MAIL4.php

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