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