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