source: mangos/inc/mail/smtp.php@ 5

Last change on this file since 5 was 5, checked in by george, 18 years ago

import

File size: 52.9 KB
Line 
1<?php
2
3/***************************************************************************************
4 * *
5 * This file is part of the XPertMailer package (http://xpertmailer.sourceforge.net/) *
6 * *
7 * XPertMailer is free software; you can redistribute it and/or modify it under the *
8 * terms of the GNU General Public License as published by the Free Software *
9 * Foundation; either version 2 of the License, or (at your option) any later version. *
10 * *
11 * XPertMailer is distributed in the hope that it will be useful, but WITHOUT ANY *
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License along with *
16 * XPertMailer; if not, write to the Free Software Foundation, Inc., 51 Franklin St, *
17 * Fifth Floor, Boston, MA 02110-1301 USA *
18 * *
19 * XPertMailer SMTP & POP3 PHP Mail Client. Can send and read messages in MIME Format. *
20 * Copyright (C) 2006 Tanase Laurentiu Iulian *
21 * *
22 ***************************************************************************************/
23
24require_once 'func.php';
25require_once 'mime.php';
26
27class SMTP extends MIME {
28
29 var $_smtpconn;
30 var $_subject;
31 var $_content;
32 var $_arrcon;
33 var $_arrenc;
34
35 var $_crlf = "\r\n";
36 var $_port = 25;
37 var $_unique = 0;
38 var $_timeout = 30;
39 var $_chanklen = 70;
40
41 var $max_cl = 99;
42 var $max_sl = 1024;
43 var $result = 'unknown';
44
45 var $_header = false;
46 var $_attach = false;
47
48 var $_toaddrs = false;
49 var $_ccaddrs = false;
50 var $_bccaddrs = false;
51 var $_fromaddr = false;
52 var $_fromhost = false;
53
54 var $_relay = false;
55 var $_chunk = false;
56 var $_atext = false;
57 var $_ahtml = false;
58
59 function SMTP(){
60 //parent::MIME();
61 $this->_smtpconn = array('local');
62 $this->_arrcon = array('local' => '', 'client' => '', 'relay' => '');
63 $this->_arrenc = array('7bit' => '', '8bit' => '', 'quoted-printable' => '', 'base64' => ''); // binary not alowed
64 }
65
66 function delivery($conn){
67
68 $ret = false;
69 if(is_string($conn)){
70 $conn = trim($conn);
71 if(FUNC::is_alpha($conn, false, '-')){
72 $exp = explode('-', $conn);
73 $rep = array();
74 foreach($exp as $val){
75 $val = strtolower($val);
76 if(isset($this->_arrcon[$val])) $rep[] = $val;
77 else trigger_error('Invalid connection type value "'.$val.'", on class SMTP::delivery()', 512);
78 }
79 if(count($rep) > 0){
80 $this->_smtpconn = $rep;
81 $ret = true;
82 }
83 }else trigger_error('Invalid parameter value, on class SMTP::delivery()', 512);
84 }else trigger_error('Invalid parameter type value, on class SMTP::delivery()', 512);
85 return $ret;
86
87 }
88
89 function port($num){
90
91 $ret = false;
92 if(is_int($num)){
93 $this->_port = $num;
94 $ret = true;
95 }else trigger_error('Invalid parameter type value, on class SMTP::port()', 512);
96 return $ret;
97
98 }
99
100 function timeout($num){
101
102 $ret = false;
103 if(is_int($num)){
104 $this->_timeout = $num;
105 $ret = true;
106 }else trigger_error('Invalid parameter type value, on class SMTP::timeout()', 512);
107 return $ret;
108
109 }
110
111 function relay($raddr, $ruser = false, $rpass = false, $rport = 25, $rauth = 'autodetect', $rvssl = false){
112
113 $ret = false;
114 if(is_string($raddr)){
115 $raddr = FUNC::str_clear($raddr, array(' '));
116 $raddr = trim(strtolower($raddr));
117 if($raddr != ""){
118 $ret = true;
119 if(FUNC::is_ipv4($raddr)) $rvip = $raddr;
120 else{
121 $rvip = gethostbyname($raddr);
122 if($rvip == $raddr){
123 $ret = false;
124 trigger_error('Invalid hostname value "'.$raddr.'", on class SMTP::relay()', 512);
125 }
126 }
127 }else trigger_error('Invalid hostname/ip value, on class SMTP::relay()', 512);
128 }else trigger_error('Invalid hostname/ip type value, on class SMTP::relay()', 512);
129 if($ret){
130 if(is_bool($ruser)){
131 if(!$ruser) $ruser = '';
132 else trigger_error('Invalid username 1 type value, on class SMTP::relay()', 512);
133 }elseif(is_string($ruser)){
134 $ruser = FUNC::str_clear($ruser);
135 $ruser = trim($ruser);
136 }else{
137 $ruser = '';
138 trigger_error('Invalid username 2 type value, on class SMTP::relay()', 512);
139 }
140 if(is_bool($rpass)){
141 if(!$rpass) $rpass = '';
142 else trigger_error('Invalid password 1 type value, on class SMTP::relay()', 512);
143 }elseif(is_string($rpass)){
144 $rpass = FUNC::str_clear($rpass);
145 $rpass = trim($rpass);
146 }else{
147 $rpass = '';
148 trigger_error('Invalid password type value, on class SMTP::relay()', 512);
149 }
150 if(($ruser != "" && $rpass == "") || ($ruser == "" && $rpass != "")){
151 $ruser = $rpass = '';
152 trigger_error('Invalid username and password combination value, on class SMTP::relay()', 512);
153 }
154 if(!is_int($rport)){
155 $rport = 25;
156 trigger_error('Invalid port type value, on class SMTP::relay()', 512);
157 }
158 if(is_string($rauth)){
159 $rauth = trim(strtolower($rauth));
160 if(!($rauth == "autodetect" || $rauth == "login" || $rauth == "plain")){
161 $rauth = 'autodetect';
162 trigger_error('Invalid auth value, on class SMTP::relay()', 512);
163 }
164 }else{
165 $rauth = 'autodetect';
166 trigger_error('Invalid auth type value, on class SMTP::relay()', 512);
167 }
168 if(is_string($rvssl)){
169 $rvssl = FUNC::str_clear($rvssl);
170 $rvssl = trim(strtolower($rvssl));
171 if(!($rvssl == "tls" || $rvssl == "ssl")){
172 $rvssl = false;
173 trigger_error('Invalid TLS/SSL value, on class SMTP::relay()', 512);
174 }
175 }else{
176 if(is_bool($rvssl)){
177 $rvssl = $rvssl ? 'tls' : false;
178 }else{
179 $rvssl = false;
180 trigger_error('Invalid TLS/SSL type value, on class SMTP::relay()', 512);
181 }
182 }
183 $this->_relay = array('host' => $raddr, 'ip' => $rvip, 'user' => $ruser, 'pass' => $rpass, 'port' => $rport, 'auth' => $rauth, 'ssl' => $rvssl);
184 }
185 return $ret;
186
187 }
188
189 function addheader($hname, $hvalue){
190
191 $ret = false;
192 if(is_string($hname)){
193 $hname = FUNC::str_clear($hname, array(' '));
194 $hname = trim($hname);
195 if($hname != ""){
196 if(is_string($hvalue)){
197 $hvalue = str_replace("\r\n\t", " ", $hvalue);
198 $hvalue = FUNC::str_clear($hvalue);
199 $hvalue = trim($hvalue);
200 if($hvalue != ""){
201 $vname = strtolower($hname);
202 if($vname == "subject") trigger_error('Can not set "Subject" header value, for this, use function "Send()", on class SMTP::addheader()', 512);
203 elseif($vname == "from") trigger_error('Can not set "From" header value, for this, use function "From()", on class SMTP::addheader()', 512);
204 elseif($vname == "to") trigger_error('Can not set "To" header value, for this, use function "AddTo()", on class SMTP::addheader()', 512);
205 elseif($vname == "cc") trigger_error('Can not set "Cc" header value, for this, use function "AddCc()", on class SMTP::addheader()', 512);
206 elseif($vname == "bcc") trigger_error('Can not set "Bcc" header value, for this, use function "AddBcc()", on class SMTP::addheader()', 512);
207 elseif($vname == "date") trigger_error('Can not set "Date" header value, this value is automaticaly set, on class SMTP::addheader()', 512);
208 elseif($vname == "x-mailer") trigger_error('Can not set "X-Mailer" header value, this value is automaticaly set, on class SMTP::addheader()', 512);
209 elseif($vname == "content-type") trigger_error('Can not set "Content-Type" header value, this value is automaticaly set, on class SMTP::addheader()', 512);
210 elseif($vname == "content-transfer-encoding") trigger_error('Can not set "Content-Transfer-Encoding" header value, this value is automaticaly set, on class SMTP::addheader()', 512);
211 elseif($vname == "content-disposition") trigger_error('Can not set "Content-Disposition" header value, this value is automaticaly set, on class SMTP::addheader()', 512);
212 elseif($vname == "x-priority") trigger_error('Can not set "X-Priority" header value, for this, use function "Priority()", on class SMTP::addheader()', 512);
213 elseif($vname == "x-msmail-priority") trigger_error('Can not set "X-MSMail-Priority" header value, for this, use function "Priority()", on class SMTP::addheader()', 512);
214 elseif($vname == "mime-version") trigger_error('Can not set "MIME-Version" header value, this value is automaticaly set, on class SMTP::addheader()', 512);
215 else{
216 $ret = true;
217 $this->_header[] = array('name' => ucfirst($hname), 'value' => $hvalue);
218 }
219 }else trigger_error('Invalid 2\'nd parameter value, on class SMTP::addheader()', 512);
220 }else trigger_error('Invalid 2\'nd parameter type value, on class SMTP::addheader()', 512);
221 }else trigger_error('Invalid 1\'st parameter value, on class SMTP::addheader()', 512);
222 }else trigger_error('Invalid 1\'st parameter type value, on class SMTP::addheader()', 512);
223 return $ret;
224
225 }
226
227 function delheader($hname){
228
229 $ret = false;
230 if(is_string($hname)){
231 $hname = FUNC::str_clear($hname, array(' '));
232 $hname = trim($hname);
233 if($hname != ""){
234 if($this->_header && count($this->_header) > 0){
235 $reparr = array();
236 foreach($this->_header as $harr){
237 if(strtolower($harr['name']) != strtolower($hname)) $reparr[] = $harr;
238 else $ret = true;
239 }
240 $this->_header = $reparr;
241 }
242 }else trigger_error('Invalid parameter value, on class SMTP::delheader()', 512);
243 }else trigger_error('Invalid parameter type value, on class SMTP::delheader()', 512);
244 return $ret;
245
246 }
247
248 function addto($adrr, $name = ''){
249
250 $ret = false;
251 if(is_string($adrr)){
252 $adrr = FUNC::str_clear($adrr, array(' '));
253 $adrr = strtolower(trim($adrr));
254 if($adrr != "" && FUNC::is_mail($adrr)){
255 if(!isset($this->_toaddrs[$adrr])){
256 $this->_toaddrs[$adrr] = '';
257 $ret = true;
258 if(is_string($name)){
259 $name = FUNC::str_clear($name);
260 $name = trim($name);
261 if($name != "") $this->_toaddrs[$adrr] = $this->qpheader($name);
262 }else trigger_error('Invalid 2\'nd parameter type value, on class SMTP::addto()', 512);
263 }else trigger_error('Already exists, on class SMTP::addto()', 512);
264 }else trigger_error('Invalid 1\'st parameter value, on class SMTP::addto()', 512);
265 }else trigger_error('Invalid 1\'st parameter type value, on class SMTP::addto()', 512);
266 return $ret;
267
268 }
269
270 function delto($adrr = 'all'){
271
272 $ret = false;
273 if(is_string($adrr)){
274 $adrr = FUNC::str_clear($adrr, array(' '));
275 $adrr = strtolower(trim($adrr));
276 if($adrr != ""){
277 if($adrr == "all"){
278 $this->_toaddrs = false;
279 $ret = true;
280 }elseif(FUNC::is_mail($adrr)){
281 if(is_array($this->_toaddrs) && count($this->_toaddrs) > 0){
282 $reb = array();
283 foreach($this->_toaddrs as $num => $val){
284 if($num != $adrr) $reb[$num] = $val;
285 else $ret = true;
286 }
287 $this->_toaddrs = $reb;
288 }
289 }else trigger_error('Invalid 2 parameter value, on class SMTP::delto()', 512);
290 }else trigger_error('Invalid 1 parameter value, on class SMTP::delto()', 512);
291 }else trigger_error('Invalid parameter type value, on class SMTP::delto()', 512);
292 return $ret;
293
294 }
295
296 function addcc($adrr, $name = ''){
297
298 $ret = false;
299 if(is_string($adrr)){
300 $adrr = FUNC::str_clear($adrr, array(' '));
301 $adrr = strtolower(trim($adrr));
302 if($adrr != "" && FUNC::is_mail($adrr)){
303 if(!isset($this->_ccaddrs[$adrr])){
304 $this->_ccaddrs[$adrr] = '';
305 $ret = true;
306 if(is_string($name)){
307 $name = FUNC::str_clear($name);
308 $name = trim($name);
309 if($name != "") $this->_ccaddrs[$adrr] = $this->qpheader($name);
310 }else trigger_error('Invalid 2\'nd parameter type value, on class SMTP::addcc()', 512);
311 }else trigger_error('Already exists, on class SMTP::addcc()', 512);
312 }else trigger_error('Invalid 1\'st parameter value, on class SMTP::addcc()', 512);
313 }else trigger_error('Invalid 1\'st parameter type value, on class SMTP::addcc()', 512);
314 return $ret;
315
316 }
317
318 function delcc($adrr = 'all'){
319
320 $ret = false;
321 if(is_string($adrr)){
322 $adrr = FUNC::str_clear($adrr, array(' '));
323 $adrr = strtolower(trim($adrr));
324 if($adrr != ""){
325 if($adrr == "all"){
326 $this->_ccaddrs = false;
327 $ret = true;
328 }elseif(FUNC::is_mail($adrr)){
329 if(is_array($this->_ccaddrs) && count($this->_ccaddrs) > 0){
330 $reb = array();
331 foreach($this->_ccaddrs as $num => $val){
332 if($num != $adrr) $reb[$num] = $val;
333 else $ret = true;
334 }
335 $this->_ccaddrs = $reb;
336 }
337 }else trigger_error('Invalid 2 parameter value, on class SMTP::delcc()', 512);
338 }else trigger_error('Invalid 1 parameter value, on class SMTP::delcc()', 512);
339 }else trigger_error('Invalid parameter type value, on class SMTP::delcc()', 512);
340 return $ret;
341
342 }
343
344 function addbcc($adrr){
345
346 $ret = false;
347 if(is_string($adrr)){
348 $adrr = FUNC::str_clear($adrr, array(' '));
349 $adrr = strtolower(trim($adrr));
350 if($adrr != "" && FUNC::is_mail($adrr)){
351 if(!isset($this->_bccaddrs[$adrr])){
352 $this->_bccaddrs[$adrr] = '';
353 $ret = true;
354 }else trigger_error('Already exists, on class SMTP::addbcc()', 512);
355 }else trigger_error('Invalid parameter value, on class SMTP::addbcc()', 512);
356 }else trigger_error('Invalid parameter type value, on class SMTP::addbcc()', 512);
357 return $ret;
358
359 }
360
361 function delbcc($adrr = 'all'){
362
363 $ret = false;
364 if(is_string($adrr)){
365 $adrr = FUNC::str_clear($adrr, array(' '));
366 $adrr = strtolower(trim($adrr));
367 if($adrr != ""){
368 if($adrr == "all"){
369 $this->_bccaddrs = false;
370 $ret = true;
371 }elseif(FUNC::is_mail($adrr)){
372 if(is_array($this->_bccaddrs) && count($this->_bccaddrs) > 0){
373 $reb = array();
374 foreach($this->_bccaddrs as $num => $val){
375 if($num != $adrr) $reb[$num] = $val;
376 else $ret = true;
377 }
378 $this->_bccaddrs = $reb;
379 }
380 }else trigger_error('Invalid 2 parameter value, on class SMTP::delbcc()', 512);
381 }else trigger_error('Invalid 1 parameter value, on class SMTP::delbcc()', 512);
382 }else trigger_error('Invalid parameter type value, on class SMTP::delbcc()', 512);
383 return $ret;
384
385 }
386
387 function from($adrr, $name = ''){
388
389 $this->_fromaddr = $ret = false;
390 if(is_string($adrr)){
391 $adrr = FUNC::str_clear($adrr, array(' '));
392 $adrr = strtolower(trim($adrr));
393 if($adrr != "" && FUNC::is_mail($adrr)){
394 $ret = true;
395 $this->_fromaddr = array('address' => $adrr, 'name' => '');
396 if(is_string($name)){
397 $name = FUNC::str_clear($name);
398 $name = trim($name);
399 if($name != "") $this->_fromaddr = array('address' => $adrr, 'name' => $this->qpheader($name));
400 }else trigger_error('Invalid 2\'nd parameter type value, on class SMTP::from()', 512);
401 }else trigger_error('Invalid mail address format, on class SMTP::from()', 512);
402 }else trigger_error('Invalid first parameter type value, on class SMTP::from()', 512);
403 return $ret;
404
405 }
406
407 function fromhost($server, &$havemx){
408
409 $this->_fromhost = $ret = $havemx = false;
410 if(is_string($server)){
411 $server = FUNC::str_clear($server, array(' '));
412 $server = strtolower(trim($server));
413 if($server != ""){
414 $ret = true;
415 $this->_fromhost = $server;
416 if(FUNC::is_hostname($server)){
417 $havemx = FUNC::is_win() ? FUNC::getmxrr_win($server, $mxhost) : getmxrr($server, $mxhost);
418 }
419 }else trigger_error('Invalid parameter value, on class SMTP::fromhost()', 512);
420 }else trigger_error('Invalid parameter type value, on class SMTP::fromhost()', 512);
421 return $ret;
422
423 }
424
425 function text($text, $charset = 'iso-8859-1', $encoding = 'quoted-printable', $disposition = 'inline'){
426
427 $this->_atext = $ret = false;
428 if(is_string($charset)){
429 $charset = FUNC::str_clear($charset, array(' '));
430 $charset = trim($charset);
431 $charlen = strlen($charset);
432 if(!($charlen > 1 && $charlen < 60)){
433 $charset = 'us-ascii';
434 trigger_error('Invalid charset value, on class SMTP::text()', 512);
435 }
436 }else{
437 $charset = 'us-ascii';
438 trigger_error('Invalid charset type value, on class SMTP::text()', 512);
439 }
440 if(is_string($encoding)){
441 $encoding = FUNC::str_clear($encoding, array(' '));
442 $encoding = trim(strtolower($encoding));
443 if(!($encoding != "" && isset($this->_arrenc[$encoding]))){
444 $encoding = 'quoted-printable';
445 trigger_error('Invalid encoding value, on class SMTP::text()', 512);
446 }
447 }else{
448 $encoding = 'quoted-printable';
449 trigger_error('Invalid encoding type value, on class SMTP::text()', 512);
450 }
451 if(is_string($disposition)){
452 $disposition = FUNC::str_clear($disposition, array(' '));
453 $disposition = trim(strtolower($disposition));
454 if(!($disposition == "attachment" || $disposition == "inline")){
455 $disposition = 'inline';
456 trigger_error('Invalid disposition value, on class SMTP::text()', 512);
457 }
458 }else{
459 $disposition = 'inline';
460 trigger_error('Invalid disposition type value, on class SMTP::text()', 512);
461 }
462 if(is_string($text)){
463 $text = trim($text);
464 if($text != ""){
465 $htext = 'Content-Type: text/plain;'.$this->_crlf."\t".'charset="'.$charset.'"'.$this->_crlf.
466 'Content-Transfer-Encoding: '.$encoding.$this->_crlf.
467 'Content-Disposition: '.$disposition;
468 $this->_atext = array($htext, $encoding, $text);
469 $ret = true;
470 }else trigger_error('Invalid text/plain value, on class SMTP::text()', 512);
471 }else trigger_error('Invalid text/plain type value, on class SMTP::text()', 512);
472 return $ret;
473
474 }
475
476 function html($html, $charset = 'iso-8859-1', $encoding = 'quoted-printable', $disposition = 'inline'){
477
478 $this->_ahtml = $ret = false;
479 if(is_string($charset)){
480 $charset = FUNC::str_clear($charset, array(' '));
481 $charset = trim($charset);
482 $charlen = strlen($charset);
483 if(!($charlen > 1 && $charlen < 60)){
484 $charset = 'us-ascii';
485 trigger_error('Invalid charset value, on class SMTP::html()', 512);
486 }
487 }else{
488 $charset = 'us-ascii';
489 trigger_error('Invalid charset type value, on class SMTP::html()', 512);
490 }
491 if(is_string($encoding)){
492 $encoding = FUNC::str_clear($encoding, array(' '));
493 $encoding = trim(strtolower($encoding));
494 if(!($encoding != "" && isset($this->_arrenc[$encoding]))){
495 $encoding = 'quoted-printable';
496 trigger_error('Invalid encoding value, on class SMTP::html()', 512);
497 }
498 }else{
499 $encoding = 'quoted-printable';
500 trigger_error('Invalid encoding type value, on class SMTP::html()', 512);
501 }
502 if(is_string($disposition)){
503 $disposition = FUNC::str_clear($disposition, array(' '));
504 $disposition = trim(strtolower($disposition));
505 if(!($disposition == "attachment" || $disposition == "inline")){
506 $disposition = 'inline';
507 trigger_error('Invalid disposition value, on class SMTP::html()', 512);
508 }
509 }else{
510 $disposition = 'inline';
511 trigger_error('Invalid disposition type value, on class SMTP::html()', 512);
512 }
513 if(is_string($html)){
514 $html = trim($html);
515 if($html != ""){
516 $hhtml = 'Content-Type: text/html;'.$this->_crlf."\t".'charset="'.$charset.'"'.$this->_crlf.
517 'Content-Transfer-Encoding: '.$encoding.$this->_crlf.
518 'Content-Disposition: '.$disposition;
519 $this->_ahtml = array($hhtml, $encoding, $html);
520 $ret = true;
521 }else trigger_error('Invalid text/html value, on class SMTP::html()', 512);
522 }else trigger_error('Invalid text/html type value, on class SMTP::html()', 512);
523 return $ret;
524
525 }
526
527 function attachsource($source, $name, $mimetype = 'autodetect', $disposition = 'attachment', $encoding = 'base64'){
528
529 $ret = false;
530 if(is_string($source) && $source != ""){
531 if(is_string($name)){
532 $name = FUNC::str_clear($name);
533 $name = trim($name);
534 if($name != ""){
535 $ret = true;
536 $mime = 'application/octet-stream';
537 if(is_string($mimetype)){
538 $mimetype = FUNC::str_clear($mimetype, array(' '));
539 $mimetype = trim(strtolower($mimetype));
540 $mime = ($mimetype == "autodetect" || $mimetype == "") ? FUNC::mimetype($name) : $mimetype;
541 }
542 $disp = 'attachment';
543 if(is_string($disposition)){
544 $disposition = FUNC::str_clear($disposition, array(' '));
545 $disposition = trim(strtolower($disposition));
546 if($disposition == "attachment" || $disposition == "inline") $disp = $disposition;
547 else trigger_error('Invalid disposition value, on class SMTP::attachsource()', 512);
548 }else trigger_error('Invalid disposition type value, on class SMTP::attachsource()', 512);
549 $encode = 'base64';
550 if(is_string($encoding)){
551 $encoding = FUNC::str_clear($encoding, array(' '));
552 $encoding = trim(strtolower($encoding));
553 if($encoding != "" && isset($this->_arrenc[$encoding])) $encode = $encoding;
554 else trigger_error('Invalid encoding value, on class SMTP::attachsource()', 512);
555 }else trigger_error('Invalid encoding type value, on class SMTP::attachsource()', 512);
556 $this->_attach[] = array('name' => $name, 'mime' => $mime, 'disp' => $disp, 'encode' => $encode, 'source' => $source);
557 }else trigger_error('Invalid name value, on class SMTP::attachsource()', 512);
558 }else trigger_error('Invalid name type value, on class SMTP::attachsource()', 512);
559 }else trigger_error('Invalid source value, on class SMTP::attachsource()', 512);
560 return $ret;
561
562 }
563
564 function attachfile($file, $name = false, $mimetype = 'autodetect', $disposition = 'attachment', $encoding = 'base64'){
565
566 $ret = false;
567 if(is_string($file)){
568 $file = FUNC::str_clear($file);
569 $file = trim($file);
570 if($file != "" && is_file($file) && is_readable($file)){
571 if((is_bool($name) && !$name) || (is_string($name) && $name == '')){
572 $exp1 = explode("/", $file);
573 $name = $exp1[count($exp1)-1];
574 $exp2 = explode("\\", $name);
575 $name = $exp2[count($exp2)-1];
576 }
577 $ret = $this->attachsource(file_get_contents($file), $name, $mimetype, $disposition, $encoding);
578 }else trigger_error('Invalid file source, on class SMTP::attachfile()', 512);
579 }else trigger_error('Invalid file type value, on class SMTP::attachfile()', 512);
580 return $ret;
581
582 }
583
584 function delattach($name = true){
585
586 $ret = false;
587 if(is_bool($name)){
588 if($name){
589 $this->_attach = false;
590 $ret = true;
591 }else trigger_error('Invalid 2 file name type value, on class SMTP::delattach()', 512);
592 }elseif(is_string($name)){
593 $name = trim($name);
594 if($name != ""){
595 if($this->_attach && count($this->_attach) > 0){
596 $rebatt = array();
597 foreach($this->_attach as $attarr){
598 if($attarr['name'] != $name) $rebatt[] = $attarr;
599 else $ret = true;
600 }
601 if($ret) $this->_attach = $rebatt;
602 }
603 }else trigger_error('Invalid file name value, on class SMTP::delattach()', 512);
604 }else trigger_error('Invalid file name type value, on class SMTP::delattach()', 512);
605 return $ret;
606
607 }
608
609 function priority($level = 3){
610
611 $ret = $set = false;
612 if(is_int($level)){
613 if($level == 1) $set = array('1', 'High');
614 elseif($level == 3) $set = array('3', 'Normal');
615 elseif($level == 5) $set = array('5', 'Low');
616 else trigger_error('Invalid 1 parameter value, on class SMTP::priority()', 512);
617 }elseif(is_string($level)){
618 $level = FUNC::str_clear($level, array(' '));
619 $level = trim(strtolower($level));
620 if($level == "high") $set = array('1', 'High');
621 elseif($level == "normal") $set = array('3', 'Normal');
622 elseif($level == "low") $set = array('5', 'Low');
623 else trigger_error('Invalid 2 parameter value, on class SMTP::priority()', 512);
624 }else trigger_error('Invalid parameter type value, on class SMTP::priority()', 512);
625 if($set){
626 $this->delheader('X-Priority');
627 $this->delheader('X-MSMail-Priority');
628 $this->_header[] = array('name' => 'X-Priority', 'value' => $set[0]);
629 $this->_header[] = array('name' => 'X-MSMail-Priority', 'value' => $set[1]);
630 $ret = true;
631 }
632 return $ret;
633
634 }
635
636 function _sendtoip($ip, $arrto, $isrelay){
637
638 $ssl = '';
639 $pnm = $this->_port;
640 if($isrelay){
641 $ssl = $this->_relay['ssl'] ? $this->_relay['ssl'].'://' : '';
642 $pnm = $this->_relay['port'];
643 }
644 if(!$sock = fsockopen($ssl.$ip, $pnm, $errnum, $errmsg, $this->_timeout)){
645 $this->result = 'Error 10: '.$errmsg;
646 return false;
647 }
648 stream_set_timeout($sock, $this->_timeout);
649 $loop = $rcv = 0;
650 while(!feof($sock)){
651 $loop++;
652 if($rcv = fgets($sock, $this->max_sl)){
653 if($loop == $this->max_cl || substr($rcv, 0, 4) != "220-") break;
654 }else break;
655 }
656 if(!$rcv){
657 $this->result = 'Error 11: can\'t read';
658 return false;
659 }
660 if(substr($rcv, 0, 4) != "220 "){
661 $this->result = 'Error 12: '.$rcv;
662 return false;
663 }
664 if(!FUNC::is_connection($sock)){
665 $this->result = 'Error 13: invalid resource connection';
666 return false;
667 }
668 if($isrelay && $this->_relay['user'] != "" && $this->_relay['pass'] != ""){
669 if(!fputs($sock, 'EHLO '.$this->_fromhost.$this->_crlf)){
670 $this->result = 'Error 20: can\'t write';
671 return false;
672 }
673 $loop = $rcv = 0;
674 $getinfo = '';
675 while(!feof($sock)){
676 $loop++;
677 if($rcv = fgets($sock, $this->max_sl)){
678 $getinfo .= $rcv;
679 if($loop == $this->max_cl || substr($rcv, 0, 4) != "250-") break;
680 }else break;
681 }
682 if(!$rcv){
683 $this->result = 'Error 21: can\'t read';
684 return false;
685 }
686 if(substr($rcv, 0, 4) != "250 "){
687 if(!FUNC::is_connection($sock)){
688 $this->result = 'Error 22: invalid resource connection';
689 return false;
690 }
691 if(!fputs($sock, 'HELO '.$this->_fromhost.$this->_crlf)){
692 $this->result = 'Error 23: can\'t write';
693 return false;
694 }
695 $loop = $rcv = 0;
696 $getinfo = '';
697 while(!feof($sock)){
698 $loop++;
699 if($rcv = fgets($sock, $this->max_sl)){
700 $getinfo .= $rcv;
701 if($loop == $this->max_cl || substr($rcv, 0, 4) != "250-") break;
702 }else break;
703 }
704 if(!$rcv){
705 $this->result = 'Error 24: can\'t read';
706 return false;
707 }
708 if(substr($rcv, 0, 4) != "250 "){
709 $this->result = 'Error 25: '.$rcv;
710 return false;
711 }
712 }
713 $authlogin = strstr($getinfo, 'LOGIN');
714 $authplain = strstr($getinfo, 'PLAIN');
715 $authtype = 'login';
716 if($this->_relay['auth'] == "autodetect" || $this->_relay['auth'] == "login"){
717 if(!$authlogin){
718 if($authplain) $authtype = 'plain';
719 }
720 }elseif($this->_relay['auth'] == "plain"){
721 if($authplain) $authtype = 'plain';
722 }
723 if(!FUNC::is_connection($sock)){
724 $this->result = 'Error 26: invalid resource connection';
725 return false;
726 }
727 if($authtype == "login"){
728 if(!fputs($sock, 'AUTH LOGIN'.$this->_crlf)){
729 $this->result = 'Error 270: can\'t write';
730 return false;
731 }
732 if(!$rcv = fgets($sock, $this->max_sl)){
733 $this->result = 'Error 271: can\'t read';
734 return false;
735 }
736 if(substr($rcv, 0, 4) != "334 "){
737 $this->result = 'Error 272: '.$rcv;
738 return false;
739 }
740 if(!FUNC::is_connection($sock)){
741 $this->result = 'Error 273: invalid resource connection';
742 return false;
743 }
744 if(!fputs($sock, base64_encode($this->_relay['user']).$this->_crlf)){
745 $this->result = 'Error 274: can\'t write';
746 return false;
747 }
748 if(!$rcv = fgets($sock, $this->max_sl)){
749 $this->result = 'Error 275: can\'t read';
750 return false;
751 }
752 if(substr($rcv, 0, 4) != "334 "){
753 $this->result = 'Error 276: '.$rcv;
754 return false;
755 }
756 if(!FUNC::is_connection($sock)){
757 $this->result = 'Error 277: invalid resource connection';
758 return false;
759 }
760 if(!fputs($sock, base64_encode($this->_relay['pass']).$this->_crlf)){
761 $this->result = 'Error 278: can\'t write';
762 return false;
763 }
764 if(!$rcv = fgets($sock, $this->max_sl)){
765 $this->result = 'Error 279: can\'t read';
766 return false;
767 }
768 if(substr($rcv, 0, 4) != "235 "){
769 $this->result = 'Error 280: '.$rcv;
770 return false;
771 }
772 }elseif($authtype == "plain"){
773 if(!FUNC::is_connection($sock)){
774 $this->result = 'Error 281: invalid resource connection';
775 return false;
776 }
777 if(!fputs($sock, 'AUTH PLAIN '.base64_encode($this->_relay['user'].chr(0).$this->_relay['user'].chr(0).$this->_relay['pass']).$this->_crlf)){
778 $this->result = 'Error 282: can\'t write';
779 return false;
780 }
781 if(!$rcv = fgets($sock, $this->max_sl)){
782 $this->result = 'Error 283: can\'t read';
783 return false;
784 }
785 if(substr($rcv, 0, 4) != "235 "){
786 $this->result = 'Error 284: '.$rcv;
787 return false;
788 }
789 }
790 }else{
791 if(!fputs($sock, 'HELO '.$this->_fromhost.$this->_crlf)){
792 $this->result = 'Error 30: can\'t write';
793 return false;
794 }
795 $loop = $rcv = 0;
796 while(!feof($sock)){
797 $loop++;
798 if(!$rcv = fgets($sock, $this->max_sl)){
799 if($loop == $this->max_cl || substr($rcv, 0, 4) != "250-") break;
800 }else break;
801 }
802 if(!$rcv){
803 $this->result = 'Error 31: can\'t read';
804 return false;
805 }
806 if(substr($rcv, 0, 4) != "250 "){
807 if(!FUNC::is_connection($sock)){
808 $this->result = 'Error 32: invalid resource connection';
809 return false;
810 }
811 if(!fputs($sock, 'EHLO '.$this->_fromhost.$this->_crlf)){
812 $this->result = 'Error 33: can\'t write';
813 return false;
814 }
815 $loop = $rcv = 0;
816 while(!feof($sock)){
817 $loop++;
818 if(!$rcv = fgets($sock, $this->max_sl)){
819 if($loop == $this->max_cl || substr($rcv, 0, 4) != "250-") break;
820 }else break;
821 }
822 if(!$rcv){
823 $this->result = 'Error 34: can\'t read';
824 return false;
825 }
826 if(substr($rcv, 0, 4) != "250 "){
827 $this->result = 'Error 35: '.$rcv;
828 return false;
829 }
830 }
831 }
832 if(!FUNC::is_connection($sock)){
833 $this->result = 'Error 40: invalid resource connection';
834 return false;
835 }
836 if(!fputs($sock, 'MAIL FROM:<'.$this->_fromaddr['address'].'>'.$this->_crlf)){
837 $this->result = 'Error 41: can\'t write';
838 return false;
839 }
840 if(!$rcv = fgets($sock, $this->max_sl)){
841 $this->result = 'Error 42: can\'t read';
842 return false;
843 }
844 if(substr($rcv, 0, 4) != "250 "){
845 $this->result = 'Error 43: '.$rcv;
846 return false;
847 }
848 $relayh = $isrelay ? '@'.$this->_relay['host'].':' : '';
849 $setver = true;
850 foreach($arrto as $arrval){
851 if(!FUNC::is_connection($sock)){
852 $this->result = 'Error 50: invalid resource connection';
853 $setver = false;
854 break;
855 }
856 if(!fputs($sock, 'RCPT TO:<'.$relayh.$arrval.'>'.$this->_crlf)){
857 $this->result = 'Error 51: can\'t write';
858 $setver = false;
859 break;
860 }
861 if(!$rcv = fgets($sock, $this->max_sl)){
862 $this->result = 'Error 52: can\'t read';
863 $setver = false;
864 break;
865 }
866 $submsg = substr($rcv, 0, 4);
867 if(!($submsg == "250 " || $submsg == "251 ")){
868 $this->result = 'Error 53: '.$rcv;
869 $setver = false;
870 break;
871 }
872 }
873 if(!$setver) return false;
874 if(!FUNC::is_connection($sock)){
875 $this->result = 'Error 60: invalid resource connection';
876 return false;
877 }
878 if(!fputs($sock, 'DATA'.$this->_crlf)){
879 $this->result = 'Error 61: can\'t write';
880 return false;
881 }
882 if(!$rcv = fgets($sock, $this->max_sl)){
883 $this->result = 'Error 62: can\'t read';
884 return false;
885 }
886 if(substr($rcv, 0, 4) != "354 "){
887 $this->result = 'Error 63: '.$rcv;
888 return false;
889 }
890 if(!FUNC::is_connection($sock)){
891 $this->result = 'Error 70: invalid resource connection';
892 return false;
893 }
894 if(!fputs($sock, $this->_content['header']['client'])){
895 $this->result = 'Error 71: can\'t write';
896 return false;
897 }
898 $setver = true;
899 foreach($this->_content['body'] as $partmsg){
900 if(!FUNC::is_connection($sock)){
901 $this->result = 'Error 72: invalid resource connection';
902 $setver = false;
903 break;
904 }
905 if(!fputs($sock, $partmsg)){
906 $this->result = 'Error 73: can\'t write';
907 $setver = false;
908 break;
909 }
910 }
911 if(!$setver) return false;
912 if(!FUNC::is_connection($sock)){
913 $this->result = 'Error 80: invalid resource connection';
914 return false;
915 }
916 if(!fputs($sock, $this->_crlf.'.'.$this->_crlf)){
917 $this->result = 'Error 81: can\'t write';
918 return false;
919 }
920 if(!$rcv = fgets($sock, $this->max_sl)){
921 $this->result = 'Error 82: can\'t read';
922 return false;
923 }
924 if(substr($rcv, 0, 4) != "250 "){
925 $this->result = 'Error 83: '.$rcv;
926 return false;
927 }
928 if(FUNC::is_connection($sock)){
929 if(fputs($sock, 'RSET'.$this->_crlf)){
930 if(FUNC::is_connection($sock)){
931 if($rcvr = @fgets($sock, $this->max_sl)){
932 if(substr($rcvr, 0, 3) == "250"){
933 if(fputs($sock, 'QUIT'.$this->_crlf)){
934 if(FUNC::is_connection($sock)){
935 if($rcvq = @fgets($sock, $this->max_sl)) $rcv = $rcvq;
936 FUNC::close($sock);
937 }
938 }
939 }
940 }
941 }
942 }
943 }
944 $this->result = 'Success: '.$rcv;
945 return true;
946
947 }
948
949 function _sendtohost($hname, $arrto, $isrelay){
950
951 $ret = false;
952 if($hname == "localhost"){
953 $ret = mail($this->_content['header']['to'], $this->_subject, implode('', $this->_content['body']), $this->_content['header']['local']);
954 if(!$ret) $ret = $this->_sendtoip('127.0.0.1', $arrto, $isrelay);
955 }else{
956 if($isrelay) $ret = $this->_sendtoip($hname, $arrto, $isrelay);
957 else{
958 if(FUNC::is_ipv4($hname)) $ret = $this->_sendtoip($hname, $arrto, $isrelay);
959 else{
960 $resmx = FUNC::is_win() ? FUNC::getmxrr_win($hname, $mxhost) : getmxrr($hname, $mxhost);
961 $iparr = array();
962 if($resmx){
963 foreach($mxhost as $hostname){
964 $iphost = gethostbyname($hostname);
965 if($iphost != $hname && FUNC::is_ipv4($iphost) && !isset($iparr[$iphost])) $iparr[$iphost] = $iphost;
966 }
967 }else{
968 $iphost = gethostbyname($hname);
969 if($iphost != $hname && FUNC::is_ipv4($iphost)) $iparr[$iphost] = $iphost;
970 }
971 if(count($iparr) > 0){
972 foreach($iparr as $ipaddr) if($ret = $this->_sendtoip($ipaddr, $arrto, $isrelay)) break;
973 }else trigger_error('Can not find any valid ip address for hostname "'.$hname.'", on class SMTP::_sendtohost()', 512);
974 }
975 }
976 }
977 return $ret;
978
979 }
980
981 function _splitmsg($longmsg, $approx = 10240){
982
983 $longmsg = str_replace(array(".\r\n", ".\n", ".\r"), ". ".$this->_crlf, $longmsg);
984 $msgarr = explode($this->_crlf, $longmsg);
985 $addmsg = "";
986 $arrmsg = array();
987 foreach($msgarr as $inline){
988 $addmsg .= $inline.$this->_crlf;
989 if(strlen($addmsg) >= $approx){
990 $arrmsg[] = $addmsg;
991 $addmsg = "";
992 }
993 }
994 if(count($arrmsg) > 0 && $addmsg != "") $arrmsg[] = $addmsg;
995 else $arrmsg[] = $longmsg;
996 return $arrmsg;
997
998 }
999
1000 function _getunique(){
1001 return md5(microtime(1).$this->_unique++);
1002 }
1003
1004 function _putcid($str, $ids){
1005
1006 $find1 = $repl1 = array();
1007 foreach($ids as $name => $code){
1008 $find1[] = "=\"".$name;
1009 $repl1[] = "=\"cid:".$code;
1010 $find2[] = "=".$name;
1011 $repl2[] = "=cid:".$code;
1012 }
1013 $res = str_replace($find1, $repl1, $str);
1014 $res = str_replace($find2, $repl2, $res);
1015 return $res;
1016
1017 }
1018
1019 function _encodemsg($src, $enc){
1020 $res = '';
1021 if($enc == "7bit" || $enc == "8bit") $res .= chunk_split($src, $this->_chanklen, $this->_crlf);
1022 elseif($enc == "base64") $res .= chunk_split(base64_encode($src), $this->_chanklen, $this->_crlf);
1023 elseif($enc == "quoted-printable") $res .= $this->qpencode($src, $this->_chanklen, $this->_crlf);
1024 return $res;
1025 }
1026
1027 function _writemsg(){
1028
1029 if(!$this->_fromaddr){
1030 $fromaddr = ini_get('sendmail_from');
1031 if($fromaddr == ""){
1032 if(isset($_SERVER['SERVER_ADMIN']) && FUNC::is_mail($_SERVER['SERVER_ADMIN'])) $fromaddr = $_SERVER['SERVER_ADMIN'];
1033 elseif(isset($_SERVER['SERVER_NAME'])) $fromaddr = 'postmaster@'.$_SERVER['SERVER_NAME'];
1034 elseif(isset($_SERVER['HTTP_HOST'])) $fromaddr = 'postmaster@'.$_SERVER['HTTP_HOST'];
1035 elseif(isset($_SERVER['REMOTE_ADDR'])) $fromaddr = 'postmaster@'.$_SERVER['REMOTE_ADDR'];
1036 elseif(isset($_SERVER['SERVER_ADDR'])) $fromaddr = 'postmaster@'.$_SERVER['SERVER_ADDR'];
1037 else $fromaddr = 'postmaster@localhost';
1038 }
1039 $this->_fromaddr = array('address' => $fromaddr, 'name' => '');
1040 }
1041 if(!$this->_fromhost){
1042 if(isset($_SERVER['SERVER_NAME'])) $this->_fromhost = $_SERVER['SERVER_NAME'];
1043 elseif(isset($_SERVER['HTTP_HOST'])) $this->_fromhost = $_SERVER['HTTP_HOST'];
1044 elseif(isset($_SERVER['REMOTE_ADDR'])) $this->_fromhost = $_SERVER['REMOTE_ADDR'];
1045 elseif(isset($_SERVER['SERVER_ADDR'])) $this->_fromhost = $_SERVER['SERVER_ADDR'];
1046 else{
1047 $fexp = explode('@', $this->_fromaddr['address']);
1048 $this->_fromhost = $fexp[1];
1049 }
1050 }
1051 $tostr = $ccstr = $bccstr = '';
1052 foreach($this->_toaddrs as $taddr => $tname){
1053 if($tname == "") $tostr .= $taddr.', ';
1054 else $tostr .= '"'.str_replace('"', '\\"', $tname).'" <'.$taddr.'>, ';
1055 }
1056 $tostr = $hto = substr($tostr, 0, -2);
1057 if($this->_ccaddrs && count($this->_ccaddrs) > 0){
1058 foreach($this->_ccaddrs as $caddr => $cname){
1059 if($cname == "") $ccstr .= $caddr.', ';
1060 else $ccstr .= '"'.str_replace('"', '\\"', $cname).'" <'.$caddr.'>, ';
1061 }
1062 $ccstr = substr($ccstr, 0, -2);
1063 }
1064 if($this->_bccaddrs && count($this->_bccaddrs) > 0){
1065 foreach($this->_bccaddrs as $baddr => $bname) $bccstr .= $baddr.', ';
1066 $bccstr = substr($bccstr, 0, -2);
1067 }
1068 if($this->_fromaddr['name'] == "") $fromstr = $this->_fromaddr['address'];
1069 else $fromstr = '"'.str_replace('"', '\\"', $this->_fromaddr['name']).'" <'.$this->_fromaddr['address'].'>';
1070 $arrval1 = $arrval2 = array();
1071 $arrval1[] = array('name' => 'From', 'value' => $fromstr);
1072 $arrval2[] = array('name' => 'From', 'value' => $fromstr);
1073 $arrval2[] = array('name' => 'To', 'value' => $tostr);
1074 $arrval2[] = array('name' => 'Subject', 'value' => $this->_subject);
1075 if($ccstr != ""){
1076 $arrval1[] = array('name' => 'Cc', 'value' => $ccstr);
1077 $arrval2[] = array('name' => 'Cc', 'value' => $ccstr);
1078 }
1079 if($bccstr != "") $arrval1[] = array('name' => 'Bcc', 'value' => $bccstr);
1080 $arrval2[] = array('name' => 'Date', 'value' => date('r'));
1081 if($this->_header && count($this->_header) > 0){
1082 foreach($this->_header as $hvarr){
1083 $arrval1[] = $hvarr;
1084 $arrval2[] = $hvarr;
1085 }
1086 }
1087 $xmail = array('name' => base64_decode('WC1NYWlsZXI='), 'value' => base64_decode('WFBNMiB2LjAuMSA8d3d3LnhwZXJ0bWFpbGVyLmNvbT4='));
1088 $arrval1[] = $xmail;
1089 $arrval2[] = $xmail;
1090 $hval1 = $hval2 = $bval = '';
1091 foreach($arrval1 as $heach1) $hval1 .= $heach1['name'].': '.$heach1['value'].$this->_crlf;
1092 foreach($arrval2 as $heach2) $hval2 .= $heach2['name'].': '.$heach2['value'].$this->_crlf;
1093 $multipart = false;
1094 if($this->_atext && $this->_ahtml) $multipart = true;
1095 if($this->_attach && count($this->_attach) > 0) $multipart = true;
1096 if($multipart){
1097 $bval .= 'This is a message in MIME Format. If you see this, your mail reader does not support this format.'.$this->_crlf.$this->_crlf;
1098 $boundary1 = '=_'.$this->_getunique();
1099 $boundary2 = '=_'.$this->_getunique();
1100 $boundary3 = '=_'.$this->_getunique();
1101 $haveatt = ($this->_attach && count($this->_attach) > 0) ? true : false;
1102 $inline = $attachment = false;
1103 $idarr = array();
1104 if($haveatt){
1105 foreach($this->_attach as $attdesc){
1106 if($attdesc['disp'] == "inline"){
1107 $inline = true;
1108 $fname = $attdesc['name'];
1109 if(!isset($idarr[$fname])) $idarr[$fname] = $this->_getunique();
1110 }else $attachment = true;
1111 }
1112 }
1113 $hadd = '';
1114 if($this->_atext && $this->_ahtml){
1115 $vhtml = (count($idarr) > 0) ? $this->_putcid($this->_ahtml[2], $idarr) : $this->_ahtml[2];
1116 if($inline && $attachment){
1117 $hadd .= 'Content-Type: multipart/mixed;'.$this->_crlf."\t".'boundary="'.$boundary1.'"'.$this->_crlf;
1118 $bval .= '--'.$boundary1.$this->_crlf.
1119 'Content-Type: multipart/related;'.$this->_crlf."\t".'boundary="'.$boundary2.'"'.$this->_crlf.$this->_crlf.
1120 '--'.$boundary2.$this->_crlf.
1121 'Content-Type: multipart/alternative;'.$this->_crlf."\t".'boundary="'.$boundary3.'"'.$this->_crlf.$this->_crlf.
1122 '--'.$boundary3.$this->_crlf.
1123 $this->_atext[0].$this->_crlf.$this->_crlf.
1124 $this->_encodemsg($this->_atext[2], $this->_atext[1]).
1125 $this->_crlf.'--'.$boundary3.$this->_crlf.
1126 $this->_ahtml[0].$this->_crlf.$this->_crlf.
1127 $this->_encodemsg($vhtml, $this->_ahtml[1]).
1128 $this->_crlf.'--'.$boundary3.'--'.$this->_crlf;
1129 foreach($this->_attach as $attdesc){
1130 if($attdesc['disp'] == "inline"){
1131 $bval .= '--'.$boundary2.$this->_crlf.
1132 'Content-Type: '.$attdesc['mime'].$this->_crlf.
1133 'Content-Transfer-Encoding: '.$attdesc['encode'].$this->_crlf.
1134 'Content-Disposition: '.$attdesc['disp'].';'.$this->_crlf."\t".'filename="'.$attdesc['name'].'"'.$this->_crlf.
1135 'Content-ID: <'.$idarr[$attdesc['name']].'>'.$this->_crlf.$this->_crlf.
1136 $this->_encodemsg($attdesc['source'], $attdesc['encode']);
1137 }
1138 }
1139 $bval .= '--'.$boundary2.'--'.$this->_crlf;
1140 foreach($this->_attach as $attdesc){
1141 if($attdesc['disp'] == "attachment"){
1142 $bval .= '--'.$boundary1.$this->_crlf.
1143 'Content-Type: '.$attdesc['mime'].$this->_crlf.
1144 'Content-Transfer-Encoding: '.$attdesc['encode'].$this->_crlf.
1145 'Content-Disposition: '.$attdesc['disp'].';'.$this->_crlf."\t".'filename="'.$attdesc['name'].'"'.$this->_crlf.$this->_crlf.
1146 $this->_encodemsg($attdesc['source'], $attdesc['encode']);
1147 }
1148 }
1149 $bval .= '--'.$boundary1.'--';
1150 }elseif($inline){
1151 $hadd .= 'Content-Type: multipart/related;'.$this->_crlf."\t".'boundary="'.$boundary1.'"'.$this->_crlf;
1152 $bval .= '--'.$boundary1.$this->_crlf.
1153 'Content-Type: multipart/alternative;'.$this->_crlf."\t".'boundary="'.$boundary2.'"'.$this->_crlf.$this->_crlf.
1154 '--'.$boundary2.$this->_crlf.
1155 $this->_atext[0].$this->_crlf.$this->_crlf.
1156 $this->_encodemsg($this->_atext[2], $this->_atext[1]).
1157 $this->_crlf.'--'.$boundary2.$this->_crlf.
1158 $this->_ahtml[0].$this->_crlf.$this->_crlf.
1159 $this->_encodemsg($vhtml, $this->_ahtml[1]).
1160 $this->_crlf.'--'.$boundary2.'--'.$this->_crlf;
1161 foreach($this->_attach as $attdesc){
1162 $bval .= '--'.$boundary1.$this->_crlf.
1163 'Content-Type: '.$attdesc['mime'].$this->_crlf.
1164 'Content-Transfer-Encoding: '.$attdesc['encode'].$this->_crlf.
1165 'Content-Disposition: '.$attdesc['disp'].';'.$this->_crlf."\t".'filename="'.$attdesc['name'].'"'.$this->_crlf.
1166 'Content-ID: <'.$idarr[$attdesc['name']].'>'.$this->_crlf.$this->_crlf.
1167 $this->_encodemsg($attdesc['source'], $attdesc['encode']);
1168 }
1169 $bval .= '--'.$boundary1.'--';
1170 }elseif($attachment){
1171 $hadd .= 'Content-Type: multipart/mixed;'.$this->_crlf."\t".'boundary="'.$boundary1.'"'.$this->_crlf;
1172 $bval .= '--'.$boundary1.$this->_crlf.
1173 'Content-Type: multipart/alternative;'.$this->_crlf."\t".'boundary="'.$boundary2.'"'.$this->_crlf.$this->_crlf.
1174 '--'.$boundary2.$this->_crlf.
1175 $this->_atext[0].$this->_crlf.$this->_crlf.
1176 $this->_encodemsg($this->_atext[2], $this->_atext[1]).
1177 $this->_crlf.'--'.$boundary2.$this->_crlf.
1178 $this->_ahtml[0].$this->_crlf.$this->_crlf.
1179 $this->_encodemsg($vhtml, $this->_ahtml[1]).
1180 $this->_crlf.'--'.$boundary2.'--'.$this->_crlf;
1181 foreach($this->_attach as $attdesc){
1182 $bval .= '--'.$boundary1.$this->_crlf.
1183 'Content-Type: '.$attdesc['mime'].$this->_crlf.
1184 'Content-Transfer-Encoding: '.$attdesc['encode'].$this->_crlf.
1185 'Content-Disposition: '.$attdesc['disp'].';'.$this->_crlf."\t".'filename="'.$attdesc['name'].'"'.$this->_crlf.$this->_crlf.
1186 $this->_encodemsg($attdesc['source'], $attdesc['encode']);
1187 }
1188 $bval .= '--'.$boundary1.'--';
1189 }else{
1190 $hadd .= 'Content-Type: multipart/alternative;'.$this->_crlf."\t".'boundary="'.$boundary1.'"'.$this->_crlf;
1191 $bval .= '--'.$boundary1.$this->_crlf.
1192 $this->_atext[0].$this->_crlf.$this->_crlf.
1193 $this->_encodemsg($this->_atext[2], $this->_atext[1]).
1194 $this->_crlf.'--'.$boundary1.$this->_crlf.
1195 $this->_ahtml[0].$this->_crlf.$this->_crlf.
1196 $this->_encodemsg($vhtml, $this->_ahtml[1]).
1197 $this->_crlf.'--'.$boundary1.'--';
1198 }
1199 }elseif($this->_atext){
1200 $hadd .= 'Content-Type: multipart/mixed;'.$this->_crlf."\t".'boundary="'.$boundary1.'"'.$this->_crlf;
1201 $bval .= '--'.$boundary1.$this->_crlf.
1202 $this->_atext[0].$this->_crlf.$this->_crlf.
1203 $this->_encodemsg($this->_atext[2], $this->_atext[1]).$this->_crlf;
1204 foreach($this->_attach as $attdesc){
1205 $bval .= '--'.$boundary1.$this->_crlf.
1206 'Content-Type: '.$attdesc['mime'].$this->_crlf.
1207 'Content-Transfer-Encoding: '.$attdesc['encode'].$this->_crlf.
1208 'Content-Disposition: '.$attdesc['disp'].';'.$this->_crlf."\t".'filename="'.$attdesc['name'].'"'.$this->_crlf.$this->_crlf.
1209 $this->_encodemsg($attdesc['source'], $attdesc['encode']);
1210 }
1211 $bval .= '--'.$boundary1.'--';
1212 }elseif($this->_ahtml){
1213 $vhtml = (count($idarr) > 0) ? $this->_putcid($this->_ahtml[2], $idarr) : $this->_ahtml[2];
1214 if($inline && $attachment){
1215 $hadd .= 'Content-Type: multipart/mixed;'.$this->_crlf."\t".'boundary="'.$boundary1.'"'.$this->_crlf;
1216 $bval .= '--'.$boundary1.$this->_crlf.
1217 'Content-Type: multipart/related;'.$this->_crlf."\t".'boundary="'.$boundary2.'"'.$this->_crlf.$this->_crlf.
1218 '--'.$boundary2.$this->_crlf.
1219 $this->_ahtml[0].$this->_crlf.$this->_crlf.
1220 $this->_encodemsg($vhtml, $this->_ahtml[1]).$this->_crlf;
1221 foreach($this->_attach as $attdesc){
1222 if($attdesc['disp'] == "inline"){
1223 $bval .= '--'.$boundary2.$this->_crlf.
1224 'Content-Type: '.$attdesc['mime'].$this->_crlf.
1225 'Content-Transfer-Encoding: '.$attdesc['encode'].$this->_crlf.
1226 'Content-Disposition: '.$attdesc['disp'].';'.$this->_crlf."\t".'filename="'.$attdesc['name'].'"'.$this->_crlf.
1227 'Content-ID: <'.$idarr[$attdesc['name']].'>'.$this->_crlf.$this->_crlf.
1228 $this->_encodemsg($attdesc['source'], $attdesc['encode']);
1229 }
1230 }
1231 $bval .= '--'.$boundary2.'--'.$this->_crlf;
1232 foreach($this->_attach as $attdesc){
1233 if($attdesc['disp'] == "attachment"){
1234 $bval .= '--'.$boundary1.$this->_crlf.
1235 'Content-Type: '.$attdesc['mime'].$this->_crlf.
1236 'Content-Transfer-Encoding: '.$attdesc['encode'].$this->_crlf.
1237 'Content-Disposition: '.$attdesc['disp'].';'.$this->_crlf."\t".'filename="'.$attdesc['name'].'"'.$this->_crlf.$this->_crlf.
1238 $this->_encodemsg($attdesc['source'], $attdesc['encode']);
1239 }
1240 }
1241 $bval .= '--'.$boundary1.'--';
1242 }elseif($inline){
1243 $hadd .= 'Content-Type: multipart/related;'.$this->_crlf."\t".'boundary="'.$boundary1.'"'.$this->_crlf;
1244 $bval .= '--'.$boundary1.$this->_crlf.
1245 $this->_ahtml[0].$this->_crlf.$this->_crlf.
1246 $this->_encodemsg($vhtml, $this->_ahtml[1]).$this->_crlf;
1247 foreach($this->_attach as $attdesc){
1248 $bval .= '--'.$boundary1.$this->_crlf.
1249 'Content-Type: '.$attdesc['mime'].$this->_crlf.
1250 'Content-Transfer-Encoding: '.$attdesc['encode'].$this->_crlf.
1251 'Content-Disposition: '.$attdesc['disp'].';'.$this->_crlf."\t".'filename="'.$attdesc['name'].'"'.$this->_crlf.
1252 'Content-ID: <'.$idarr[$attdesc['name']].'>'.$this->_crlf.$this->_crlf.
1253 $this->_encodemsg($attdesc['source'], $attdesc['encode']);
1254 }
1255 $bval .= '--'.$boundary1.'--';
1256 }elseif($attachment){
1257 $hadd .= 'Content-Type: multipart/mixed;'.$this->_crlf."\t".'boundary="'.$boundary1.'"'.$this->_crlf;
1258 $bval .= '--'.$boundary1.$this->_crlf.
1259 $this->_ahtml[0].$this->_crlf.$this->_crlf.
1260 $this->_encodemsg($vhtml, $this->_ahtml[1]).$this->_crlf;
1261 foreach($this->_attach as $attdesc){
1262 $bval .= '--'.$boundary1.$this->_crlf.
1263 'Content-Type: '.$attdesc['mime'].$this->_crlf.
1264 'Content-Transfer-Encoding: '.$attdesc['encode'].$this->_crlf.
1265 'Content-Disposition: '.$attdesc['disp'].';'.$this->_crlf."\t".'filename="'.$attdesc['name'].'"'.$this->_crlf.$this->_crlf.
1266 $this->_encodemsg($attdesc['source'], $attdesc['encode']);
1267 }
1268 $bval .= '--'.$boundary1.'--';
1269 }
1270 }
1271 $hadd .= 'MIME-Version: 1.0';
1272 $hval1 .= $hadd;
1273 $hval2 .= $hadd;
1274 }else{
1275 if($this->_atext){
1276 $hval1 .= $this->_atext[0];
1277 $hval2 .= $this->_atext[0];
1278 $bval .= $this->_encodemsg($this->_atext[2], $this->_atext[1]);
1279 }else{
1280 $hval1 .= $this->_ahtml[0];
1281 $hval2 .= $this->_ahtml[0];
1282 $bval .= $this->_encodemsg($this->_ahtml[2], $this->_ahtml[1]);
1283 }
1284 }
1285 return array('header' => array('to' => $hto, 'local' => $hval1, 'client' => $hval2.$this->_crlf.$this->_crlf), 'body' => $this->_splitmsg($bval));
1286
1287 }
1288
1289 function send($subject){
1290
1291 $ret = false;
1292 if(is_string($subject)){
1293 $subject = FUNC::str_clear($subject);
1294 $subject = trim($subject);
1295 if($subject != ""){
1296 $this->_subject = $this->qpheader($subject);
1297 $ver = true;
1298 if(!($this->_toaddrs && count($this->_toaddrs) > 0)){
1299 $ver = false;
1300 trigger_error('You must set "To" e-mail address(es) using function "AddTo()", on class SMTP::send()', 512);
1301 }
1302 if(!($this->_atext || $this->_ahtml)){
1303 $ver = false;
1304 trigger_error('You must set the mail message using function(s) "Text() or/and Html()", on class SMTP::send()', 512);
1305 }
1306 if($ver){
1307 if($this->_ccaddrs && count($this->_ccaddrs) > 0){
1308 $clearcc1 = array();
1309 foreach($this->_ccaddrs as $ccaddrs1 => $ccname1){
1310 $vercc1 = true;
1311 foreach($this->_toaddrs as $toaddrs1 => $toname1){
1312 if($ccaddrs1 == $toaddrs1) $vercc1 = false;
1313 }
1314 if($vercc1) $clearcc1[$ccaddrs1] = $ccname1;
1315 else trigger_error('The e-mail address "'.$ccaddrs1.'" appear in To and Cc, on class SMTP::send()', 512);
1316 }
1317 $this->_ccaddrs = $clearcc1;
1318 }
1319 if($this->_bccaddrs && count($this->_bccaddrs) > 0){
1320 $clearbcc1 = array();
1321 foreach($this->_bccaddrs as $bccaddrs1 => $bccname1){
1322 $verbcc1 = true;
1323 foreach($this->_toaddrs as $toaddrs2 => $toname2){
1324 if($bccaddrs1 == $toaddrs2) $verbcc1 = false;
1325 }
1326 if($verbcc1) $clearbcc1[$bccaddrs1] = $bccname1;
1327 else trigger_error('The e-mail address "'.$bccaddrs1.'" appear in To and Bcc, on class SMTP::send()', 512);
1328 }
1329 $this->_bccaddrs = $clearbcc1;
1330 }
1331 if($this->_bccaddrs && count($this->_bccaddrs) > 0 && $this->_ccaddrs && count($this->_ccaddrs) > 0){
1332 $clearbcc2 = array();
1333 foreach($this->_bccaddrs as $bccaddrs2 => $bccname2){
1334 $verbcc2 = true;
1335 foreach($this->_ccaddrs as $ccaddrs2 => $ccname2){
1336 if($bccaddrs2 == $ccaddrs2) $verbcc2 = false;
1337 }
1338 if($verbcc2) $clearbcc2[$bccaddrs2] = $bccname2;
1339 else trigger_error('The e-mail address "'.$bccaddrs2.'" appear in Cc and Bcc, on class SMTP::send()', 512);
1340 }
1341 $this->_bccaddrs = $clearbcc2;
1342 }
1343 $group = $alldom = array();
1344 foreach($this->_toaddrs as $toaddrs3 => $toname3){
1345 $exp1 = explode('@', $toaddrs3);
1346 $group[$exp1[1]][] = $toaddrs3;
1347 $alldom[] = $toaddrs3;
1348 }
1349 if($this->_ccaddrs && count($this->_ccaddrs) > 0){
1350 foreach($this->_ccaddrs as $ccaddrs3 => $ccname3){
1351 $exp2 = explode('@', $ccaddrs3);
1352 $group[$exp2[1]][] = $ccaddrs3;
1353 $alldom[] = $ccaddrs3;
1354 }
1355 }
1356 if($this->_bccaddrs && count($this->_bccaddrs) > 0){
1357 foreach($this->_bccaddrs as $bccaddrs3 => $bccname3){
1358 $exp3 = explode('@', $bccaddrs3);
1359 $group[$exp3[1]][] = $bccaddrs3;
1360 $alldom[] = $bccaddrs3;
1361 }
1362 }
1363 $this->_content = $this->_writemsg();
1364 $success = false;
1365 foreach($this->_smtpconn as $conntype){
1366 if(!$success){
1367 if($conntype == "local"){
1368 $success = $this->_sendtohost('127.0.0.1', $alldom, false);
1369 }elseif($conntype == "relay"){
1370 if($this->_relay) $success = $this->_sendtohost($this->_relay['ip'], $alldom, true);
1371 else trigger_error('You must set relay options with function "Relay()" in order to use relay connection, on class SMTP::send()', 512);
1372 }elseif($conntype == "client"){
1373 $back1 = $back2 = true;
1374 foreach($group as $hostname => $domaddrs){
1375 $back1 = $this->_sendtohost($hostname, $domaddrs, false);
1376 if(!$back1) $back2 = false;
1377 }
1378 $success = $back2;
1379 }
1380 }else break;
1381 }
1382 $ret = $success;
1383 }
1384 }else trigger_error('Invalid subject value, on class SMTP::send()', 512);
1385 }else trigger_error('Invalid subject type value, on class SMTP::send()', 512);
1386 return $ret;
1387
1388 }
1389
1390}
1391
1392?>
Note: See TracBrowser for help on using the repository browser.